sum/avg/min/max and even or odd

I'm having a bit of trouble with an assignment.
I need to use while loop to obtain 10 numbers. Then I need to output whether each number is even or odd, and output the sum/avg/min/max of the 10 numbers. I've looked at similar questions, but they seem to use array which we did not learn yet.
Below is what I have so far, which makes me stuck in an infinite loop of entering numbers but even/odd does work.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;
int main()
{
	int n;
	int minimum;
	int maximum;
	double average;
	
	int x=1;
	int sum = 0;
	
	while(x <= 10)
	{
	cout << " Please enter a number." << endl;
	cin >> n;
	minimum = n;	
	maximum = n;	
    
    	if (n%2==0) {
		cout << n << " is even.";
	}
		else {
		cout << n << " is odd.";
	}
	
	}

	while(x <= n)
	{
		sum = sum + x;
		x = x + 1;	
	}
	average = sum / 10.0;
	
	while(x<=n)
	{
		cin >> n;
		if (n < minimum)
		{
			minimum = n;
		}
		x = x + 1;
	}

	while(x<=n)
	{
		cin >> n;
		if (n > maximum)
		{
			maximum = n;
		}
		x = x + 1;
	}
	
	cout << "The average is " << average << endl;
	cout << "The minimum is " << minimum << endl;
	cout << "The maximum is " << minimum << endl;
	cout << "The sum is " << sum << "." << endl;
	return 0;
}
Last edited on
closed account (48T7M4Gy)
You should only be using one cin statement and maybe 2 for loops

1
2
cout << "The minimum is " << minimum << endl;
cout << "The maximum is " << minimum << endl;
Not good.

Try it with pencil and paper first, then pseudocode, then C++ code.
We were specifically instructed to use while loop only
closed account (48T7M4Gy)
We were specifically instructed to use while loop only

OK so maybe 2 while loops and one cin. And, of course an array or similar to store the numbers.
That's ok.

He still has a point. Always make sure you know what you want your program to do and you can do that by the use of pseudocode. I am not sure if writing it on pencil and paper is necessary.

while loops, do-while loops and for loops are interchangeable just make sure when you use a while loop or do-while loop you have the integer that you are looking for declared outside the loop.

Like this:
1
2
3
4
5
6
7
8
9
10
11
#include <array>;
#include <iostream>;
int main () {
std::array<int, 5>person = {1, 2, 3, 4, 5}
int i = 0
while (0 < 5) {
   std::cout << person[i]
   i++
}
return 0;
}


or like so:
1
2
3
4
5
6
7
8
9
10
11
12
#include <array>;
#include <iostream>;
int main() {
std::array<int, 5>person = {1, 2, 3, 4, 5}
int i = 0
do {
   std::cout << person[i]
   i++
}while (0 < 5)
return 0;
}


or finally like:
1
2
3
4
5
6
7
8
9
#include <array>;
#include <iostream>;
int main() {
std::array<int, 5>person = {1, 2, 3, 4, 5}
for (int i = 0; i < 5; i++) {
   std::cout << person[i]
}
return 0;
}


As you can see all three can be interchanged.

If you need more help please take a look at the following or feel free to ask:
http://www.cplusplus.com/doc/tutorial/control/

I hope this helps.

- Hirokachi
Last edited on
closed account (48T7M4Gy)
I am not sure if writing it on pencil and paper is necessary.

I'm sure writing on the paper with a pencil is the preferred method for setting out your pseudocode if you decide to use pencil and paper.
Yes I saw examples using array. However, we did not learn them in class, and the assignment has to be done only using the material learned so far.
The link seems helpful, I'll try to fix the code using that
closed account (48T7M4Gy)
You only need an array if you need/want to store the 10 numbers. Otherwise you can do the calculations progressively as you enter and count the numbers as they are input.
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main()
{
	int n = 0;
	int count = 0;
	int sum = 0;
	int minimum = 10000;
	
	while(count < 5)
	{
	cout << " Please enter a number " ;
	cin >> n;
	
	if (n % 2 == 0)
		cout << n << " is even" << endl;
	else
	    cout << n << " is odd" << endl;
	    
	if (n < minimum)
	    minimum = n;
	    
	 sum += n;
	 count++;
	}
	cout << "The minimum is " << minimum << endl;
	cout << "    The sum is " << sum  << endl;
	
	return 0;
}
Thank you to everyone that posted, I finally got it
Topic archived. No new replies allowed.