HOW TO DISPLAY THE HIGHEST LOWEST AND MIDDLE VALUE?

im really new with c++...please help me with this problem..
"write a program that need 3 input numbers and after accepting the 3 numbers the program will display the highest , middle and the lowest value.

il try to make the program..but my prof tell me that it is wrong..

here it is..

#include <iostream.h>
main()
{
int H,M,L,num,i;

for (i=1;i<=3;i++){
cout << "enter number" << i <<";
cin >> num;
if (i==1){L=num; H=num; M=num}
if (num>H)H=num;
if (num<L)L=num;
if (num !=H || !=L) m=num;

cout<<"H=="<<H;
cout<<"L=="<<L;
cout<<"M=="<<M;
return 0;
}


please help me how to correct or make that program..tnx

Hello,

your professor is right.

Please use the code tag so that the lines are numbered.

Cour cout line for enteringe the number seems odd. Cut the <<" .

Your logic is good, at first sight at least.
But think about the following: What happens if the second number that is entered is larger than the first one? H will be overwritten with the new value and the first value will be lost.

Let the user enter the three numbers.
Then you sort the numbers with a sorting algorithm. For three numbers, it is quite simple.

in pseudo-code (v1,v2,v3 are the three value):
if v3 < v2 then switch the two
if v1 < v2 then swith the two.
if v3 < v2 then switch the two again.

That would be the sorting.

If you do not know how to switch:
v3 = temp
v3 = v2
v2 = temp

I hope, that helps

int main

1. we need to store (hold) three numbers so define a variable for that
may be an array is better
int Numbers[3];
2. Now tell the user to enter three numbers, and fill it in the array
3. Iterate through the array and find out which is the largest,smallest

You have a few mistakes in there:
1. The main is a function and it has to have a return type, usually int int main(){

2. You open the brackets for the for loop but you forgot to close them after the cin >>num.

3. cout << "enter number" << i <<"; you open double quotes at the end but you never close them.

4. if (i==1){L=num; H=num; M=num} M=num is still a command that needs to be terminated by semicolon before the closing bracket "}"

5. if (num !=H || !=L) In c++ you can't use this syntax. You have to use again the "num!=L" if (num != H || num != L)

6. The cout and cin commands are part of the namespace std. To use them you have to use the namespace notation. You can do that by simply adding using namespace std; under your #include. Also The include doesn't need the ".h" at the end for most of the c++ standard libraries.

[EDIT] Sorry, it took me way to long to write this answer and then I saw the above :-)
Last edited on
tnx guys... love lots
Also

 
if(num != H || num != L)


Will return true if it is not equal to either of them, but will also return true if num == H or num == L. Meaning it will always return true unless num is equal to H and L. I don't think that's what you want so use &&
Topic archived. No new replies allowed.