In: Computer Science
Use DevC++ to implement a program that can store and output 5 integers, where the user can input its value into an array named arrayValue. The program should implement the following:
1. Calculate the sum and average of the array.
2. Output the content (value) that stored in the array of five elements.
3. Continue to ask the user to store for storing data into another array.
c++ ONLY PLEASE NEED IT FAST AS POSSIBLE
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<iostream>
using namespace std;
//method to ask user to enter size number of elements and store it in an array
void readArray(int *arr, int size){
cout<<"Enter "<<size<<" integers: ";
//reading size number of elements, storing in array
for(int i=0;i<size;i++){
cin>>arr[i];
}
}
//method to print the contents of an array
void printArray(int *arr, int size){
cout<<"Array: ";
//looping and printing
for(int i=0;i<size;i++){
cout<<arr[i];
//if this is not last element, printing a comma and space
if(i!=size-1){
cout<<", ";
}
}
cout<<endl;
}
//finds the sum of values in an array, returns it
int sum(int *arr, int size){
int s=0;
//summing values
for(int i=0;i<size;i++){
s+=arr[i];
}
return s;
}
//finds the average of values in an array, returns it
double average(int *arr, int size){
//finding sum
int s=sum(arr,size);
//finding average
double avg=(double) s/size;
return avg;
}
//method to display a menu to the user
void displayMenu(){
cout<<"1. Calculate the sum and average of the array."<<endl;
cout<<"2. Output the content (value) that stored in the array of five elements."<<endl;
cout<<"3. Input another set of values."<<endl;
cout<<"4. Quit"<<endl;
cout<<"Enter your choice: ";
}
//main method
int main(){
//setting up an integer array of size 5
const int size=5;
int arr[size];
//reading initial values
readArray(arr,size);
int ch=0;
//looping until user wish to quit
while(ch!=4){
//displaying menu
displayMenu();
//getting choice
cin>>ch;
//handling choice
switch(ch){
case 1: cout<<"Sum is "<<sum(arr,size)<<" and average is "<<average(arr,size)<<endl;
break;
case 2: printArray(arr,size);
break;
case 3: readArray(arr,size);
break;
case 4: cout<<"Bye!"<<endl;
break;
}
}
return 0;
}
/*OUTPUT*/
Enter 5 integers: 1 2 3 4 5
1. Calculate the sum and average of the array.
2. Output the content (value) that stored in the array of five elements.
3. Input another set of values.
4. Quit
Enter your choice: 1
Sum is 15 and average is 3
1. Calculate the sum and average of the array.
2. Output the content (value) that stored in the array of five elements.
3. Input another set of values.
4. Quit
Enter your choice: 2
Array: 1, 2, 3, 4, 5
1. Calculate the sum and average of the array.
2. Output the content (value) that stored in the array of five elements.
3. Input another set of values.
4. Quit
Enter your choice: 3
Enter 5 integers: 15 16 17 22 0
1. Calculate the sum and average of the array.
2. Output the content (value) that stored in the array of five elements.
3. Input another set of values.
4. Quit
Enter your choice: 1
Sum is 70 and average is 14
1. Calculate the sum and average of the array.
2. Output the content (value) that stored in the array of five elements.
3. Input another set of values.
4. Quit
Enter your choice: 2
Array: 15, 16, 17, 22, 0
1. Calculate the sum and average of the array.
2. Output the content (value) that stored in the array of five elements.
3. Input another set of values.
4. Quit
Enter your choice: 4
Bye!