Thread: Finding the max in row

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    8

    Finding the max in row

    Hi
    i want to find the max vaule in each row of my 2d array so if i have something like this :
    45 45 32 2345
    45 25 454 45
    32 437 45 45
    454 45 45 45
    i would like the output to be the following :

    day 1: 2345
    day 2: 454
    day 3 : 437
    day 4 : 454

    With my code bellow i am getting...
    day 1: 2345
    day 2 : 2345
    day 3 : 2345
    day 4 : 2345

    So it finds the maximum and if the other maximums from other rows are smaller it prints the maximum from the row before.. if that makes sense:

    Code:
    int LargestDay() {
         int d,w,q=0;
         int largeD[i];
      printf("The largest number of phones sold on a given day:\n");
         for(d=0;d<i;d++)
             {
             for(w=0;w<=j;w++)
                 {
                 if( array[d][w]>q)
                 q=array[d][w];
                 }
             printf("\t Day %d is %d\n",d+1,q);
             }
             printf ("\n");
         }
    i and j are global
    thanks for your help
    Last edited by qweqwe; 12-04-2011 at 07:17 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So you declare an int variable, and on the inner loop (which has all the column logic), you set this variable (I'll call it maxRow), to the first value in the row, no matter what that value is.

    As each value is scanned for processing, (printing, whatever), you check if it is > maxRow. If it is, you assign that value to maxRow.

    Print it out, after the column (inner) for loop has finished, and there you go.


  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    isn't that what i did with q?
    or am i missing something from what you said?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by qweqwe View Post
    isn't that what i did with q?
    or am i missing something from what you said?
    That is what you did with q ... but q (and any single letter name) is just a suck-butt variable name... use something more descriptive like "highest" or "rowmax" etc. You will find that variable names that represent their purpose make your code MUCH easier to follow...

    Also you should avoid using global variables as much as possible. Way too much to go wrong with that...

    What Adak is suggesting and I will second is that you aren't correctly initializing your variables...

    Code:
         for(d=0;d<i;d++)
             {
          
            q = array[d][0];
    
            for(w=0;w<=j;w++)
                 {
                 if( array[d][w]>q)
                 q=array[d][w];
                 }
             printf("\t Day %d is %d\n",d+1,q);
             }
             printf ("\n");
         }
    Last edited by CommonTater; 12-04-2011 at 08:11 PM.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Ohh i see, yep that's sorted it out thanks for that.

    Variables do need better names i guess its fine for me but if i need help or someone looks at it they won't have a clue.

    Thanks again

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by qweqwe View Post
    Ohh i see, yep that's sorted it out thanks for that.

    Variables do need better names i guess its fine for me but if i need help or someone looks at it they won't have a clue.

    Thanks again
    And, trust me on this because I speak frome bad experience here... if you don't name things in a meaningful way and add comments for at least the non-obvious stuff, you will come back to it in 5 years and YOU won't understand it either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GPA and finding a job
    By camel-man in forum General Discussions
    Replies: 3
    Last Post: 10-17-2011, 06:18 AM
  2. Finding OS
    By Chandana in forum C Programming
    Replies: 13
    Last Post: 08-08-2009, 03:01 PM
  3. Finding Max and Min Value
    By will15 in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2006, 11:27 AM
  4. Finding a Key - C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-30-2001, 07:19 AM
  5. Finding GCF
    By cpp4ever in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2001, 04:26 PM