Lab 22

  1. Write a complete C++ program that does the following:
  2. Example main function that uses fillRandom and printArray:
    int main() { int n; cout << "Enter a positive number between 1 and 100: "; cin >> n; int nums[n]; fillRandom(nums, n); printArray(nums, n); // prints elements of 1D array separated by spaces return 0; }

    Sample run of program:
    Enter a positive number between 1 and 100: 11 64 18 96 99 51 75 98 57 30 86 58
  3. Write a complete C++ program with a function called arrayFun that takes an 1D integer array of user-specified size and finds the sum, average, and largest and smallest of the numbers stored in the array.
  4. Example main function that uses arrayFun:
    int main() { int size; cout << "How many numbers to enter? "; cin >> size; int nums[size]; cout << "Enter numbers: "; for(int i = 0; i < size; i++){ cin >> nums[i]; } arrayFun(nums, size); // prints sum, average, largest and smallest of elements in nums return 0; }

    Sample run of program:
    How many numbers to enter? 5 Enter numbers: 12 45 2 13 20 The sum of all elements is 92 The average of all elements is 18.4 The largest element is 45 The smallest element is 2
  5. Write a complete C++ program that does the following:
  6. Example main function that uses reverseCopy:
    int main() { int nums[10], copyNums[10]; cout << "Enter numbers: "; for(int i = 0; i < 10; i++){ cin >> nums[i]; } reverseCopy(nums, copyNums, 10); printArray(nums, 10); // prints elements of array separated by spaces printArray(copyNums, 10); return 0; }

    Sample run of program:
    Enter numbers: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
  7. Write a complete C++ program that does the following:
  8. Example main function that uses splitArray:
    int main() { int nums[10], numsSplit[2][5]; fillRandom(nums, 10); splitArray(nums, numsSplit, 10, 2, 5); printArray1D(nums, 10); // prints elements of 1D array separated by spaces printArray2D(numsSplit, 2, 5); // prints elements of 2D array separated by spaces, each row on its own line return 0; }

    Sample run of program:
    77 57 88 60 23 7 70 64 39 14 77 57 88 60 23 7 70 64 39 14
  9. Write a complete C++ program that does the following: