Thread: need help finding the max and min of a 2d array

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

    need help finding the max and min of a 2d array

    I'm having major problems figuring out this homework problem.

    Write a program that finds the maximum and minimum of elements of a two dimensional array using a function that has a 2D array in its parameter list.

    Any help would be greatly appreciated.

    insert
    Code:
    /* Chapter 9 Problem 9 */
    
    #include <stdio.h>
    
    
    double find_max(double [][5], int n);
    double find_min(double [][5], int n);
    
    int main(void)
    {
    int n;
    double a[][5] = {2, 7, 6, 8, 4};
    int max, min;
    find_max(a[][5], 2);
    find_min(a[][5], 2);
    
    printf("Max: %d\nMin: %d", max, min);
    
    return 0;
    }
    
    
    double find_max(double a[][5], int m)
    {
    int i,j;
    int max = a[0][0];
    
    for(i = 0; i < 2; i++)
    for(j = 0; j < 5; j++)
    if(a[i][j] > max)
    {
    max = a[i][j];
    }
    return max;
    }
    
    double min(double a[][5], int m)
    {
    int i,j;
    int min = a[0][0];
    
    for(i = 0; i < 2; i++)
    for(j = 0; j < 5; j++)
    if(a[i][j] < min)
    {
    min = a[i][j];
    }
    return min;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by finalreign View Post
    I'm having major problems figuring out this homework problem.

    Write a program that finds the maximum and minimum of elements of a two dimensional array using a function that has a 2D array in its parameter list.

    Any help would be greatly appreciated.

    insert
    Code:
    /* Chapter 9 Problem 9 */
    
    #include <stdio.h>
    
    
    int max(double [][5]);
    int min(double [][5]);
    
    int main(void)
    {
    
    int a[][] = {
    { 2, 7, 6, 8, 4},
    {3, 9, 1, 5, 6}
    };
    
    int max, min;
    
    max = max(a);
    min = min(a);
    
    printf("Max: %d\nMin: %d", max, min);
    
    return 0;
    }
    
    
    int max(int a[][5])
    {
       int i,j;
       int max = a[0][0];
    
       for(i = 0; i < 2; i++)
          for(j = 0; j < 5; j++)
             if(a[i][j] > max)
             {
                max = a[i][j];
             }
       return max;
    }
    
    double min(double a[][5], int m)
    {
    int i,j;
    int min = a[0][0];
    
    for(i = 0; i < 2; i++)
    for(j = 0; j < 5; j++)
    if(a[i][j] < min)
    {
    min = a[i][j];
    }
    return min;
    }
    Your input numbers are all integers, and I'd advise ALWAYS use int's instead of doubles, if possible. Doubles and floats bring trouble when making comparisons with ints, etc.

    Your indentation sucks, and will mislead your eye. Learn 2,3 or 4 space indentations, and use it consistently - and your eyes will become trained to spot code problem, much easier.

    I fixed up max() for you, you fix up min. This isn't the ONLY way to do this, it's just one good way. I didn't actually run the code I changed, so it may have slight errors in it. Treat it as good pseudo-code, and polish it up, as needed.
    Last edited by Adak; 04-28-2009 at 10:06 PM.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Thank you, that helps a lot. I'll definitely try your advice on the indentations.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good!

    Guess what I learned? max and min are reserved words for a macro on my compiler!

    So... let this be your example start:

    Code:
    #include <stdio.h>
    
    int maxx(int a[][5]);
    int minn(int a[][5]);
    
    int main(void)
    {
    
       int a[][5] = {
       { 2, 7, 6, 8, 4},
       {3, 9, 1, 5, 6}
       };
    
       int max1, min1;
       max1 = maxx(a);
       min1 = minn(a);
       printf("Max: %d\nMin: %d", max1, min1);
       max1 = getchar();
       return 0;
    }
    int maxx(int a[][5])
    {
       int i,j;
       int mymax = a[0][0];
    
       for(i = 0; i < 2; i++)
          for(j = 0; j < 5; j++)
             if(a[i][j] > mymax)
             {
                mymax = a[i][j];
             }
       return mymax;
    }
    Now you can polish off minn() in short order.

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Note that you can compute the maximum and minimum value at the same time.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by Snafuist View Post
    Note that you can compute the maximum and minimum value at the same time.

    Greets,
    Philip
    Just what I was going to write. One loop nested inside another and you're done. Unless you were specifically told to use seperate functions.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need two nested loops, just one loop:

    Code:
    void max_min(int *a, int n, int *minn, int *maxx)  {
       int i;
       for(i = 0; i < n; i++ )  {
          if(a[i] > maxx)
             maxx = array[i];
    
          //add the if statement for minn, here
       }
    }
    The earlier program showed a nice use for a return number from a function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Max and Min Value
    By will15 in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2006, 11:27 AM
  2. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  3. Passing a 2d array by Reference
    By loko in forum C Programming
    Replies: 8
    Last Post: 07-23-2005, 06:19 AM
  4. 2D array sorting from min. to max.
    By khaled_helmy in forum C Programming
    Replies: 1
    Last Post: 10-14-2004, 02:17 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM