friends i have 1 question

i am making a program to find out average
but i must be up-to 10 numbers
i made it but it needs 10 numbers if user just want average of 2 numbers he cant use that so how to make program for getting average up-to 10 numbers
my code:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include "conio.h"
using namespace std;
void main()
{
	cout << "enter 10 num to find out there average" << endl;
	int a,b,c,d,e,f,g,h,i,j;
	cout << "1st:" ;
	cin >> a;
	cout << "2nd:";
	cin >> b;
	cout << "3rd:";
	cin >> c;
	cout << "4th:";
	cin >> d;
	cout << "5th:";
	cin >> e;
	cout << "6th:";
	cin >> f;
	cout << "7th:";
	cin >> g;
	cout << "8th:";
	cin >> h;
	cout << "9th:";
	cin >> i;
	cout << "10th:";
	cin >> j;
	cout << "Sum:" << a+b+c+d+e+f+g+h+i+j << endl;
	cout << "average:" << (a+b+c+d+f+g+h+i+j)/10 << endl;
	_getch();
}

plzz help me for making it correct
First, ask the user how many numbers they want to input. Then, go from there.
if user said 5 how to use that? can u give me example by changing this code?

This seems like a job for a partially filled array. You can use a loop that will allow the user to enter in integers and the loop can also keep track of the numbers entered. Then use that array to add all the numbers and divide by how many numbers there are.
You could prompt the user to enter the number of items to average before then iterate x many times in a loop (where x is the number specified by the user). You should store the numbers to be averaged in an array then you can use x as an index to access the values stored in the array. Then just perform another loop to add the numbers together and you should have your solution.

I hope this helps.
but i dont know how to use loop function i am new to C++
Last edited on
This should get you started:

1
2
3
4
5
6
7
8
9
int count = 5;
int times = 0;

do
{
   //loop body goes here

   times++;
}while(times < count);


Note there are other kinds of loops called while loops and for loops. Look them up and choose the best one for your needs.
Last edited on
http://cplusplus.com/doc/tutorial/control

You really need a loop for this problem, unless you want to do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//DON'T DO THIS!!!
int num;
cin >> num; //assume an input in [0,10]

int sum = 0;
int x; //for input

if( num > 9 ) {
   cin >> x;
   sum += x;
} else if( num > 8 ) {
   cin >> x;
   sum += x;
} else if( num > 7 ) {
...
} else if( num > 0 ) {
   cin >> x;
   sum += x;
}

cout << (sum/num) << endl;
return 0;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Aubrey Nickelson // DO NOT USE THIS CODE // FOR EXAMPLE ONLY
#include <iostream>
using namespace std;

int main () {
	
	int count, num, running_sum; // This declare all your variables 
	
    cout << "This program will find the average of a list of numbers\n";
	cout << "How many numbers will you be entering? ";
	cin >> count; // count = number of times you want to enter in data
	cout << "\nNow begin entering in "<< count <<" numbers:\n";
	
	for (int i = 0; i < count; i++) { // declare for loop to run x times
		cin >> num; // read in a number, the old data cotained in num will 
		running_sum += num; // over written, this line same as
	}// running_sum = running_sum + num;
	
	running_sum = running_sum/count; // find the average;
	
	cout << "The average of these numbers is: "<< running_sum; // output the results
    return 0;
}


again do NOT use my code for your homework assignment, just use mine as an example and rewrite your own solution. It is the only way you will learn lol

I just got tired of all the riddles guys... just tell him to use a for loop...
Last edited on
@orange7crush, you don't really have to worry about arrays in this.

You need four variables for this. A count of how many numbers have been entered, the total value of the numbers entered, a temporary variable to store the next number and a queue as to whether or not to continue getting numbers or not. Having a queue allows you to go on as long as the user needs too without forcing them to count out how much data they have. Try it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using std::cout;
using std::endl;
using std::cin;

int main(void)
{
	int count = 0;
	double total = 0;
	double number;
	char queue;
	cout << "Enter \'y\' to continue or anything else to stop" << endl;
	do
	{
		cin >> number;
		count++;
		total += number;
		cout << "Continue? ";
		cin >> queue;
	} while(queue == 'y' || queue == 'Y');

	cout << "The average is: " << total / count 
		<< endl;

	return 0;
}


Also hackthisred it looks as if you're code will throw an exception because in line 16 you are adding to running_sum without initializing it with a value, so it just has a leftover junk value from some other program. Declare it like this int running_sum = 0; and you shouldn't have any troubles.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
	int max = 0, ctr = -1;
	int sum = 0, input = 0;

	cout << "how many numbers are you averaging: ";
	cin >> max;
    
	while((++ctr) < max)
	{
		cout << "Enter number: ";
		cin >> input;

		sum += input;
	}
	cout << "Sum: " << sum << endl;
	cout << "Ave: " << sum / max  << endl;

	return 0;
}


try this code.. i havent compiled it but i think it runs.. :D
Last edited on
These still aren't very user-friendly... I think mine is still the best :)
I just got tired of all the riddles guys... just tell him to use a for loop...

Oh trust me, I would've loved to be more helpful off the bat. But, just the other day I got chewed out for being overly helpful: http://www.cplusplus.com/forum/beginner/43670/

I didn't want a repetition of that. I'm getting mixed signals here :S
Last edited on
thanks all i tried all worked for me
and i had no homework i becoz i dont go to any class to learn c++
i am learning c++ becoz i love it and programming. here, there are no class for students of just 14 years old they need min age 16 years
Topic archived. No new replies allowed.