Thread: 2-D array question

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    2-D array question

    I have written a program that reads in data from a file into a 5X6 array and then find the minimum and maximum values in the array. I also need to determine which row the minimum and maximum values are in. For example(3X3 array):

    Row
    1 2.1 4.5 2.3
    2 1.1 0.5 5.0
    3 2.4 4.4 6.4

    0.5 is the smallest value and 6.4 is the largest value and the output needs to look like this:

    Minimum = 0.5 in row2
    Maximum = 6.4 in row 3


    My program can read in the data and find the max and min values I just don't know how to have it read which row those values and could use some help on donig that.

    thanks
    Last edited by NLH; 04-21-2009 at 04:34 PM. Reason: not finished

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Keep track of the row you find the largest value in, the same way you actually keep track of the largest value. Simply make a variable and store the value of the row in it, updating it whenever you find a bigger value.


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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Yea I think I tried something like that. To find the max and min values I initialized the value of the array and used for loops and so I set up variables called min_row and max_row and used them as follows to find the row they were in but it didn't work. It gave me the final number that i is equal to the final value for i, so if rows = 4 min_row and max_row= 3.

    Code:
    minimum = data[0][0]
    maximum = data[0][0]
    
    for( i=0; i <= rows - 1; i ++)
    {
            for ( j = 0; j <= columns - 1; j++)
            {
                   if( data[i][j] < minimum)
                        minimum = data[i][j];
                        min_row = i
                   if( data[i][j] > maximum)
                        maximum = data[i][j];
                        max_row = i
             }
    }
    I tried a few other things but i continued to get the same output.
    Any suggestions?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    if statements bind to the nearest statement, unless enclosed in blocks. This:
    Code:
    if( x == y )
        printf("x == y");
        printf("\n...\n" );
    is not the same as:
    Code:
    if( x == y )
    {
        printf("x == y" );
        printf("\n...\n" );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Wow that was simple but it works! Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM