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;
          }

Dani AI

Generated

Nice start, . The main logic issue (as spotted) is the else-if chain. Once one condition is true, the rest are skipped, so you never compare against all five values. Also watch for a few compile problems in your original snippet: missing # in #include, missing quotes around prompts, a missing semicolon after high=num1, and using double when the assignment says integers.

You do not need arrays to solve this cleanly. Read each value one at a time and update both highest and lowest as you go. Initialize both to the first number you read, then use plain if statements for the rest. This processes all five numbers and only stores two extremes.

#include <iostream>

int main() {
    std::cout << "Enter five integers:\n";

    int hi = 0, lo = 0, x = 0;
    for (int i = 0; i < 5; ++i) {
        std::cin >> x;
        if (i == 0) {              // seed both with the first input
            hi = lo = x;
        } else {
            if (x > hi) hi = x;    // no else-if here
            if (x < lo) lo = x;    // check both directions
        }
    }

    std::cout << "Highest: " << hi << "  Lowest: " << lo << "\n";
    return 0;
}

If your instructor insists on five separate variables instead of a loop, the same rule applies: set hi and lo from num1, then for each of num2..num5 do two independent if checks (if (num2 > hi) hi = num2; and if (num2 < lo) lo = num2;). That mirrors ’s suggestion about walking the data and keeps the comparisons correct without arrays.

Recommended Answers

All 5 Replies

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

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;
}

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

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.