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.
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;
@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.
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
#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.