#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1

    Using 'if' statements


    I am trying to write a program that finds the largest and smallest of four integers entered by the user. For example:
    Code:
    Enter 4 integers: 4 17 9 32
    Largest integer: 32
    Smallest integer: 4
    The challenge is to use the 'if' statement for maximum 4 times. Help me if you can. Thanks in advance :)
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Show us what you have done so far.

    You've been on this ride before, so you already know the rules.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    40
    Rep Power
    18
    Originally Posted by arman.khandaker
    I am trying to write a program that finds the largest and smallest of four integers entered by the user. For example:
    Code:
    Enter 4 integers: 4 17 9 32
    Largest integer: 32
    Smallest integer: 4
    The challenge is to use the 'if' statement for maximum 4 times. Help me if you can. Thanks in advance :)
    More challenge if you use 'if' 0 time ;)

    Code:
    #include<stdio.h>
    void main(){
    	int i=1,x,y,z[4];
    	for(printf("Enter 4 integers: "),scanf("%d %d %d %d", &z[0],&z[1],&z[2],&z[3]),
    		x=y=z[0];i<4;x=(z[i]>x)?z[i]:x,y=(z[i]<y)?z[i]:y,i++);
    	printf("Largest integer: %d\nSmallest integer: %d\n",x,y);
    }

    Comments on this post

    • DaWei_M disagrees : Don't use "void main."
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Really? Have you tried yet - this is a really simple algorithm.

    Clue:

    if a is bigger than my biggest result so far, then my biggest result so far is a.

    That's one if and one value. You have 4 values and can use 4 ifs. It is so trivial that I have probably already said too much.

    Note that you need to initialise the biggest so far to the smallest possible integer - and that is not zero BTW.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    the code could be like this! the trick is in green color, initialize it outside the loop :D

    Code:
    int main() 
    {
    int a[4],i,max,min;     
    scanf("%d %d %d %d", &a[0],&a[1],&a[2],&a[3]);
    max=min=a[0]; 	
    /* Edited out solution - please don't hand out complete answers */
    printf("max:%d\nmin:%d\n", max, min); 	
    return 0; 
    }
    Last edited by Scorpions4ever; June 25th, 2013 at 04:38 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    Okay thanks guys. I was actually looking for a way to do it without using arrays. :)

    Comments on this post

    • clifford disagrees : And we were actually looking for you to make an attempt. The example showing an array does so only for convenience; you could use four separate variables if you wish.

IMN logo majestic logo threadwatch logo seochat tools logo