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

1. ## 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. Originally Posted by finalreign
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.

3. Thank you, that helps a lot. I'll definitely try your advice on the indentations.

4. 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. Note that you can compute the maximum and minimum value at the same time.

Greets,
Philip

6. Originally Posted by Snafuist
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. 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.