What's my problem?

I'm going to print the highest and the lowest among 3 numbers.

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
#include<stdio.h>
int main()
{
int num1,num2,num3,num4;
int lowest, highest;

printf("First input: ");
scanf("%d", &num1);
printf("Second input: ");
scanf("%d" ,&num2);
printf("Third input: ");
scanf("%d, &num3);

lowest=num1;
if(lowest > num2)
{
lowest=num2;
}
else if (lowest>num3)
{
lowest=num3;
printf("The lowest number is: %d\n" ,lowest);

}

highest=num1;
if(highest < num2)
{
highest=num2;
}
else if (highest<num3)
{
highest=num3;
printf("The highest number is: %d\n",highest);

}
return(0);
} 
You're missing a close quote on line 12, as the syntax highlighting clearly shows.

EDIT: also your logic is not quite right. If num2 is less than num1 and num3 is less than num2 (example: 5, 3, 1), then your code will record num2 (3) as the lowest value.

Also, you will only print the output if num3 is determined to be the highest/lowest value. If it turns out to be num1 or num2, you won't print anything.
Last edited on
Thank you know here's my code
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
#include<stdio.h>
int main()
{
int num1,num2,num3;
int lowest, highest;
 
 
scanf("%d", &num1);
 
scanf("%d" ,&num2);
 
scanf("%d", &num3);
 
lowest=num1;
if(lowest > num2)
{
lowest=num2;
}
else if (lowest>num3)
{
lowest=num3;
 
}
printf("The lowest number is: %d\n" ,lowest);
 
int high1=num1;
int high2=num2;
int high3=num3;
 
highest=high1;
if(highest < high2)
{
highest=high2;
}
else if (highest<high3)
{
highest=high3;
}
printf("The highest number is: %d\n",highest);
return(0);
} 
Better, but you still have the problem with it favoring num2 over num3.

Take a look at lines 14 - 23:


1
2
3
4
5
6
7
8
9
lowest=num1;
if(lowest > num2)
{
lowest=num2;
}
else if (lowest>num3)
{
lowest=num3;
}


Plug in these numbers:
num1 = 10
num2 = 5
num3 = 2

Step through the logic of what the code is doing. Your program will report '5' as the lowest number when the correct answer is '2'.

Hint: Remember what else means.
help me please what's the correct code for it.
Remember that an else clause will only execute if the preceeding if statement was false.

For example:

1
2
3
4
5
6
7
8
9
10
int foo = 5;

if(foo == 3)
{
  cout << "this will NOT print";
}
else
{
  cout << "this WILL print";
}


Similarly:

1
2
3
4
5
6
7
8
9
10
int foo = 5;

if(foo == 5)
{
  cout << "this WILL print";
}
else
{
  cout << "this will NOT print";
}


That said... you are doing an else if statement, which is the same conceptually as what's illustrated below:

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
// This code...
lowest=num1;
if(lowest > num2)
{
    lowest=num2;
}
else if (lowest>num3)
{
    lowest=num3;
}


// is the same as this code...
lowest=num1;
if(lowest > num2)
{
    lowest=num2;
}
else
{
    if (lowest>num3)
    {
        lowest=num3;
    }
}


Notice how (due to the else clause) the check against num3 will only happen if the check against num2 came back as false. If it comes back as true, the num3 check will be skipped.
Topic archived. No new replies allowed.