Question

This is a function prototype to take in a 2D array, specific row, specific column, and size of the array (size is both the amount of rows and columns). It is then supposed to return the single max of the row and the column. I'm not quite sure what is happening but it is not behaving correctly and is just returning weird results. So I'm kind of lost, I feel like I'm close but not really sure what is going wrong.

int search_max(int a[][MAX], int r, int c, int size)
{
 int maxr = a[0][0];
 int maxc = a[0][0];
 int maxfin = 0;
 int x;
 int y;

 for (x = r; x <= r; x++)
 {
     for (y = 0; y < size; y++)
     {
         if (maxr < a[x][y])
         {
             maxr = a[x][y];
            return maxr;
         }

     }   
 }

 for (y = c; y <= c; y++)
 {
     for (x = 0; x < size; x++)
     {
         if (maxc < a[x][y])
         {
             maxc = a[x][y];
             return maxc;
         }
     }
 }

 if (maxc > maxr)
 {
     maxc = maxfin;
 }

 if (maxr > maxc)
 {

     maxr = maxfin;
 }

 else
 {
     maxr = maxfin;
 }

 return maxfin;
}
Was it helpful?

Solution

Just remove the return maxc and the return maxr and remove the

 if (maxc > maxr)
 {
     maxc = maxfin;
 }

OTHER TIPS

Your problem statement and your function prototype are mismatched:

This is a function prototype to take in a 2D array, specific row, specific column, and size of the array (size is both the amount of rows and columns).

int search_max(int a[][MAX], int r, int c, int size)

The prototype says the array is of indeterminate length but each row is of size MAX, not of size size. In C99 or later, you can write what you specify using a VLA — variable length array:

int search_max(int size, int a[size][size], int row, int col)

You can make your code work as long as the array passed does have a width of MAX, and as long as size is not larger than MAX, and as long as both row and column are less than size.

To search down a column, you don't need nested loops; one is sufficient. Likewise to search across a row, you don't need nested loops. When you do set the initial value for maxc or maxr, you need to set it from the row or column that you're about to analyze.

Putting all that together leads to:

int search_max(int size, int a[size][size], int row, int col)
{
    assert(row >= 0 && row < size);
    assert(col >= 0 && col < size);
    assert(size <= MAX);
    
    int maxr = a[r][0];
    int maxc = a[0][c];
    
    for (int i = 1; i < size; i++)
    {
        if (a[i][c] > maxc)
            maxc = a[i][c];
    }
    for (int i = 1; i < size; i++)
    {
        if (a[r][i] > maxr)
            maxr = a[r][i];
    }
    return (maxr > maxc) ? maxr : maxc;
}

With the given assertions, your function signature could be used in place of mine.

It would also be possible to do without the separate maxr and maxc variables; simply use:

int max = a[r][c];  // Or a[r][0] or a[0][c]

and then run the two loops over the range 0 (instead of 1) to size, comparing and setting and returning max. Keep the loops symmetric. (If the arrays are big, the cost of the extra comparison is insignificant; if the arrays are small, the cost of the extra comparison is insignificant.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top