Loops

Feb 8, 2013 at 9:08pm
What is wrong with 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
27
28
29
30
31
32
33
34
#include <iostream>
using namespace 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;

}
Feb 8, 2013 at 9:11pm
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.
Feb 8, 2013 at 10:18pm
I changed it to == but it is still not orking.
Feb 9, 2013 at 12:03am
i think you should also put the sentence "sum = sum + a;" under the "count = count ++;" in the block .
Feb 9, 2013 at 1:35am
Line 19 is undefined behavior. It most likely does nothing. Change it to just say "++count;" and nothing more.
Feb 9, 2013 at 2:04am
Compare and Contrast my program and yours. Analyze my program and maybe you'll learn something.

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
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace 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;

}
Feb 9, 2013 at 4:08am
Frankly, I think the original program was better...
Feb 9, 2013 at 4:43pm
...Explain
Feb 9, 2013 at 5:05pm
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

Last edited on Feb 9, 2013 at 5:27pm
Feb 10, 2013 at 5:13pm
But it works....I guess I got to addicted to arrays...
Feb 12, 2013 at 9:51am
that s fine.. actually that s better then me. i have some problems with arrays. and yes it works fine. :)
Feb 12, 2013 at 12:46pm
closed account (ihq4izwU)
I'm really sorry if I didn't make my code clearer but I just want to make sure that the author who posted this problem can understand it better.
Anyways just try compiling it. =)
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
#include <iostream>
using namespace 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;
}
Feb 12, 2013 at 1:00pm

@mjjm08910


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

average = sum/count;

results in aborting the execution.
Feb 12, 2013 at 1:49pm
closed account (ihq4izwU)
I'm sorry
vlad from moscow
. 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 =)
Feb 12, 2013 at 1:53pm
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.
Feb 13, 2013 at 2:46am
data set should start at 0;

count = 0;
Topic archived. No new replies allowed.