quick help.

Hi,

I wrote this code and I want it to find if all number were positive or negative or mixture? + I want to find the largest and smallest, finding the largest number works fine but finding the smallest is not working. Could you help me using if statement?

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
63
64

#include <iostream>

using namespace std;

void main()
{

   float num,
   average,
   sum,
   largest,
   smallest,
   positive,
   negative;
   
   int count;
   sum = 0;
   count = 1;
   negative = 0;
   
   largest= 0;
   smallest = 0;
   
   while ( count < 11)
   {
      cout << "Number #" << count << ": ";
      cin >> num;
      
      if ( num < 0 )negative++;
         
      sum += num;
      
      if ( count != 0 )
      {
         if ( num > largest )
            largest = num;
         else if ( num < smallest )
            smallest = num;
      }
         
      count++;
    } 
    
    average = ( sum / 10 );


    cout << endl << "The smallest number is " << smallest << endl;
    cout << "The largest number is " << largest << endl;
    cout << "The average of all numbers is " << average << endl;
    cout << "The sum of all numbers is " << sum << endl;
    cout << endl;
    cout << "There are " << negative << " negative numbers." << endl;
   
    if ( negative == 0 )
       cout << "All numbers are positive.";
    else if ( negative == 10 )
       cout << "All numbers are negative.";
    else 
    {
       cout << "There is a mixture of numbers.";
    }
}


made some changes still can't get it right.
Last edited on
Any help would be appreciated. How can make this program work? Thanks
1. Your code doesn't compile
2. You default small to 0. So you have to enter a number smaller than zero before it considers your number the smallest. Default it to something high like 1e20
I updated the code but it still doesn't work. help
Paste your code again pls.
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
63
64

#include <iostream>
using namespace std;

void main()
{

   float num,
   average,
   sum,
   largest,
   smallest,
   positive,
   negative;
   
   int count;
   sum = 0;
   count = 1;
   negative = 0;
   
   largest = 0;
   smallest = 0;
   
   while ( count < 11)
   {
      cout << "Number #" << count << ": ";
      cin >> num;
      
      if ( num < 0 )negative++;
         
      sum += num;
      
      if ( count == 1 )
      {
         if ( num > largest )
            largest = num;
         if ( num < smallest )
            smallest = num;
      }
         
      count++;
    } 
    
    average = ( sum / 10 );


    cout << endl << "The smallest number is " << smallest << endl;
    cout << "The largest number is " << largest << endl;
    cout << "The average of all numbers is " << average << endl;
    cout << "The sum of all numbers is " << sum << endl;
    cout << endl;
    cout << "There are " << negative << " negative numbers." << endl;
   
    if ( negative == 0 )
       cout << "All numbers are positive.";
    else if ( negative == 10 )
       cout << "All numbers are negative.";
    else
    {
       cout << "There is a mixture of numbers.";
    }
}



Read my response. Smallest is still initialised to 0.
I know and I changed it to large number but it still doesn't work when finding if all numbers were positive or negative or mix
Last edited on

the value for smallest and largest is still 0

I tried to use

if ( count == 1 )
{
if ( num > largest )
largest = num;
if ( num < smallest )
smallest = num;
}


it still didn't work
Try 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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
using std::cout;
using std::cin;
using std::fixed;
using std::endl;

#include<iomanip>
using std::setprecision;


int main(){

int
        countNumbers=1,
        number,
        smallest,
        largest,
        negative=0,
        positive=0;

double
        average,
        sum=0;
bool
        allPositives=false,
        allNegatives=false,
        mixedNumbers=false;

while(countNumbers<=10){

cout<<"Enter number #"<<countNumbers<<" ";
cin>>number;

if(countNumbers==1){
        smallest=number;
        largest=number;
}else{

if(number>largest)
        largest=number;
if(number<smallest)
        smallest=number;

}//end if...else

if(number<0)
        negative++;
else
        positive++;

sum+=number;
countNumbers++;

}//end while

average=sum/10;

if(negative>=1 && positive>=1)
        mixedNumbers=true;

if(negative>=1 && positive==0)
        allNegatives=true;

if(negative==0 && positive>=1)
        allPositives=true;



cout<<fixed<<setprecision(2)<<"The largest number is: "<<largest
        <<"\nThe smallest number is: "<<smallest
        <<"\nThe sum is: "<<sum
        <<"\nThe average is: "<<average;

if(mixedNumbers==true)
        cout<<"\nThere are "<<negative<<" negative numbers and "
              <<positive<<" positive numbers"<<endl;

else{
        if(allNegatives==true)
                cout<<"\nThere are "<<negative<<" negative numbers"
                      <<endl;

        else
                cout<<"\nThere are "<<positive<<" positive numbers"<<endl;
}//end if...else
return 0; //indicates successful termination
}//end main 



Juan-Enrique-Hernandez-Perezs-MacBook-Pro:test Enrique$ ./hw_testNumbers 
Enter number #1 -1
Enter number #2 -2
Enter number #3 -3
Enter number #4 -4
Enter number #5 -5
Enter number #6 -6
Enter number #7 -7
Enter number #8 -8
Enter number #9 -9
Enter number #10 -10
The largest number is: -1
The smallest number is: -10
The sum is: -55.00
The average is: -5.50
There are 10 negative numbers



Enter number #1 1
Enter number #2 2
Enter number #3 3
Enter number #4 4
Enter number #5 5
Enter number #6 6
Enter number #7 7
Enter number #8 8
Enter number #9 9
Enter number #10 10
The largest number is: 10
The smallest number is: 1
The sum is: 55.00
The average is: 5.50
There are 10 positive numbers



Enter number #1 -2
Enter number #2 -1
Enter number #3 3
Enter number #4 4
Enter number #5 5
Enter number #6 6
Enter number #7 7
Enter number #8 8
Enter number #9 9
Enter number #10 10
The largest number is: 10
The smallest number is: -2
The sum is: 49.00
The average is: 4.90
There are 2 negative numbers and 8 positive numbers
Last edited on
Topic archived. No new replies allowed.