CS 176, Introduction to Computer Science II

Summer 2001 - Quiz 1


  1. Write the prototype for a function named compare which accepts as arguments the two integer arrays sort1 and sort2, compares the two arrays element by element, and returns an integer index value at which the two arrays have different values. compare also accepts as arguments the integer value s1size, which gives the number of elements in sort1, and the integer argument s2size, which gives the number of elements in sort2.

    Do not write the function itself; just write the prototype.


    Either

    int compare(int sort1[], int sort2[], int s1size, int s2size);

    or

    int compare(int[], int[], int, int);

    will do, although the first prototype is preferred because the variable names help document the arguments. Also acceptable were any permutations of the arguments in either prototype, such as

    int compare(int sort1[], int s1size, int sort2[], int s2size);


  2. Write the code for the function
    int check_ones(int count[], int values[], int size);
    
    that searches the integer array values for all elements having the value 1 and copies the index value of those elements into consecutive elements of the array count starting with the first element count[0]. The argument size gives the number of elements available in the arrays values and count.

    For example, if values contained three 1s at values[0], values[3], and values[4], then, when check_ones() returns, count[0] would equal 0, count[1] would equal 3, and count[2] would equal 4.

    The function returns the total number of 1s found in values.


    int check_ones(int count[], int values[], int size) {
    
      int j = 0;
    
      for (int i = 0; i < size; i++)
        if (values[i] == 1)
          count[j++] = i;
    
      return j;
      }
    


  3. Given the following statements
    int year;
    char color;            // 'b' means "blue", 'r' means "red".
    cin >> year >> color;
    
    write a single selection statement for each of the following conditions with each selection statement printing the given message if the given condition holds.

    1. only blue t-shirts were available if the year is less than 1980.

    2. both blue and red t-shirts were available if the color is blue or the color is red or the year is between 1980 and 1989 inclusive.

    3. only red t-shirts are currently available if the color is red and the year is greater than 1989.


    1. if (year < 1980) 
        cout << "only blue t-shirts were available";
      

    2. if ((color == 'b') || (color == 'r') || ((1980 <= year) && (year <= 1989)))
        cout << "both blue and red t-shirts were available";
      
      The extra parenthesis around (1980 <= year) && (year <= 1989) are recommended even though they're not necessary. The extra parenthesis indicate that you want to treat the two conditions (1980 <= year) and (year <= 1989) as a single unit rather than two separate conditions.

    3. if ((color == 'r') && (year > 1989))
        cout << "only red t-shirts are currently available";
      



This page last modified on 23 May 2001.