CSCI 1320 (Principles of Computer Science I), Fall 2012:
Homework 5

Credit:
30 points.

Reading

Be sure you have read chapter 7.

Programming Problems

Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) by sending mail to bmassing@cs.trinity.edu, with each file as an attachment. Please use a subject line that mentions the course and the assignment (e.g., ``csci 1320 homework 5'' or ``CS1 hw5''). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in.

  1. (15 points) This problem compares various ways of operating on arrays and lists in Scala. Your mission is to write two Scala programs, one using arrays and one using lists, each with four different functions that count how many times a particular element appears in an array/list. To make it somewhat easier to get non-trivial arrays/lists to search, the programs will use util.Random.nextInt to generate integer data.

    So, each program should start by prompting the user for the number of elements in the array or list and the maximum value. It should then generate the array or list, and then prompt the user for a number to search for and call each of the different functions to count how many times the number to search for appears in the array or list. The four functions should all accomplish the same goal, but using different methods:

    Here is a sample execution (with text in boldface what you type and text in typewriter font what the program prints):
    how many numbers?
    20
    maximum value?
    10
    the numbers:
    0
    9
    4
    2
    8
    2
    3
    10
    2
    6
    0
    8
    10
    6
    8
    10
    10
    2
    5
    9
    number to search for?
    10
    count using recursion = 4
    count using count = 4
    count using filter = 4
    count using map = 4

    Hints:

  2. (15 points) For this problem your mission is to write a Scala program to evaluate polynomials -- i.e., expressions of the form

    $\displaystyle a_n x^n + a_{n-1} x^{n-1} + \cdots + a_1 x + a_0
$

    The program should prompt for the coefficients $ a_n$ through $ a_0$ and the value of the variable $ x$ and then print the value of the above expression. It's probably simplest to first ask how many coefficients there will be, then ask for that many values (so you can use fill to get the values), and finally ask for the value of the variable $ x$ . Sample execution (as usual, text in boldface is what you type and text in typewriter font is what the program prints):
    how many coefficients?
    4
    enter 4 coefficients, one per line
    1
    3
    2
    4
    enter value for variable
    100
    value is 1030204.0
    (Probably the program should allow entering doubles rather than integers; the example uses integers to make the answer easier to check.)

    Hints:



Berna Massingill
2012-10-17