Homework 17: Due Tuesday, November 28th

Read Chapter 12, section 12.3, pages 645 through 649.

Complete the following problems:

1.  Write array declarations, including initializers, for the following:

      A.  an array of 8 ints: 2, 4, 8, 16, 32, 64, 128, and 256
      B.  an array of five characters: the letters a, e, i, o, and u
      C.  an array of 10 floating point numbers; the first five numbers
      are 5.5, 12.3, -4.8, 767.0, and -48.9


2.  What does the following program print?

    void swap(int&,int&);
    void print(int[],int);
    int main()
    {
       const int SIZE = 5;
       int nums[SIZE] = {80,40,100,60,20};
       swap(nums[1],nums[4]);
       print(nums,SIZE);
       swap(nums[2],nums[0]);
       print(nums,SIZE);
    }
    void swap(int& i,int& j)
    {
       int temp = i;
       i = j;
       j = i;
    }
    void print(int nums[],int elements)
    {
       int i;
       for (i = 0 ; i < elements ; i++)
          cout << nums[i] << "  ";
       cout << endl;
    }


3.  Write a function named addup which will add all the elements in an
    array of integers. The function should return the sum. The array
    and the size of the array are passed as parameters.


4.  Write a prototype for the function in the previous problem.


5.  Write a function named fill that will read values into an array
    from a file. Assume that the array is large enough to hold all
    values in the file, and that there may be more elements in the
    array than are needed. Your function should read values into the
    array until end of file is reached. The array is passed as a
    parameter. The function should return the array and the number of
    values read into the array. (You can declare and open the input
    file inside the function.)


6.  Write a prototype for the function in the previous problem.


7.  Write the statements to declare an array, call the fill function,
    call the addup function, and print the sum.


8.  Write a function named count that will count and return the number
    of capital As in an array named letters.  The parameters will be
    the array letters and the size of the array. For example:  If the
    array contained the values
             x 8 R A a a 0 s S A
    then the function would return 2.


9.  Write a prototype for the function in the previous problem.


10. Write the statements to declare an array of characters, and initialize
    the array to contain the characters: f A i @ N Z a 7 p Y h A. Call
    the count function and print the number of times the character A is
    contained in the array.

Remember to type your answers and to make two copies of each homework assignment. One copy is submitted to me and the other you keep to use in the group discussions.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2004