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

  1. #1
    Registered User
    Join Date
    Feb 2009

    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...

    // Programming Project 5.7
    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;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    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
    if (first) {
         //go through three if statements in a similar way
    } else {
         //go through three possibly different if statements

  3. #3
    Registered User
    Join Date
    Sep 2008
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  2. Question about K&R program
    By Aerie in forum C Programming
    Replies: 15
    Last Post: 04-24-2005, 07:09 AM
  3. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM
  4. Program Has Stuff Wrong Wit It
    By oobootsy1 in forum C++ Programming
    Replies: 5
    Last Post: 08-12-2003, 08:38 PM
  5. God
    By datainjector in forum A Brief History of
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM