# What's wrong with this program (a beginner's question)

• 02-20-2009
maccat
What's wrong with this program (a beginner's question)
Hi, I'm learning C by self-study from King's book. I'm now on Chapter 5 re. "if statements" and I'm stuck on one programming project from this chapter. I need to find the largest and smallest values of 4 integers entered by the user, using as few if statements as possible. In fact, it hints that 4 statements should be enough for the purpose. I wrote this program below. I sense something is wrong (logic error), but I still can't figure out how to fix it/correct it. Could you please help? 4 if statements only...

Code:

```// Programming Project 5.7 #include<stdio.h> int main(void) {         int a = 0, b = 0, c = 0, d = 0;         int max = 0, min = 0;                 printf("Enter 4 integers: ");         scanf("%d%d%d%d", &a, &b, &c, &d);                         if(a > b && b > c && c > d) {                 max = a;                 min = d;         }         else if (b > c && c > d && d > a) {                 max = b;                 min = a;         }         else if(c > d && d > a && a > b) {                 max = c;                 min = b;         }         else if(d > a && a > b && b > c) {                 max = d;                 min = c;         }         printf("Largest: %d\nSmallest: %d\n", max, min);         return 0; }```
• 02-20-2009
tabstop
I don't have King's book, but I would suggest that they might not mean four if-statements total, but that no matter what path through the program you take you will meet four if-statements, i.e., something like
Code:

```if (first) {     //go through three if statements in a similar way } else {     //go through three possibly different if statements }```
• 02-20-2009
t014y
the best i can think of is to have 4 if cases to find the max then inside of each if statement have 3 more to find the min. i don't know if that counts as 4 or 16 though. there are only 4 if statements but each if statement has if statements.

its totally possible that i am way off to. any that is what i have come up with.