Thread: Problem with looking for MIN,MAX in Array

  1. #1
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24

    Problem with looking for MIN,MAX in Array

    I have a code . Looking for Min,Max in a Array . I can find Min in array
    but Max display garbage. Please tell me why .

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        int number[25];
        int i,j,n,min,max;
    
        printf("Input total object in array: ");
        scanf("%d",&n);
    
        printf("Please input each object:");
        for(i=0;i<n;i++)
        {
            printf("Object %d \n:",i+1);
            scanf("%d",&number[i]);
        }
    
         for(i=0;i<n;i++)
        {
            if(number[i]<min)
            min=number[i];
        }
        printf("\n Min number in array is: %d",min);
    
        for(j=0;j<n-1;j++)
        {
            if(number[j]>max)
            max=number[j];
        }
        printf("\n Max number in array is : : %d",max);
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are using both min and max in an uninitialzed state...
    Ask what are their values right before the start of their respective loops?

    Also it wouldn't be much of a problem to combine those two loops...

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your code never initialises max and min before using them. Accessing the value of any uninitialised variable gives undefined behaviour.

    The lines
    Code:
        if (number[i] < min)
    and
    Code:
        if (number[j] > max)
    both give undefined behaviour, because both max and min are uninitialised the first time through the respective loop, and these statements access their values.

    You just got lucky in having your calculation of min appearing to be correct. The calculation is still wrong.

    Upshot: you need to initialise max and min before using them.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Thanks a lot CommonTater, vs Grumpy . I was correct it and it don't have problem . But i till don't know when we must initialize value of variable .
    This is my code :
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        int number[25];
        int i,n;
        int min;
        int max;
    
        printf("Input total object in array: ");
        scanf("%d",&n);
    
        printf("Please input each object:");
        for(i=0;i<n;i++)
        {
            printf("\nObject %d:",i+1);
            scanf("%d",&number[i]);
        }
         max = number[0];
         min = number[0];
         for(i=0;i<n;i++)
        {
            if(number[i]<min) min=number[i];
            if(number[i]>max) max=number[i];
        }
        printf("\n Min number in array is: %d",min);
        printf("\n Max number in array is : : %d",max);
        return 0;
    }
    Don't need two the loops.
    Last edited by ToNy_; 12-17-2011 at 10:13 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ToNy_ View Post
    Thanks a lot CommonTater, vs Grumpy . I was correct it and it don't have problem . But i till don't know when we must initialize value of variable .
    That's easy .... Always.

    We have to because C doesn't.

  6. #6
    Registered User ToNy_'s Avatar
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    That's easy .... Always.

    We have to because C doesn't.
    Ok . I'm will do it.
    Start from beginning .
    Your life is what you do daily and weekly .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By JayCee++ in forum C++ Programming
    Replies: 8
    Last Post: 10-04-2011, 11:28 AM
  2. Problem With Array
    By mdthejericho in forum C Programming
    Replies: 17
    Last Post: 10-25-2010, 06:45 PM
  3. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  4. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  5. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM