• Status: Solved
• Priority: Medium
• Security: Public
• Views: 306

# maximum for each column in 2d array

Hi everyone,

I wrote a small programm to find the maximum for each row by following a tutorial on finding the average for each row

Im wondering how can i make it to find the maximum for each column....would i have to have 2 for statements?

here's what i have:
``````#include "stdafx.h"
#include <stdio.h>
void printArray(const int data[][5],int facilities, int locations);
int maximum(const int setdata[],int facilities);

int _tmain(int argc, _TCHAR* argv[])
{
int location;
const int locationdata[5][5]={
{0,82,37,51,100},
{67,0,78,93,97},
{74,48,0,20,49},
{20,87,27,0,66},
{62,37,51,87,0}
};

printf("The initial data array is:");

printArray(locationdata,5,5);

for(location=0;location<5;location++){

printf("\n\nHighest Number: %d\n",maximum(locationdata[location],5));
}

return 0;
}

void printArray(const int data[][5],int facilities, int locations)
{
int i;
int j;

for(i=0;i< facilities; i++){
printf("\n ");
for(j=0;j<locations;j++){
printf("%d ", data[i][j]);
}
}
}

int maximum(const int setdata[],int facilities)
{
int i;
int j;
int highNumber=0;

for(i=0; i<facilities; i++){
if(highNumber<setdata[i]){
highNumber=setdata[i];
}
}

return highNumber;
}
``````
0
goofy26
• 2
1 Solution

Commented:
>> Im wondering how can i make it to find the maximum for each column....would i have to have 2 for statements?

You already have two for loops (one in main, and one in the maximum function).

So, yes, you'd need two for loops for getting the maximum of each column too, just like for rows.

The code will be very similar, except that you work on columns, rather than rows.

Note that you might want to revisit the parameters of the maximum function, so you can access a whole column.
0

Author Commented:

ok it works now but it actually doesnt need 2 for statements in the maximum function
the mistake was that i didnt declare a 2d array in there...how silly of me...

thanks a lot
0

Commented:
>> but it actually doesnt need 2 for statements in the maximum function

I didn't say that ;) I said you need two for loops in total ... You can place them wherever you want.
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.