Note that, please do it from scratch there is a programm similar to this But I n
ID: 3861838 • Letter: N
Question
Note that, please do it from scratch there is a programm similar to this But I need Update summing method to incorporate a recursive method to sum the numbers in the array. Please use C++.
thanks in Advance.
Write a program that uses an array to find the Average of 10 double values. Allow the use to enter values for the 10 double numbers. A for loop will facilitate the user input of the 10 random values. Then use a different for loop to iterate through the array summing the total for all 10 double values. Update summing method to incorporate a recursive method to sum the numbers in the array.
Create 1 method to handle user input
Create 1 method to sum and output average of all numbers in array
Create 1 method to output the list of numbers.
Output the list of numbers input by user.
Output the average of all the numbers.
Explanation / Answer
#include <iostream>
using namespace std;
void getInput(double a[], int n) {
for(int i=0; i<n; i++){
cout<<"Enter a number: ";
cin >> a[i];
}
}
double getAverage(double a[], int n){
double sum = 0;
for(int i=0; i<n; i++){
sum = sum + a[i];
}
return sum/n;
}
void printArray(double a[], int n){
cout<<"Array elements are: ";
for(int i=0; i<n; i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
int main()
{
int n = 10;
double a[n];
getInput(a,n);
printArray(a,n);
cout<<"Average is "<<getAverage(a,n)<<endl;
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter a number: 1.2
Enter a number: 2.2
Enter a number: 3.3
Enter a number: 4.4
Enter a number: 5.5
Enter a number: 6.6
Enter a number: 7.7
Enter a number: 8.8
Enter a number: 9.9
Enter a number: 1.1
Array elements are: 1.2 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 1.1
Average is 5.07
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.