Programming average

Whats up everyone. I am a beginner at programming but have already learned so much. I can program on codeblocks to do all sorts of things like multiplying numbers and squaring numbers. Now I have to make a program that will take the average of a set of numbers you type in. Im not sure how to do this though. Could someone show me how to program average? Thanks so much.
Add all the numbers that are inputted, and then divide that number by how many inputs there were.
1
2
3
4
5
6
// Numbers that were inputted
503
407
905
679
408

These numbers add up to 2902. Since there are 5 sets of numbers, we divide by 5, and get 580.4. That is the average.
I know what average is. The problem is I need a program that will average nth amount of numbers. In other words, one can type as many numbers as he/she pleases and the program will take the average of them.
Sorry, snesnerd. I guess you should better describe what you actually need, so others can help you without telling you things you already know. As for my defense, you did say
Now I have to make a program that will take the average of a set of numbers you type in. Im not sure how to do this though. Could someone show me how to program average? Thanks so much.
i just started programing c++ 2 weeks ago soooooo, i made something right now that sort of answers your problem, maybe you can take this code and fix it up to make it work better for yourself.

#include<iostream>
#include<cmath>

using namespace std;

int main(){

int a;
int b;
int c;
int d;
int e;

int answer;

cout << "Please enter five numbers" << endl;
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
answer = (a + b + c + d + e) / 4;
cout << "The average of those numbers are " << answer << endl;

return 0;

}
@snesnerd: why didnt u try it wen u knew wat average is? pls try to code urself, ask if there are any problems or concerns.
I suggest u use an array, assign memory to array using new depending on user input for size of array, then store input values in array, calculate avg of these values.
Last edited on
Or you could do it online with O(1) storage
one can type as many numbers as he/she pleases
while(cin>>number)
if you enter 2 numbers in a cin input, seperated by a space, only the first number will be stored in the variable, however the second one is still stored in the stream buffer and be accessed again by assigning it to another variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

float variable = 0;
float total = 0;
float average = 0;
float numb = 0;

int main()
{
    while (true)
    {
        std::cin >> variable;

        total += variable;

        numb++;

        average = total / numb;

        std::cout << "The average is: " << average << " [" << total << " / " << average << "]" << std::endl;
    }
}


if you enter "10 30 60 72" the output should be

The average is: 10 [10 / 1]
The average is: 20 [40 / 2]
The average is: 33.33333 [100 / 3]
The average is: 43 [172 / 4]

I wrote that all here so I haven't compiled it, should work though.
Topic archived. No new replies allowed.