Solved

maximum value(location) in array

Posted on 2006-11-09
5
251 Views
Last Modified: 2010-04-15
hi,
I have a program like below how can I modify it to let it return me the location of maximum value

for example:
row 0 has maximum 4 and loacted in t[0][0]
row 0 has maximum 4 and loacted in t[0][1]
row 1 has maximum 8 and loacted in t[1][0]
row 2 has maximum 4 and loacted in t[2][2]


----------code------------------------
#include <stdio.h>
#include <stdlib.h>
#include <float.h>

                                 
int t [3][3]={{4,4,2},
              {8,2,1},
              {3,3,4}
              };                    
                   
main (){
   
   int i,j, max, allmax;
   allmax=0;
   
   for (i=0; i<3 ; i++)
   {
    max = 0;
      for (j=0; j<3; j++)
      {
        if (t[i][j]>max)
        {
          max=t[i][j];
         }
         
        if (t[i][j]>allmax)
        {
          allmax=t[i][j];
        }
      }
     
      printf("row[%d] has maximun %d and loacte in t[][]: \n",i,max );
   }

printf("allmax is : %d\n", allmax );
system("pause");
return 0;

}
0
Comment
Question by:rmtogether
  • 3
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17908150
Similar to your last Q, you are almost there - make that

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
                                 
int t [3][3]={  {3,3,4},
                {8,2,1},
                {3,3,4}
              };                    
                   

main (){
   
   int i,j, max, allmax;

   allmax = 0;
   for (i=0; i<3 ; i++){
      max = 0;
      for (j=0; j<3; j++){
        if (t[i][j] > max) {
          max=t[i][j];
        }
        if (t[i][j] > allmax) {
          allmax=t[i][j];
        }
      }
      printf("max vaule is : %d\n",max );
   }

   printf("max vaule overall is : %d\n",allmax );


system("pause");
return 0;

}
0
 
LVL 86

Expert Comment

by:jkr
ID: 17908169
Sorry, missed the 'location part:

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
                                 
int t [3][3]={  {3,3,4},
                {8,2,1},
                {3,3,4}
              };                    
                   

main (){
   
   int i,j, max, allmax, alli = 0, allj = 0;

   allmax = 0;
   for (i=0; i<3 ; i++){
      max = 0;
      for (j=0; j<3; j++){
        if (t[i][j] > max) {
          max=t[i][j];
        }
        if (t[i][j] > allmax) {
          allmax=t[i][j];
          alli = i;
          allj = j;
        }
      }
      printf("max vaule is : %d\n",max );
   }

   printf("max vaule overall is : %d at %d/%d\n",allmax, alli, allj );


system("pause");
return 0;

}
0
 

Author Comment

by:rmtogether
ID: 17908261

how can I get the maximum value for each row as well
the final output look like


row 0 has maximum 4 and loacted in t[0][0]
row 0 has maximum 4 and loacted in t[0][1]
row 1 has maximum 8 and loacted in t[1][0]
row 2 has maximum 4 and loacted in t[2][2]

max over over is 8 is in t[1][0]
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17908289
Just add that part from your code again, e.g.

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
                                 
int t [3][3]={  {3,3,4},
                {8,2,1},
                {3,3,4}
              };                    
                   

main (){
   
   int i,j, max, allmax, alli = 0, allj = 0, rowj = 0;

   allmax = 0;
   for (i=0; i<3 ; i++){
      max = 0;
      for (j=0; j<3; j++){
        if (t[i][j] > max) {
          max=t[i][j];
          rowj = j;
        }
        if (t[i][j] > allmax) {
          allmax=t[i][j];
          alli = i;
          allj = j;
        }
      }
      printf("max vaule is : %d at %d\n",max,rowj );
   }

   printf("max vaule overall is : %d at %d/%d\n",allmax, alli, allj );


system("pause");
return 0;

}
0
 
LVL 84

Expert Comment

by:ozo
ID: 17910172
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
                                 
int t [3][3]={  {4,4,2},
                {8,2,1},
                {3,3,4}
              };                    
                   

main (){
   
   int i,j, max, allmax, alli = 0, allj = 0, rowj = 0;

   allmax = 0;
   for (i=0; i<3 ; i++){
      max = 0;
      for (j=0; j<3; j++){
        if (t[i][j] > max) {
          max=t[i][j];
          rowj = j;
        }
        if (t[i][j] > allmax) {
          allmax=t[i][j];
          alli = i;
          allj = j;
        }
      }
      for (j=0; j<3; j++){
        if( t[i][j] == max ){
         printf("row %d has maximum %d and loacted in t[%d][%d]\n",i,max,i,j );
        }
      }
   }

   printf("max vaule overall is : %d at %d/%d\n",allmax, alli, allj );


system("pause");
return 0;

}
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 18
Where is my core dump file in Ubuntu? 12 429
Trouble linking program with -lcrypt 3 134
Need example 5 101
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now