im stump right now. im looking in my book and i cant solve this program to do what is required this is what i have so far.
"these are the requirements for the assignment. we havent gotten to arrays yet so if you can help with the most basic solution
Design and code a program that asks the user how many numbers from which to determine an average.
Then prompt the user for the integer values and sum them to a total.
Display the sum of the numbers and the calculated average with appropriate accompanying text.
The average should be shown with 1 decimal place.
Repeat the process until the user enters zero (0) as the number of values to be averaged.
// algorithm
// 1) Ask user to enter amount of number they wish to average
// a if user enters a number greater than zero
// 1) enter the numbers to avgerage
// 2) calculate average to 1 decimal point
// 3) loop back to start
// b) if user enters zero
// 2) end program
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
double sum(double[],double);
int main()
{
int num, num2;
double total;
do
{
cout << "Enter the amount of numbers to average: " ;
cin >> num;
if ( num > 0 )
{
for ( int i = 0; i < num; i++)
{
cout << "Enter number " << i + 1 << ":";
cin >> num2;
}
int sum=0;
int i=0;
while (i < total)
{
sum = sum + num2;
i++;
}
cout << "The average of the numbers you entered is " << sum/total << endl;
}
system("pause");
return 0;
} while (num > 0);
}
for ( int i = 0; i < num; i++)
{
cout << "Enter number " << i + 1 << ":";
cin >> num2;
}
You're on the right track but you're missing a key point. You enter a number in num2, but then you dont do anything with it. Here is where your "total" is suppose to come in handy, which you never use in your code by the way.
total = tatal + num2;
This will sum up all your numbers entered and put them into total.
After that you just have to divide it by the variable that holds your amount of numbers, which in this case is num.
so i adjusted a few things around so i can get it to loop correctly now that it can solve an equation.
but what happing now it not doing the math correctly when i enter 1,2,3,4,5 it comes out with the answer is 5 which should be 3
// algorithm
// 1) Ask user to enter amount of number they wish to average
// a if user enters a number greater than zero
// 1) enter the numbers to avgerage
// 2) calculate average to 1 decimal point
// 3) loop back to start
// b) if user enters zero
// 2) end program
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
double sum(double[],double);
int main()
{
int num, num2;
double total;
do
{
cout << "Enter the amount of numbers to average: " ;
cin >> num;
if ( num > 0 )
{
for ( int i = 0; i < num; i++)
{
cout << "Enter number " << i + 1 << ":";
cin >> num2;
total = total + num2;
}
int sum=0;
int i=0;
while (i < total)
{
sum = sum + num2;
i++;
}
cout << "The average of the numbers you entered is " << sum/total << endl;
}
} while (num > 0);
system("pause");
return 0;
}
i see what its doing its not adding up all the numbers its taking the last value entered and using that as the answer how would i go about fixing it to actually take the sum of all number input and get a average
It is adding all the numbers together. That is what total = total + num2; is doing
1 2 3 4 5 6 7 8
int sum=0;
int i=0;
while (i < total)
{
sum = sum + num2;
i++;
}
I have no idea what this code is doing here.
You have the total, or the sum if you want to call it that. Its in the variable total. What else is missing for the average? The amount of numbers entered. And you already have that.
1 2
cout << "Enter the amount of numbers to average: " ;
cin >> num;
// algorithm
// 1) Ask user to enter amount of number they wish to average
// a if user enters a number greater than zero
// 1) enter the numbers to avgerage
// 2) calculate average to 1 decimal point
// 3) loop back to start
// b) if user enters zero
// 2) end program
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
int main()
{
int num, num2;
int total;
float avg;
do
{
cout << "Enter the amount of numbers to average: " ;
cin >> num;
if ( num > 0 )
{
for ( int i = 0; i < num; i++)
{
cout << "Enter number " << i + 1 << ":";
cin >> num2;
total = total + num2;
avg = total/num;
}
cout << "The average of the numbers you entered is " << setprecision(2) << avg << endl;
}
} while (num > 0);
system("pause");
return 0;
}
when i do this its not calculating the answer correctly.
i tried changing the avg to a double. i tried throwing a showpoint in there with setprecision. i all out of idea i know about.
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
int main() {
int num, num2;
do {
cout << "Enter the amount of numbers to average: ";
cin >> num;
int sum = 0;
if (num > 0) {
for (int i = 0; i < num; i++) {
cout << "Enter number " << i + 1 << ":";
cin >> num2;
sum = sum + num2;
cout << "sum:" << sum;
}
cout << "The average of the numbers you entered is "
<< (sum / num) << endl;
}
} while (num > 0);
return 0;
}
Enter the amount of numbers to average: 2
Enter number 1:12
sum:12Enter number 2:12
sum:24The average of the numbers you entered is 12