Here's a problem we're supposed to do for my class:
Input five numbers from the user and print the highest, the lowest positive number, the highest (closest to zero) negative number and the lowest number. If the user enters all positives or all negatives, print "none" for the missing output.

I'm not sure how to do this. So far, I have:

#include "stub.h"
int main()
{
   int x = 0;
   int highest_pos = 0;
   int lowest_pos;
   int counter;
   int highest_neg;
   int lowest_neg;
   
   for(counter = 1; counter <= 5; counter ++)
   {
               cin >> x;
               
               if ( x > highest_pos && x > 0)
                  highest_pos = x;
                  
               if ( x < lowest_pos && x > 0)
                  lowest_pos = x;
                  
               if ( x > highest_neg && x < 0)
                  highest_neg = x;
                  
               if ( x < lowest_neg && x < 0)
                  lowest_neg = x;
                  
               
                  
   }
   
                  cout << highest_pos << "     " << lowest_pos << "   " << endl << highest_neg << "    " << lowest_neg << endl;
                  
                  cin.ignore(INT_MAX);

    getchar();
	return 0;
}

Recommended Answers

All 4 Replies

You're in the right direction.
One thing you should be carefull about is declaring an int (or some other var) and NOT giving it an initial value.
So change all your declarations to this:

int highest_pos = 0;
    int lowest_pos = INT_MAX;
    int counter = 0;
    int highest_neg = INT_MIN;
    int lowest_neg = 0;

INT_MAX and INT_MIN are defined in 'limits.h'. The reason why I use these is because if you should declare lowest_pos with '0' the first 'if' would be this: if ( x < 0 && x > 0) , which would never be true.

I guess this should solve most of your problems.

#include "stub.h"
int main()
{
   int x = 0;
   int highest_pos = 0;
   int lowest_pos;
   int counter;
   int highest_neg;
   int lowest_neg;

The values lowest_pos, counter, highest_neg, and lowest_neg start with unknown values. They could be 0, or 165432343, or -8554545487. You need to initialize them to some appropriate value.

Keep in mind if you initialize lowest_pos to 0 what will happen when you execute

if ( x < lowest_pos && x > 0)
    lowest_pos = x;

OK, I can get it to print everything right, except now I'm supposed to say "none" if there is no max/min negative values or no max/min positive values. I'm not sure how to get it to do that. Here's what I have:

#include "stub.h"
int main()
{
   int x = 0;
   int highest_pos = 0;
   int lowest_pos = INT_MAX;
   int counter = 0;
   int highest_neg = INT_MIN;
   int lowest_neg = 0;
   
   for(counter = 1; counter <= 5; counter ++)
   {
               cin >> x;
               
               if ( x > highest_pos)
                  highest_pos = x;
                  
               if ( x < lowest_pos && x > 0)
                  lowest_pos = x;
                  
               if ( x > highest_neg && x < 0)
                  highest_neg = x;
                  
               if ( x < lowest_neg)
                  lowest_neg = x;
                                                               
   }
   
   if(x > 0)
   cout << highest_pos << "     " << lowest_pos << "   " << endl << "None    " << "None" << endl;
   
   if(x < 0)
   cout << "None     " << "None     " << endl << highest_neg << "     " << lowest_neg << endl; 

   if(x > 0 && x <= 0)
   cout << highest_pos << "     " << lowest_pos << "   " << endl << highest_neg << "    " << lowest_neg << endl;
                  
                  cin.ignore(INT_MAX);

    getchar();
	return 0;
}

What values are the variables going to have if there were no numbers in the given range? Test for those values before you output the min/max.

Hint1: look at the special value you used.
Hint2: what does testing the last value entered (x) when you output your results do for you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.