#include <iostream>
usingnamespace std;
int main()
{
int a;
int sum = 0;
int average;
int count=-1;
cout << "There is one variable, a."<< endl;
do
{
cout << "What is the value of a?" << endl;
cin >> a;
;
count = count ++;
;
}
while (a != 0);
if (a = 0)
sum = sum + a;
average = sum/count;
cout << average << endl;
cout << "You have entered " << count << " numbers." << endl;
cout << "The sum of the numbers you have entered is " << sum <<" ." <<endl;
cout << "The average of all the numbers are " << average
<< " ." << endl;
return 0;
}
There are a large quantity of problems with your code, but the most pressig matter is line 23. That is not the equality comparison operator, that is the copy assignment operator.
#include <iostream>
#include <cstdio>
#include <cstdlib>
usingnamespace std;
int main(int nNumberofArgs,char* pszArgs[])
{
int a[256];
int sum = 0;
int average = 0;
int size;
int counter = 0;
int counter2 = 0;
cout << "What is data set size?: ";
cin >> size;
cout << endl;
for(;counter2 < size;)
{
cout << "Next number: ";
cin >> counter;
a[counter2] = counter;
sum += counter;
cout << endl;
counter2++;
}
average = sum / counter2;
cout << "You have entered " << counter2 << " numbers." << endl;
cout << "The sum of the numbers you have entered is " << sum <<" ." <<endl;
cout << "The average of all the numbers are " << average << " ." << endl;
system("PAUSE");
return 0;
}
I like first program better too.. i don t want to judge because I am just a begginer too but why is in greenleaf program line 23. I don t think that it is necessery to be there and than you don t need line 8 neither. and all you need to include is iostream. I am sorry if I am wrong but that s what I see.
and in the bolong s program well everything what s need to be said was already said.
line 19 = count = count ++ ->change to count++; like LB said
line 20 and 17 = no need;
line 23 = a has to be == to 0 like LB said
line 24 = after line 19 (maybe change to sum += a;) like lixtary said
#include <iostream>
usingnamespace std;
int main()
{
int a;
int sum = 0;
int average;
int count=-1;
cout << "There is one variable, a."<< endl;
do
{
cout << "What is the value of a?" << endl;
cin >> a;
sum = sum + a;
count = count + 1;
}
while (a != 0);
average = sum/count;
cout << "\nYou have entered " << count << " numbers." << endl;
cout << "The sum of the numbers you have entered is " << sum <<" ." <<endl;
cout << "The average of all the numbers are " << average
<< " ." << endl;
return 0;
}
Your code contains a bug. Let assume that the user entered one number equal to 0. In this case sum will be equal to 0 and count also will be equal to 0. So the statement
. There's no need to assume that my code contains not a single bug. I only assume that the user will entered a desirable input. At least It shows the output desired by the one who posted this problem.
I'm not a pro, vlad. Still beginner... unlike you. Hmp. maybe =)
I do not understand such statements as "There's no need to assume that my code contains not a single bug" I think that if you provide a code you shall guarantee that it is correct.