for Loop problem

So this is the problem I have to work on :
Using for loop and if - else statements , write a program that reads 8 integers from the user. The integers can be positive, negative
or zero values.
After entering your program should do the following :
a) determine the frequency of the positive negative and zero values
b) determine the integer with the lowest value
c) calculate the sum of all positive values
d) caluclate the sum of all negative values

I can grasp what its asking but I'm a bit confused by the for loop. Can someone tell me how can I approach the whole for loop usage? Also how can I get the compiler to determine the lowest value? Not sure if I should declare the integers 8 separate times. I don't know how to sum the values inside the loop as they are repeated by the for function.


This is all I got now

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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
	int integer, freq_pos = 0, freq_neg = 0, freq_zero = 0, sum_pos = 0, sum_neg = 0, total = 0, x, y;
	
	for (x = 0; x <= 8; x++)
	{
	cout << "Please enter an integer" << endl;
	cin >> integer;
	
	if(integer = 0)
	{
		freq_zero = freq_zero + 1;
		sum_pos = sum_pos + 1;
	}
	else
	{
		if (integer < 0)
		{
			freq_neg = freq_neg + 1;
			sum_neg = sum_neg + 1;
		}
	
	else
	{
		if (integer >0)
		{
			freq_pos = freq_pos + 1;
			sum_pos = sum_pos +1;
	    }
    
	else
	{
		if (integer = -99)
		
		break;
	 }
    }
   }
  }
    cout << "There are " << freq_pos << " positive numbers, " << freq_neg << " negative numbers and " << freq_zero << " zeroes" << endl;
	cout << "The lowest number is" << endl;
	cout << "The sum of all positive values is " << sum_pos << endl;
	cout << "The sum of all negative values is " << sum_neg << endl;
}
	
	
Last edited on
You would need to store the inputted numbers in an array or std::vector. The for-loop would be then used to iterate over the array, asking the user to input a number and storing that number in the array, on each iteration.
1
2
3
4
5
6
7
8
9
const int MAX_NUMS = 8;

// zero-initialise array
int nums[MAX_NUMS] = { 0 };
for( int i = 0; i < MAX_NUMS; i++ ) {
    cout << "Enter an integer: ";
    // store input in array
    cin >> nums[i];
}
Okay I'm I figured most of it out except the minimum value part.
How do I get it to display the minimum value?
How do I get it to display the minimum value?

You would typically loop over the elements in the array.
Assume that the smallest element is the first element. Iterate through the array comparing the current smallest element with the current element. If the current element is smaller than the current smallest element, then the current smallest element would be updated to the current element. After you have iterated over the array, you should have the smallest element in that array stored in a variable.
Last edited on
Alright thanks. One last thing is that I have to add a sentinel. It's so that when entered value -500 it will stop. How do I do that with a do while loop?
um anyone?
You would need to store the inputted numbers in an array or std::vector.

No. Don't store the numbers unless you have to. And in this case you don't have to. Just update the various values as you go.

Looking at your code:
Line 11: x <= 8 should be x < 8.
Line 16 & 39: = should be ==.
Line 18 etc. You can increment a variable with the ++ operator: ++freq_zero
Line 19 & 34: You're counting the number of positive values. You're supposed to compute the sum . Same problem with sum_neg.

To track the smallest number you need a variable to hold it:
int minValue;
and then in the code you do
if (integer < minValue) minValue = integer;
The hard part here is "what should you initialize minValue to?" If you set it to something large, then the first number will be smaller than the initial value and everything will work fine, but how big is big enough? 100? 1000? 1000000? It turns out that it's easy to set it to the biggest possible integer value:
1
2
3
#include <limits>
...
    int minValue {numeric_limits<int>::max()};


Thanks that explains a couple of things.
Can you tell me why when counting the SUM or the FREQ for negative values it ignores the first value entered and just counts for the second one? It works for positives and zeroes but for negative the first entered is not counted and it is from the second one.

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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main()
{
	int integer, freq_pos = 0, freq_neg = 0, freq_zero = 0, sum_pos = 0, sum_neg = 0, total = 0, min = 0, max, x, y;
	

	for (y = 0; x < 8; x++)
	{
	cout << "Please enter an integer" << endl;
	cin >> integer;
	
	if (integer < min)
	{
		min = integer;
	}
	else if(integer == 0)
	{
		freq_zero = ++freq_zero;
		sum_pos = sum_pos + integer;
	}
	else if (integer < 0)
		{
			freq_neg = ++freq_neg;
			sum_neg = sum_neg + integer;
		}
	
	else if (integer > 0)
		{
			freq_pos = ++freq_pos;
			sum_pos = sum_pos + integer;
	    } 
	}

    cout << "There are " << freq_pos << " positive numbers, " << freq_neg << " negative numbers and " << freq_zero << " zeroes" << endl;
	cout << "The lowest number is " << min << endl;
	cout << "The sum of all positive values is " << sum_pos << endl;
	cout << "The sum of all negative values is " << sum_neg << endl;
   return 0;
}
   
  
 

	
	
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int integer, freq_pos = 0, freq_neg = 0, freq_zero = 0, sum_pos = 0, sum_neg = 0 ;

for (int x = 0; x <= 8; x++)
{
cout << "Please enter an integer:" ;
cin >> integer;
cout << endl;
if(integer == 0)
{
freq_zero++;
sum_pos++;
}
else if (integer < 0)
{
freq_neg++;
sum_neg++;
}
else if (integer >0)
{
freq_pos++;
sum_pos++;
}
}
cout << "There are " << freq_pos << " positive numbers, " << freq_neg << " negative numbers and " << freq_zero << " zeroes" << endl;
cout << "The lowest number is" << endl;
cout << "The sum of all positive values is " << sum_pos << endl;
cout << "The sum of all negative values is " << sum_neg << endl;
}
Last edited on
Can you tell me why when counting the SUM or the FREQ for negative values it ignores the first value

Remove the "else" at line 20. With the "else" present, it won't update any of the frequency or count values when the value entered is the minimum. Have some fun and enter the numbers 4 3 2 1 0 -1 -2 -3. You'll see that none of the statistics get updated because each value is the new min.
Topic archived. No new replies allowed.