Thread: problem with 2d array program.

  1. #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.

  2. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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