To Do in C++: For this lab you must: 1. Declare and use an array of 10 integers
ID: 653845 • Letter: T
Question
To Do in C++:
For this lab you must:
1. Declare and use an array of 10 integers in your main function.
2. Collect 10 integer values from the user and store these in the array
3. Create and call a function that takes the array as an argument and returns the smallest value in the array.
4. Create and call a function that takes the array as an argument and returns the largest value in the array.
5. Create and call a function that takes the array as an argument and returns the sum of the array values.
6. Create and call a function that takes the array as an argument and returns the average of the array values.
7. Create and call a function that takes the array as an argument and reverses the order of the values in the array. a. For example: if you pass an array with these values in this order {2,5,3,1,4} i. Then the array will contain these values in this order {4,1,3,5,2} after the call to the function.
8. Create and call a function takes the array as an argument and that will print out the values in the array. a. For example: if the array contains these values in this order {2,5,3,1,4} it will print out: 2, 5, 3, 1, 4
9. Display the smallest value in the array for the user.
10. Display the largest value in the array for the user.
11. Display the sum of the values in the array for the user.
12. Display the average of the values in the array for the user
13. Display the values in the array before it is reversed and after it is reversed
Given:
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
// 1. Declare and use an array of 10 integers in your main function.
const int n = 10;
int array[n];
// 2. Collect 10 integer values from the user and store these in the array
for(int i=0; i<n; i++){
cout <<"Enter value " << (i+1) << " : ";
cin >> array[i];
}
// 3. Create and call a function that takes the array as an argument and returns the smallest value in the array.
int smallest(int array[],int n){
int small = array[0];
for(int i=1; i<n; i++)
if(array[i]<small) small = array[i];
return small;
}
// 4. Create and call a function that takes the array as an argument and returns the largest value in the array.
int largest(int array[],int n){
int large = array[0];
for(int i=1; i<n; i++)
if(array[i]>large) large = array[i];
return large;
}
// 5. Create and call a function that takes the array as an argument and returns the sum of the array values.
int sum(int array[],int n){
int sum = 0;
for(int i=0; i<n; i++) sum += array[i];
return sum;
}
// 6. Create and call a function that takes the array as an argument and returns the average of the array values.
double average(int array[],int n){
return static_cast<double> (sum(array,n))/n;
}
// 7. Create and call a function that takes the array as an argument and reverses the order of the values in the array.
void reverse(int array[],int n){
for(int i=0; i<n/2; i++){
int temp = array[i];
array[i] = array[n-i-1];
array[n-i-1] = temp;
}
}
// 8. Create and call a function takes the array as an argument and that will print out the values in the array.
void print(int array[],int n){
cout << array[0];
for(int i=1; i<n; i++){
cout <<", " << array[i];
}
}
// 9. Display the smallest value in the array for the user.
cout <<"Smallest value in array is " << smallest(array, n) << endl;
// 10. Display the largest value in the array for the user.
cout <<"Largest value in array is " << largest(array, n) << endl;
// 11. Display the sum of the values in the array for the user.
cout <<"Sum of values in array is " << sum(array, n) << endl;
// 12. Display the average of the values in the array for the user
cout <<"Average of values in array is " << average(array, n) << endl;
// 13. Display the values in the array before it is reversed and after it is reversed
print(array,n);
reverse(array,n);
cout << endl << "After reversing array " << endl;
print(array,n);
cin.sync(); cin.get();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.