I have a program that averages numbers. It works perfectly, but I need it to work with multiple functions and arrays. I'm stuck on figuring out how to get it to work like that, and I need some help. My current code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <iomanip>
usingnamespace std;
int main( ) {
float n;
float average;
float o;
float value = 0;
cout << "What size array? ";
cin >> o;
n = o;
for(float i = 0; i < n; ++i) {
cin >> value;
average += value;
}
average /= n;
cout << "The average of these " << o << " numbers is " << fixed << setprecision(2) << average << ".";
}
How are you going to know what these array values are?
Are they going to be given to your program by the user? Are they going to be given one by one? Are they going to be held in a text file? Will the user be giving them in one long string?
If so, you will need to create a function that will input the user's elements into the array, then run the averaging function with some edits so that it will work on the elements of the array.
It's difficult to help when one do not understand the problem. What do you mean by
but I need it to work with multiple functions and arrays
? Did you mean you'd like to store the values the user enters instead of summing them up directly in average += value; ? I'm still a little confused on the multiple functions part of the question.
EDITED: Just a side note, you should strive to always initialize the variables/objects you make use of in the code, e.g. the use of average without initialization is undefined behavior. I'm a slow writer, I didn't know there was a comment dropped before mine, sorry if this looks like duplication.