Thread: Searching array

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    4

    Searching array

    A

    Code:
        int array[2][4]; //assume that I already have values in the array here
        int i;    
        int j;
        int max=data[0][0];
        int min=data[0][0];
        int locationmax=0;
        int locationmin=0;
    
        for(i = 0; i < N; i++)
            for(j=0;j<4;j++)
                if(j==2&&array[i][j]>max)
                {
                    max=array[i][j];
                    locationmax=array[i][1];
                }
                
        for(i = 0; i < N; i++)
            for(j=0;j<4;j++)
                if(j==2&&array[i][j]<min)
                {
                    min=array[i][j];
                    locationmin=array[i][1];
    printf("Max is %d number is%d\n",max,locationmax);
        printf("Minimum is %d number is %d\n\n",min,locationmin);
                }
    Can somebody tell me why it work's with finding the max value, but not with the min value. My program saves 0 as min and 1 as locationmin?
    Last edited by dnopas; 10-08-2015 at 01:34 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You need to initialize the values in your array, right now it is filled with random values left in memory.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    4
    Quote Originally Posted by camel-man View Post
    You need to initialize the values in your array, right now it is filled with random values left in memory.
    This is simplified so you could focus on the actual code, put whatever values you want in the arrays. In my code i have a scanf that puts values in the array, and this code will only work as intended with max and maxlocation, the min and minlocation will display 1 and 0, and I can't see whats going wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching array help !!
    By aloushi in forum C Programming
    Replies: 15
    Last Post: 01-12-2014, 07:29 AM
  2. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  3. Help with my searching my array
    By snooki in forum C++ Programming
    Replies: 8
    Last Post: 03-25-2010, 05:18 PM
  4. searching an array
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2002, 10:26 AM
  5. Searching Array
    By DISGUISED in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2002, 03:21 PM