C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-08-2002, 04:38 PM   #1
Registered User
 
Join Date: Mar 2002
Posts: 11
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

Last edited by Niloc1; 04-08-2002 at 04:48 PM.
Niloc1 is offline   Reply With Quote
Old 04-08-2002, 05:47 PM   #2
Registered User
 
Join Date: Mar 2002
Posts: 95
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.
Bull is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
8 Queens, problem with searching 2D array Sentral C++ Programming 35 03-11-2009 04:12 PM
Help with mallocing a 2d array please? Gatt9 C Programming 5 10-10-2008 03:45 AM
Unknown Memory Leak in Init() Function CodeHacker Windows Programming 3 07-09-2004 09:54 AM
fopen(); GanglyLamb C Programming 8 11-03-2002 12:39 PM
Simple 2D array problem...In a Hurry! 67stangman C++ Programming 8 04-18-2002 01:22 PM


All times are GMT -6. The time now is 03:20 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22