- Write a complete C++ program that does the following:
- Asks the user to enter a number at most 100 and store this in int variable n.
- Declares an int array called nums with capacity n.
- Passes the array to a function called fillRandom that takes a 1D array and its capacity. The function fills the 1D array with random numbers between 1 and 100.
- Your main function then passes the nums array and its capacity n to another function called printArray that prints all elements of the nums array, separated by spaces.
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
- 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.
- Create an integer array and initialize it to random integers in the main function.
- Pass this array to arrayFun and print the results of the arrayFun operations to the monitor.
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
- Write a complete C++ program that does the following:
- Takes 10 integer inputs from a user and stores them in an int array called nums.
- Declares a second int array called copyNums with same capacity as nums.
- Passes the nums array, copyNums array and their capacity to a function called reverseCopy. reverseCopy copies the elements from nums into copyNums in reverse order.
- For example, if nums stores {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} copyNums will store {10, 9, 8, 7, 6, 5, 4, 3, 2, 1} after reverseCopy finishes.
- The main function then prints both the nums and copyNums arrays to the monitor (array elements separated by spaces). You can print the arrays directly in main, or pass the arrays to a printArray function that does this for you.
- printArray will take a 1D array as input, the array capacity, and then print the array elements to the monitor.
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
- Write a complete C++ program that does the following:
- Declares an integer array called nums with capacity 10.
- Declares a 2 x 5 integer array called numsSplit.
- Passes the array to a function called fillRandom that takes a 1D array and its capacity. fillRandom fills the array with random numbers between 1 and 100.
- The main function in your program then passes nums, numsSplit and their capacities to a function called splitArray.
- Recall that a function that a 2D array parameter for a function must provide a number for the second dimension.
- The title line for numsSplit is void numsSplit(int a[], int b[][5], int cap, int row, int col), where cap is the capacity of the 1D array, row is the number of rows in the 2D array and col is the number of columns in the 2D array.
- You might not actually need each of cap, row and col for your function, but it is best practice to pass these values to functions when working with arrays, until you become more comfortable working with arrays and functions.
- splitArray fills the first row of numsSplit with the first five elements from nums, and fills the second row of numsSplit with the last five elements from nums.
- The main function then prints the elements from the nums array separated by spaces.
- The main function then prints the elements from the numsSplit array, with each row from numsSplit printed as its own row.
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
- Write a complete C++ program that does the following:
- Asks the user to enter a number at most 100 and store this in int variable n.
- Declares an int array called nums with capacity n.
- Declares another int array called digits with capacity 10 and initializes each element to 0.
- This array digits has indices 0 to 9.
- Passes the array to a function called fillSingleDigits that takes a 1D array and its capacity. The function fills the 1D array with random single-digit integers.
- Your main function then passes the nums array, its capacity n, and the digits array to another function called countSingleDigits.
- countSingleDigits loops through the nums array and counts how many times each digit appears in it as follows:
- For each digit in nums[i], increment the digits array at digits[nums[i]] by 1.
- You are using the value stored in nums[i] (which is a single digit) as the index for the digits array that you are incrementing.
- Note that single digit numbers are 0 to 9 which correspond to the index positions of an array of size 10.
- You can use the array at each index to flag whether you have seen that index value from the user.
Example of main function that uses these functions:
int main() {
int n;
cout << "Enter a positive number between 1 and 100: ";
cin >> n;
int nums[n];
int digits[10] = {0}; // {0} initializes all elements of the array to 0
fillSingleDigits(nums, n);
countSingleDigits(nums, digits, n);
printArray(nums, n); // prints elements of 1D array separated by spaces
cout << "Count of each digit in nums:\n";
for(int i = 0; i < 10; i++){
cout << i << ": " << digits[i] << endl;
}
return 0;
}
Sample run of program:
Enter a positive number between 1 and 100: 15
2 4 9 5 8 7 0 2 6 7 1 1 9 2 2
Count of each digit in nums:
0: 1
1: 2
2: 4
3: 0
4: 1
5: 1
6: 1
7: 2
8: 1
9: 2