63 052
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #32257 in C++ for Jeremy Spackman

Question #32257
I need help with a program, but i cant seem to get arrays into the subprograms/subfunctions.

Input a list of positive numbers (terminated 0) in to an array, find the mean (average) of the numbers in the array, and output the results. Use a subprogram to input the numbers, a function to find the mean, and a subprogram to output the results.
Expert's answer
#include <iostream>
using namespace std;


void read_array(double *arr) {
cout << "Input array of numbers(terminated 0):" << endl;
double n;
do {
cin >> n;
*arr = n;
++arr;
} while (n != 0);
}


double get_mean(double *arr) {
double sum = 0;
int cnt = 0;
while (*arr != 0) {
sum += *arr;
++cnt;
++arr;
}
return sum / cnt;
}


void write_mean(double d) {
cout << "The mean is " << d << endl;
}


int main() {
double arr[1000];
read_array(arr);
write_mean(get_mean(arr));
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions