Hey Guys and Gals,I'm writing a program to find the highest and lowest of 5 integers.I think i have the algorithm almost right but it doesn't work.I think its something simple.......as usual.........

include <iostream.h>
include <conio.h>

int main ()


{
       double total=0,num1,num2,num3,num4,num5,high,low;
       

       cout<<please enter number;
       cin>>num1;
       cout<<please enter number;
       cin>>num2;
       cout<<please enter number;
       cin>>num3;
       cout<<please enter number;
       cin>>num4;
       cout<<please enter number;
       cin>>num5;


      high=num1
      if (num1>high) high=num1;
      else if (num2>high) 
         {
          high=num2;
          }

Edited 6 Years Ago by Nick Evan: Added code-tags

Put some //code goes here tags around your source. Also, you can add to your old post you didn't have to start a new one.

Are you allowed to use an array for this? That's probably the easiest way. Create a temporary variable to hold the high and one to hold the low, set them to appropriate values. Walk along your array and check if the current number is higher than the highest you have(in your temporary variable), if so then replace your temporary variable with the current number which is now the highest.

int lowest;
int highest;
for loop
{
      if lowest > current
             lowest = current;
      if highest < current
              highest = current;
}

Also, use #include <iostream> instead of iostream.h

Edited 3 Years Ago by mike_2000_17: Fixed formatting

If you set the highest number to the first number, then why would you compare the first number to the highest to see if it's bigger?

high=num1
   if (num1>high) high=num1;

It's not wrong, it just doesn't make much sense. If the highest number so far is number 1 then start comparing with number 2.

The code looks fine. What's the problem that you're getting?

Also, like jonsca wrote, if you use an array/vector and a simple for loop then you'll save yourself a lot of typing. (If you've gotten that far in the language, if not then portion of the program that you posted looks like you're doing things right.)

Sorry made a few mistakes in the first post,have to find the highest and lowest of 5 integers,and i cant use arrays yet,when i run the program it doesn't display the right number.This is how i'm doing it

high=num1;                  
                  if (num2>high)
                         {                
                           high=num2;
                         }
                   else if(num3>high) 
                         {
                           high=num3;
                         }                              
                   else if (num4>high) 
                          {
                          high=num4;
                          }
                   else if (num5>high) 
                          {
                           high=num5; 
                          }          

cout<<:the"the highest number is "<<high;

OK, I see where the problem is. It's an easy one, but I missed it the first time I looked at the question.

if number1 > number2 then highest = number1.... and it stops checking right here. The moment one if statement is true then that's the end of the if/else if sequence and so it doesn't try to check past the first result that comes back true. You need to use individual if statements and not else if statements to make this work.

#include <iostream>
//include <conio.h>

using std::cout;
using std::cin;
using std::endl;

int main ()


{
	double total=0,num1,num2,num3,num4,num5,high,low;


	cout << "Please enter number: ";
	cin>>num1;
	cout << "Please enter number: ";
	cin>>num2;
	cout << "Please enter number: ";
	cin>>num3;
	cout << "Please enter number: ";
	cin>>num4;
	cout << "Please enter number: ";
	cin>>num5;

	high = num1;
	if (num2>high)
	{
		high=num2;
	}
	if(num3>high)
	{
		high=num3;
	}
	if (num4>high)
	{
		high=num4;
	}
	if (num5>high)
	{
		high=num5;
	}

	cout<< "The highest number is " << high;

	return 0;
}

Edited 6 Years Ago by necrolin: n/a

Thanks very much, thats what happened when i ran the program,it would always output num2

This article has been dead for over six months. Start a new discussion instead.