# problem with 2d array program.

• 04-08-2002
Niloc1
problem with 2d array program.
I'm having a problem with a 2d array program. I wrote the bulk of a program that takes the marks of three courses and provides an average. The output looks like this;

Prgm Eng. Math Average

0,0 0,1 0,2 0,3
1,0 1,1 1,2, 1,3

and so on for five rows.

The next part of the program that I'm having trouble with is finding the highest of the three marks and adding this data to the array. My messed up code for the function looks like this:

}
void print_marks(double marks[ROWS][COLS],double avg[ROWS],int r,int
c)
{
int i,j;
for (i=0;i<r;i=i+1) {
printf("%4s%8s%8s%8s%11s\n"," ", "Prgm", "Math", "Engl", "Avrg"\$
for (j=0;j<c;j=j+1) {
printf("%8.1f", marks[i][j]);
}
printf("%11.1f\n", avg[i]);
}
}

This part returns the expected results.

The messedup find_high function:

}
void find_high(double marks[ROWS][COLS],int r,int c)
{
int i,j;

for (j=0;j<r;j=j+1) {
for (i=0;i<r;i=i+1) {
if (marks[i][j]>marks[i+1][j]) {

}

I'm confused about the code for this part. Any help would be appreciated.

Colin
• 04-08-2002
Bull
your using the value R in both for loops, I'm suspecting thats your prob:

Code:

```void find_high(double marks[ROWS][COLS],int r,int c) {   int i,j,highest=0;   for (j=0;j<r;j++)     for (i=0;i<c;i++)       if (marks[j][i]>highest)         highest=marks[j][i]; }```
or you could reference the position instead of writing it to variable highest.
you would also have a problem because I think you were reading the wrong way or atleast top to bottom rather than accross ways (rows rather than columns) and you if you had a full list then in your if statement your trying to compare it with a value that isnt defined by doing the +1.
This function will give you the highest number in the entire list, if you wanted for each one then remove the first for statement.