3 dimensional array help!

This is a discussion on 3 dimensional array help! within the C Programming forums, part of the General Programming Boards category; [So I have to change a 3 dimensional array around, and I am almost there, but not quite! So my ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Thumbs up 3 dimensional array help!

    [So I have to change a 3 dimensional array around, and I am almost there, but not quite! So my problem is as follows:
    Problem 1
    We tackled this problem in class: keeping track of 3 students and their 4 exam grades for each of
    their 2 subjects. Hence, it uses a 3(students)-2(subjects)-4(exams) array. The code for the
    following is attached.
    The program includes functions for:
    Printing an average of all grades
    Finding average grade for each subject for each student
    Finding the average grade for each student in both subjects
    You should now rewrite the code to work with 2(subjects)-3(students)-4(exams) array and do the
    same things as described above and in the attached code.

    Here is the original code:
    Code:
    #include <stdio.h> 
    #define MaxP 3 
    #define MaxR 2 
    #define MaxC 4 
     
    float ave(float points[3][2][4]); 
    float subj(float points[4]); 
    float stud(float points[2][4]); 
     
    void main(void) 
    { 
        int i, j; 
         
        float grades[3][2][4]={{{3, 4, 5, 6}, 
                                {1, 2, 3, 4}}, 
                            {{2, 4, 5, 7}, 
                                {1, 3, 5, 7}}, 
                            {{4, 6, 8, 9}, 
                                {2, 4, 6, 8}}}; 
        printf("The total average of all students/subjects/exams is %.2f\n",ave(grades)); 
        for(i=0;i<MaxP;i++) 
            for(j=0;j<MaxR;j++) 
                printf("The average for student %d in subject %d is %.2f\n", i, j, subj(grades[i][j])); 
        for(i=0;i<MaxP;i++) 
            printf("The average for student %d is %.2f\n", i, stud(grades[i])); 
         
    } 
     
     
    float ave(float points[MaxP][MaxR][MaxC]) 
    { 
        int i,j,k; 
        float total=0; 
        for(i=0;i<MaxP;i++) 
            for(j=0;j<MaxR;j++) 
                for(k=0;k<MaxC;k++) 
                    total+=points[i][j][k]; 
        return total/(MaxP*MaxR*MaxC); 
    } 
     
    float subj(float points[MaxC]) 
    { 
        int i; 
        float total=0; 
        for(i=0;i<MaxC;i++) 
            total+=points[i]; 
        return total/MaxC; 
    } 
     
    float stud(float points[MaxR][MaxC]) 
    { 
        int i,j; 
        float total=0; 
        for(i=0;i<MaxR;i++) 
            for(j=0;j<MaxC;j++) 
                total+=points[i][j]; 
        return total/(MaxR*MaxC); 
    }
    Here is what I have so far:
    Code:
    #include <stdio.h> 
    //MaxP and MaxR values are switched
    #define MaxP 2 
    #define MaxR 3 
    #define MaxC 4 
      //Dimensions 1 and 2 are flip flopped
    float ave(float points[2][3][4]); 
    float subj(float points[4]); 
    float stud(float points[3][4]); 
     
    void main(void) 
    { 
        int i, j; 
         
        float grades[2][3][4]={{{3, 4, 5, 6},{2, 4, 5, 7},{4, 6, 8, 9}}, 
                            {{1, 2, 3, 4},{1, 3, 5, 7},{2, 4, 6, 8}}}; 
        printf("The total average of all students/subjects/exams is %.2f\n",ave(grades)); 
    //Order of for loops is changed
        for(i=0;i<MaxP;i++) 
            for(j=0;j<MaxR;j++) 
                printf("The average for student %d in subject %d is %.2f\n", i, j, subj(grades[i][j])); 
        for(i=0;i<MaxP;i++) 
            printf("The average for student %d is %.2f\n", i, stud(grades[i])); 
         
    } 
     
     
    float ave(float points[MaxP][MaxR][MaxC]) 
    { 
        int i,j,k; 
        float total=0; 
        for(i=0;i<MaxP;i++) 
            for(j=0;j<MaxR;j++) 
                for(k=0;k<MaxC;k++) 
                    total+=points[i][j][k]; 
        return total/(MaxP*MaxR*MaxC); 
    } 
     
    float subj(float points[MaxC]) 
    { 
        int i; 
        float total=0; 
        for(i=0;i<MaxC;i++) 
            total+=points[i]; 
        return total/MaxC; 
    } 
     
    float stud(float points[MaxR][MaxC]) 
    { 
        int i,j; 
        float total=0; 
        for(i=0;i<MaxR;i++) 
            for(j=0;j<MaxC;j++) 
                total+=points[i][j]; 
        return total/(MaxR*MaxC); 
    }
    What my program does is it prints out the following:

    The total average of all students/subjects/exams is 4.54
    The average for student 0 in subject 0 is 4.50
    The average for student 0 in subject 1 is 2.50
    The average for student 1 in subject 0 is 4.50
    The average for student 1 in subject 1 is 4.00
    The average for student 2 in subject 0 is 6.75
    The average for student 2 in subject 1 is 5.00
    The average for student 0 is 3.00
    The average for student 1 is 2.17
    The average for student 2 is 0.00


    Everything is correct except for the last 3 lines. The average for student 0 should be 3.50, for student 1 4.25, and for student 2, 5.88

    I've been trying to mess around with my stud function but I cannot seem to get it working correctly. If you guys could offer me any help/guidance it would be greatly appreciated. C is a very powerful language but can be very tedious at times. Thanks in advance
    Last edited by kplax; 10-18-2011 at 09:46 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kplax View Post
    We tackled this problem in class: keeping track of 3 students and their 4 exam grades for each of
    their 2 subjects. Hence, it uses a 3(students)-2(subjects)-4(exams) array.
    [...]
    C is a very powerful language but can be very tedious at times.
    Especially if you program that way...yuck...no offense, since you are a beginner. I suppose it is too late now, but a more conventional method here would have been to use structs, eg:

    Code:
    struct student {
    	int id;   // might be unnecessary
    	float sub1[4];
    	float sub2[4];
    };
    Your 3D array just became a 1D array of structs with 2 1D arrays each. So eg:
    Code:
    struct student student_list[3];
    [...]
    student_list[0].subj1[2];   // grade of 3rd exam for first student's first subject
    This is much more manageable, but if you have not covered structs yet, you have not covered structs yet.

    To figure out what is wrong with your stud() function, you could try:
    Code:
    float stud(float points[MaxR][MaxC])
    {
        int i,j;
        float total=0;
        for(i=0;i<MaxR;i++)
            for(j=0;j<MaxC;j++) {
                total+=points[i][j];
      // is this the number I think it is?
                printf("%d %d %f\n", i, j, points[i][j]);
            }   
        return total/(MaxR*MaxC);
    }
    BTW, the return type of main() is int, not void.

    Also, you did not need a float array, you just needed to use floats for the averages.
    Last edited by MK27; 10-18-2011 at 10:11 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MK27 View Post
    Especially if you program that way...yuck...no offense, since you are a beginner. I suppose it is too late now, but a more conventional method here would have been to use structs, eg:

    Code:
    struct student {
    	int id;   // might be unnecessary
    	float sub1[4];
    	float sub2[4];
    };
    Which oddly enough takes up more memory than doing it without the structure, even if we ignore padding issues. :P But it is an easier way to wrap your mind around it usually.
    Quote Originally Posted by kplax View Post
    We tackled this problem in class: keeping track of 3 students and their 4 exam grades for each of
    their 2 subjects. Hence, it uses a 3(students)-2(subjects)-4(exams) array. The code for the
    following is attached.
    However, if you really want to use a 3D array, then I would advise you use good variable names.

    Code:
    #define STUDENTS 3
    #define SUBJECTS 2
    #define EXAMS 4
    int studentnum, subjectnum, examnum;
    float gradedata[ STUDENTS ][ SUBJECTS ][ EXAMS ];
    
    for( studentnum = 0; studentnum < STUDENTS; studentnum++ )
        ...
    If I was teaching C, one of the first things I would teach people is to make their code clear and easy to read. It will do wonders for you in the long run.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 dimensional array
    By dpp in forum C++ Programming
    Replies: 10
    Last Post: 03-03-2009, 01:40 PM
  2. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 05:28 AM
  3. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  4. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-10-2008, 12:53 AM
  5. Replies: 1
    Last Post: 04-25-2006, 01:14 AM

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