Homework 18: Due Tuesday, December 5th

Read Chapter 12, section 12.5, pages 654 through 663. Skip the section entitled "Internal Array Element Location Algorithm".

Complete the following problems:

1.  Write a search function that receives an array of longs, the
    size of the array, and a number to search for. The function will
    return the subscript of the first occurrence of number if number is
    found in the array. Otherwise, the function will return -1.


2.  Write a prototype for your new search function.


3.  Write the statements to call your function to search for the value
    111223333 in an array called ssns which has 100 elements. Print a
    message telling where the value was found in the array, or that the
    value was not found.


4.  Suppose you have two arrays: ssns and salaries. The ssn and salary
    of each worker is loaded into the same position in the two arrays.
    For example, if your ssn is in ssns[8], then your salary is in
    salaries[8]. Using your search function from question 1, write the
    statements to read in an ssn and print out that person's salary.
    Assume that the arrays have 100 entries. If the ssn is not found,
    print a message stating that the worker is not listed.


5.  Suppose you have the ssns and salaries arrays from question 4.
    Using your search function from question 1, write the statements
    to read in an ssn and a salary, and set that person's salary to
    the new salary that was read in. Print a message saying that the
    salary was updated, but if the ssn is not found, print a message
    stating that the worker is not listed.


6.  Write the statements to declare the following arrays:
       an array of integers with 10 rows and 15 columns
       an array of characters with 8 rows and 5 columns
       an array of floats with 200 rows and 500 columns


7.  What will the following statements print?
       const int ROWS = 3;
       const int COLS = 4;
       int i,j;
       int val[ROWS][COLS} = { {10,20,30,40},
                               {50,60,70,80},
                               {90,100,110,120} };
       for (i = 0 ; i < ROWS ; i++)
       {
          for (j = 0 ; j < COLS ; j++)
             cout << val[i][j] << "  ";
          cout << endl;
       }


8.  What will the following statements print?
       const int ROWS = 3;
       const int COLS = 4;
       int i,j;
       int val[ROWS][COLS} = {10,20,30,40,50,60,70,80,90,100,110,120};
       for (j = 0 ; j < COLS ; j++)
       {
          for (i = 0 ; i < ROWS ; i++)
             cout << val[i][j] << "  ";
          cout << endl;
       }

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