Thread: Largest Number

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    19

    Largest Number

    Hi,

    I'm required to write a program that prints the highest valued integer, from 3 entered values. Here is what i've got so far:

    Code:
    /* Week 5 Task*/
    /* By Luke Sowersby */
    
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int i1, i2, i3;
    
    int highest(int i1, int i2, int i3)
    {
        int high;
    
        if(i1>i2>i3)
        printf("%d",i1);
    
        if(i1>i3>i2)
        printf("%d",i1);
    
        if(i2>i1>i3)
        printf("%d",i2);
    
        if(i2>i3>i1)
        printf("%d",i2);
    
        if(i3>i2>i1)
        printf("%d",i3);
    
        if(i3>i1>i2)
        printf("%d",i3);
    
        return high;
    }
    
    int main(void)
    {
    
    printf("Enter number 1: ");
    scanf("%d",&i1);
    printf("Enter number 2: ");
    scanf("%d",&i2);
    printf("Enter number 3: ");
    scanf("%d",&i3);
    
    int highest(int i1, int i2, int i3);
    
    printf("Order: %d",highest(i1, i2, i3));
    
    }
    When printing the final result, it prints a 7 digit number.

    Any help please?

  2. #2
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    In function highest, where do you set the largest number in retuned (high) variable?

    Also, why do you have the declaration of highest inside main function?

    You should also check the return value from scanf(), it may be the user decides to give your program something else than numeric values, and it is generally thought to be bad practice to allow such things to go unnoticed...

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The > operator does not do exactly what you think it does.

    x > y > z is parsed as (x > y) > z. The > operator yields either a 1 or a 0, meaning true or false; this 1 or 0 is then compared to z.

    In order to do the test, you'd want to say "if x is greater than y AND y is greater than z" (I assume this is what you want). So:
    Code:
    if(x > y && y > z)

  4. #4
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    The > operator appears 12 times throughout your code, although you need it only 2 times. Think about how you would compare three numbers in real life: you'd probably take two, compare them, and then compare the result with the third.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM