Help with if statement program plz

This is a discussion on Help with if statement program plz within the C Programming forums, part of the General Programming Boards category; Hi, I'm having difficulty writing a c program that asks the user to input 4 integers and it find the ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    7

    Help with if statement program plz

    Hi,
    I'm having difficulty writing a c program that asks the user to input 4 integers and it find the smallest and largest integer. I can only use 4 "if" statements to complete the program. I know the program does not compute the smallest integer correctly, but how can i fix it ? This is what I have so far..

    Code:
    #include <stdio.h>
    
    int main()
    {
      int num1, num2, num3, num4;
      int smallest, largest;
      
      printf("Enter four integers: ");
      scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
      
     smallest = num1;
    
      if (num1 >= num2)
        if (num1 >= num3)
          if (num1 >= num4)
             largest = num1;
          else 
             largest = num4;
        else if (num4 >= num3)
          largest = num4;
        else 
          largest = num3;
      else 
        largest = num2;
    
    printf("Largest: %d\n", largest);
    printf("Smallest: %d\n", smallest);
    
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Have you encountered arrays yet? Do you really need to keep track of each number, or just the big and small? Have you used loops yet?
    Code:
    smallest = big number
    biggest = small number
    
    repeat
        read a number
        if number < smallest
            smallest = number
        if number > biggest
            biggest = number
    Set your repeat condition depending on how many times you want to repeat, or until you enter something that means 'quit looping'.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    47
    Ya, just set your biggest number to 0, loop through all numbers saying if this number is bigger, set it to your current number. Than you only need one if statement (two if you do the smallest number as well).

    Code:
    num = 0;
    for(i=0;i<4;i++)
    {
       if ( num[i] > num )
          num =  num[i];
    }
    And essentially do the opposite for the smallest number...

    This will set num to the biggest number, because the only time it's overwritten is if a number is larger and vice versa. you will have to switch your implementation to an array.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    We prefer not to give out solutions, especially in code, especially since Quzah just posted the pseudo code for that snippet you posted...
    Leave some practice for the OP.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice program, plz help
    By Johnta in forum C Programming
    Replies: 12
    Last Post: 02-22-2008, 12:53 PM
  2. Program not seeing the if statement
    By adjac in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2004, 08:04 PM
  3. help with program using switch statement
    By Aysha in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2002, 04:43 PM
  4. Can someone help me plz
    By Shadow in forum C Programming
    Replies: 5
    Last Post: 02-15-2002, 12:41 PM
  5. plz hlp me. program not running properly
    By jfl in forum C Programming
    Replies: 5
    Last Post: 02-11-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21