Array Worksheet

1.  What is the output of the following program?

    #include <iostream.h>
    int main()
    {
       int a[100], b[100], j, m;
       int suma = 0, sumb = 0, sumdiff = 0;
       cin >> m;
       for (j = 0 ; j < m ; j++)
       {
          cin >> a[j] >> b[j];
          suma = suma + a[j];
          sumb += b[j];
          sumdiff = sumdiff + (a[j] - b[j]);
       }
       for (j = m - 1 ; j >= 0 ; j--)
          cout << a[j] << "  " << b[j] << "  " << a[j] - b[j] << endl;
       cout << suma << "  " << sumb << "  " << sumdiff << endl;
    }
    DATA:
    5
    11 15
    19 14
    4 2
    17 6
    1 3


2.  Given:   int h = 6, p = 2, m = 3;
             int values[7];
    Suppose values contains: -4  0  2  6  -2  -1  14
    Show the contents of the array values after:
      for (; m <=5; m++)
         values[m] = values[h] + values[p] * values[m];


3.  Given the declarations:
       int sample[8], i, k;
    show the contents of the array sample after the following code is
    executed. Use a question mark to indicate any garbage values
    in the array.

    for (k = 0 ; k < 8 ; k++)
       if (k % 2)
          sample[k] = 1;


4.  What is the error in the following program segment?

    int main()
    {
       int i, count[10];
       cout << "please enter 10 numbers:  ";
       for (i = 1; i <= 10; i++)
          cin >> count[i];
    }


5.  Write the statements to multiply every element of an array of ints
    (of size 50) by 2.


6.  Write the statements to add up those elements of an array of ints (of
    size 25) which have an even subscript.


7.  Write the statements to add up those elements of an array of ints (of
    size 25) which have an even value.


8.  What will the following program segment print?

    int main()
    {
       int nums[10];
       int i;
       for (i = 9 ; i >= 0 ; i--)
       {
          nums[i] = 5 * (i + 1);
          cout << nums[i] << "  ";
       }
       cout << endl;
       for (i = 0 ; i < 9 ; i++)
          cout << nums[i] << "  ";
       cout << endl;
       for (i = 0 ; i < 9 ; i++)
          nums[i+1] = nums[i];
       for (i = 0 ; i < 9 ; i++)
          cout << nums[i] << "   ";
       cout << endl;
    }


9.  What will the following program segment print?

    int main()
    {
       int nums[10];
       int i;
       for (i = 9 ; i >= 0 ; i--)
       {
          nums[i] = 5 * (i + 1);
          cout << nums[i] << "  ";
       }
       cout << endl;
       for (i = 0 ; i < 9 ; i++)
          cout << nums[i] << "  ";
       cout << endl;
       for (i = 8 ; i >= 0 ; i--)
          nums[i+1] = nums[i];
       for (i = 0 ; i < 9 ; i++)
          cout << nums[i] << "   ";
       cout << endl;
    }


10. Given:   int temps[50];
    Write the statements to print "yes" if any element of the array
    temps contains the value 100.


11. Given:   int temps[50];
    Write the statements to set the variable found to true if any
    element of the array temps contains the value 100. If not, the
    variable found should be false.

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

© Copyright Emmi Schatz 2004