Thread: two dimensional array help

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    7

    Question two dimensional array help

    Hi all hope someone can give me a few pointers.
    This is what I want to achieve but don't understand.

    I want to return the two dimentinal array position of the largest value in a sequence of numbers.

    So here is my code so far which does not return the correct results.

    Code:
    #include <stdio.h>		//preprocessor directive to include stdio
    #define NUMROWS 3		//preprocessor defines
    #define NUMCOLS 3
    
    int main()			//ep to main
    {
    
    int i, j;			//for loop vars
    int val[NUMROWS][NUMCOLS] = {1,2,3,4,5,6,7,8,9};
    int maxnumpos = val[0][0];
    
    for(i = 0; i < NUMROWS; i++)	//start outer loop
    	{
    		for(j = 0; j < NUMCOLS; j++)	
    			if(val[i][j] > maxnumpos)
    				maxnumpos = val[i][j];		
    	}
    
    printf("the maximum value in the array is %d & is stored at postion %d %d\n", maxnumpos, val[i][j]);
    
    }
    Can anyone give me some pointers to begin to understand how to deal with this scenario, whether it is code examples or a text to read

    thanks in advance for your help

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you want to keep the position of the maximum value, you'll have to make a copy of where it is.
    Code:
    #include <stdio.h>		//preprocessor directive to include stdio
    #define NUMROWS 3		//preprocessor defines
    #define NUMCOLS 3
    
    int main()        //ep to main
    {
    
       int i, j;         //for loop vars
       int val[NUMROWS][NUMCOLS] = {1,2,3,4,5,6,7,8,9};
       int maxnumpos = val[0][0];
       int maxi = 0, maxj = 0;
    
       for ( i = 0; i < NUMROWS; i++ )  //start outer loop
       {
          for ( j = 0; j < NUMCOLS; j++ )
             if ( val[i][j] > maxnumpos )
             {
                maxnumpos = val[i][j];
                maxi = i;
                maxj = j;
             }
       }
    
       printf("the maximum value in the array is %d & is stored at postion %d %d\n", 
              maxnumpos, maxi, maxj);
    
    }
    
    /* my output
    the maximum value in the array is 9 & is stored at postion 2 2
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    7

    Talking appreciation

    many thanks I see it now.
    My first post got me an answer with my first reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM