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:-
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.
//Aubrey Nickelson // DO NOT USE THIS CODE // FOR EXAMPLE ONLY
#include <iostream>
usingnamespace 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...
@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:
#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.
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