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.
1
Expert's answer
2013-06-24T09:36:16-0400
#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)); }
Comments
Leave a comment