E.1. Question 1: Populating Arrays Program Requirements: Have the user input a n
ID: 3846655 • Letter: E
Question
E.1. Question 1: Populating Arrays Program Requirements: Have the user input a number, and write a program that populates a 10-element array with multiples of that number. For example, if the user enters 3, the elements of the array would be 3, 6, 9, 12, etc. Write code to print the array from the above question in the following format Element Value 27 You are also required to print the following array characteristics: o Sum of all elements o Average of all elements o Multiply each element by 2 o Reverse the list of elements (must be original array, not modified array). Using the above example, element 1 will be 30, element 2 will be 27 and so on. The program should repeat such that the user can repeat the program with a new integer entry.Explanation / Answer
Below is your code: -
#include<iostream>
#include<string>
using namespace std;
int main() {
int cont = 0;
int num;
string run;
while(cont == 0) {
cout<<"Enter the integer for array multiplier: ";
cin>>num;
cout<<" Element Value ";
int arr[10];
int sum = 0;
for(int i=1;i<=10;i++) {
cout<<(i+1)<<" "<<(i*num)<<endl;
arr[i-1] = i*num;
sum = sum + (i*num);
}
cout<<" Sum: "<<sum;
cout<<" Average: "<<(sum/10);
cout<<" Multiply each element by 2: "<<endl;
cout<<" Element Value ";
for(int i=0;i<10;i++) {
cout<<(i+1)<<" "<<(arr[i]*2)<<endl;
}
cout<<" Reverse: "<<endl;
cout<<" Element Value ";
for(int i=9;i>=0;i--) {
cout<<(10-i)<<" "<<(arr[i])<<endl;
}
cout<<" Would you like to continue Y/N?";
cin>>run;
if(run == "N" || run == "n") {
cont =1;
}
}
}
Sample Run: -
Enter the integer for array multiplier: 2
Element Value
2 2
3 4
4 6
5 8
6 10
7 12
8 14
9 16
10 18
11 20
Sum: 110
Average: 11
Multiply each element by 2:
Element Value
1 4
2 8
3 12
4 16
5 20
6 24
7 28
8 32
9 36
10 40
Reverse:
Element Value
1 20
2 18
3 16
4 14
5 12
6 10
7 8
8 6
9 4
10 2
Would you like to continue Y/N?Y
Enter the integer for array multiplier: 3
Element Value
2 3
3 6
4 9
5 12
6 15
7 18
8 21
9 24
10 27
11 30
Sum: 165
Average: 16
Multiply each element by 2:
Element Value
1 6
2 12
3 18
4 24
5 30
6 36
7 42
8 48
9 54
10 60
Reverse:
Element Value
1 30
2 27
3 24
4 21
5 18
6 15
7 12
8 9
9 6
10 3
Would you like to continue Y/N?y
Enter the integer for array multiplier: 4
Element Value
2 4
3 8
4 12
5 16
6 20
7 24
8 28
9 32
10 36
11 40
Sum: 220
Average: 22
Multiply each element by 2:
Element Value
1 8
2 16
3 24
4 32
5 40
6 48
7 56
8 64
9 72
10 80
Reverse:
Element Value
1 40
2 36
3 32
4 28
5 24
6 20
7 16
8 12
9 8
10 4
Would you like to continue Y/N?n
--------------------------------
Process exited after 10.55 seconds with return value 0
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.