Rytis - 6 months ago 27
C Question

# C language. How to find the maximum minimum. (2D arrays)

I have written code that allows you to enter one dimension of a NxN double array. It will then print random numbers in a 2D array and it finds the maximum and minimum number of each row. It then prints them and their coordinates (row and column).

ATTENTION!!!!
I have altered my code in such a way that it finds the minimum number of the maximum. I now don't know how to find it's coordinates

My code is as follows:

``````int N, i, j, min=1000, max, m , o;
time_t t;
int masyvas[100][100], minmax[100];
printf("Enter one dimension of a NxN array\n");
scanf("%d", &N);

srand((unsigned) time(&t));
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
masyvas[i][j] = rand() % 10;
printf("%4d", masyvas[i][j]);

}
printf("\n");
}

int k, l, idkeymax, idkeymin;
for(k=0; k<N; k++)
{

max=-1000;
for(l=0; l<N; l++)
{
if(max<masyvas[k][l])
{
max=masyvas[k][l];

}

}
minmax[k]=max;

}
for(m=0; m<N; m++)
{if(minmax[m]<min)
min=minmax[m];
}

printf("maziausias skaicius tarp didziausiu yra %d eiluteje %d stulpelyje %d\n",min);
``````

Here's the pseudo code of what you need to do.

``````for row in grid {
row_max = max_in_row(row)
grid_min = min(grid_min, row_max)
}
``````

Step one is to write a routine that finds the max and location in a list. You could do this as one big function, but it's much easier to understand and debug in pieces.

You also need the index where it was found. Since C can't return multiple values, we'll need a struct to store the number/index pair. Any time you make a struct, make routines to create and destroy it. It might seem like overkill for something as trivial as this, but it will make your code much easier to understand and debug.

``````typedef struct {
int num;
size_t idx;
} Int_Location_t;

static Int_Location_t* Int_Location_new() {
return calloc(1, sizeof(Int_Location_t));
}

static void Int_Location_destroy( Int_Location_t* loc ) {
free(loc);
}
``````

Now we can make a little function that finds the max number and position in a row.

``````static Int_Location_t* max_in_row(int *row, size_t num_rows) {
Int_Location_t *loc = Int_Location_new();

loc->num = row[0];
loc->idx = 0;

/* Compare starting with the second element */
for( size_t i = 1; i < num_rows; i++ ) {
if( row[i] > loc->num ) {
loc->num = row[i];
loc->idx = i;
}
}

return loc;
}
``````

Rather than starting with some arbitrary max or min, I've used an alternative technique where I set the max to be the first element and then start checking from the second one.

Now that I have a function to find the max in a row, I can now loop over it, get the max of each row, and compare it with the minimum for the whole table.

``````int main() {
int grid[3][3] = {
{10, 12, 15},
{-50, -15, -10},
{1,2,3}
};

int min = INT_MAX;
size_t row = 0;
size_t col = 0;

for( size_t i = 0; i < 3; i++ ) {
Int_Location_t *max = max_in_row(grid[i], 3);
printf("max for row %zu is %d at %zu\n", i, max->num, max->idx);

if( max->num < min ) {
min = max->num;
col = max->idx;
row = i;
}

Int_Location_destroy(max);
}

printf("min for the grid is %d at row %zu, col %zu\n", min, row, col);
}
``````

I used a different technique for initializing the minimum location, because getting the first maximum would require repeating some code in the loop. Instead I set `min` to the lowest possible integer, `INT_MAX` from limits.h which is highest possible integers. This allows the code to be used with any range of integers, there are no restrictions. This is a very common technique when working with min/max algorithms.