CPSC170A
Fundamentals of Computer Science II

Lab 4

Arrays

Arrays Tutorial

Trace Arrays

For each of the following code segments, determine what will be printed when it is run. If it produces a compile or runtime error, just state error. If it prints something different each time, state unknown.

  1. int main() {
    
      int array[3];
      
      for (int i = 0; i < 3; i++) {
        array[i] = i;
      }
      
      std::cout << array[0] << std::endl;
      std::cout << array[1] << std::endl;
      std::cout << array[2] << std::endl;
      std::cout << array[3] << std::endl;
      
      return 0;
    }
  2. void func(int array[]) {
      array[0]++;
    }
    
    int main() {
      int array[] = {1};
      std::cout << array[0]  << std::endl;
      return 0;
    }
  3. int main() {
    
      int array[] = {1, 2, 3};
      int p1 = array[0];
      int p2 = array[1];
      int p3 = array[2];
      
      std::cout << p1 << " " << p2  << " " << p3 << std::endl;
      
      return 0;
    }
  4. int main() {
    
      int array[3];
      
      for (int i = 0; i < 3; i++) {
        array[i] = i;
      }
      
      std::cout << array[0] <<  " " << array[1] << " " << array[2] << std::endl;
      
      return 0;
    }
  5. int main() {
    
      char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};
      int n = 0;
      
      while(str[n] != '\0') {
        n++;
      }
      
      std::cout << n << std::endl;
      std::cout << str << std::endl;
      
      return 0;
    }
  6. int main() {
    
      int matrix[2][2] = {{1, 2}, {3, 4}};
      int x = matrix[0][1];
      int y = matrix[0][1];
      
      std::cout << x << " " << y  << std::endl;
      
      return 0;
    }

Print Array

Write a function print(int array[], int n) that prints the first n numbers in the array. Assume that n is at least 0.

Function Header/Signature:


//PRE: n is at least 0 and less than the size of the array.
//POST: The array has been printed to the standard output.
void print(int array[], int n)

Example

The function call:

Let a be the array {8,12,13,4} of size 4.

print(a, 1);

Would produce the output: [8]

print(a, 4);

Would produce the output: [8, 12, 13, 4]

print(a, 0);

Would produce the output: []

Sum of an Array

Write a function sum(int array[], int n) returns the sum of the first n numbers in the array. Assume that n is at least 0 and less than the size of the array.

Function Header/Signature:


//PRE: n is at least 0 and less than the size of array.
//POST: The sum of the first n numbers in the array has been returned.
int sum(int array[], int n)

Example

The function call:

Let a be the array {8,12,13,4} of size 4.

print(a, 2);

Would return: 20

print(a, 4);

Would return: 37

print(a, 0);

Would return: 0

Multiplication Table

In main declare a 10 x 3 multidimensional array. The entries in the array will be a multiplication table entered by the user. The user provides the numbers to be multiplied while the program does the multiplication. The first pair of numbers to be multiplied will be stored in the first two entries of the first row of the array, the result will be stored in the last entry. The same goes for the remaining pairs, each will be stored in its own row of the array. The program should ask the user for the number of multiplications (at most 10) they wish to compute. Then, for each row, the program asks for the numbers to be multiplied. Once the user is done inputting the numbers, the program should display the multiplication table. Here's a possible use case.

How many multiplications do you want to compute? 3
Enter a pair of numbers to be multiplied: 3 9
Enter a pair of numbers to be multiplied: 7 2
Enter a pair of numbers to be multiplied: 4 8

3 x 9 = 27
7 x 2 = 14
4 x 8 = 32