Thread: Grading Program using multidimensional arrays

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Unhappy Grading Program using multidimensional arrays

    Hi,

    I am working on a program to read in a multidimensional array of the number of students in a class and the number of exams which I have written as:
    int scores [NUMBERofSTUDENTS][EXAMSCORES];

    what I am trying to do is calculate the class average for each exam, the median of all the scores, print the histogram, calculate the average of each student, and the average of the class. I have both bubble sort and the histogram functions written, but only for one-dimentional arrays. How can I modify them to work for this program?

    I also have already written the main program, but only for one-dimensional arrays too. So how can I change it to handle multi-dimentional arrays?

    I have to do this with multiple files so bubble sort, histogram, and the main are all separate.

    Basically my problem is that I cannot figure out how to pass multidimensional arrays as single dimensional arrays. Please Help me if you can. I can supply you with greater detail if needed.
    I have to have this completed by Wednesday so any help is greatly appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    needed

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There's no problem sending a 2D array to a function. I believe however, it would be easier to just use an a struct defined globally (above any function), and say each student had 3 test scores:

    Code:
    #include <stdio.h>
    
    #define MAX 50
    struct student {
      int ID_num;
      int score1;
      int score2;
      int score3;
      //int avgScore?
    }stud;
    
    int main(void) {
      struct student studs[MAX];
    You can add as many scores as you need, and studs[] will still be a 1D array, which should work easily with your current code, just using the dot operator:
    Code:
    for(i=0;i<MAX;i++) {
      sum+=studs[i].score1, etc.
      sum+=studs[i].score2, etc.
      //etc.
    }
    Time is short, if you want good help, you have to post your code, and stick around a bit to answer questions that may be raised by it.

    Forums like this are good, but if you aren't available or can't give detailed answers, they can take a very long time, since people start losing interest in the thread.

    So my advice is that you get on it, ASAP.

  4. #4
    Registered User l2krence's Avatar
    Join Date
    Nov 2010
    Posts
    24
    oh by the way, wad does += stands for ? my lecturer also talk about this too but i didnt get him~

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by l2krence View Post
    oh by the way, wad does += stands for ? my lecturer also talk about this too but i didnt get him~
    variable = variable + some_number;

    Is shortened in C to allow:

    variable += some_number;

    Same for all the other arithmetic operators:
    variable *= 4;
    variable /= 3;
    variable -= 5;

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    ok i think we have the same teacher I was working on one of the functions. which is to add all the test averages separately. like test one avegare, etc.

    Here is what i have can some one tell me what im doing wrong?

    Code:
    #include <stdio.h>
    #define NUMBERofSTUDENTS 3
    #define EXAMSCORES 4
    
    double averagexam( const int [], int );
    
    int main(void){
    
     int exams;
    const int studentGrades[NUMBERofSTUDENTS][EXAMSCORES]=
    	{ {77,68,86,73},
    	 {96,87,89,78},
    	 {70,90,86,81}};
    for(exams=0;exams<EXAMSCORES;exams++){
    	printf("the avg grade for exam %d is%.2f\n",
    	exams, averagexam(studentGrades[exams],NUMBERofSTUDENTS));
    	}
    
    return 0;
    }
    
    
    double averagexam( const int setofexams[], int students)
    {
    	int i;
    	int j;
    	int total=0;
    	for (i=0;i<students;i++){
    	
    	 total += setofexams[i];
    	 
    	}
    	return (double) total/students;
    }

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by newbc View Post
    ok i think we have the same teacher I was working on one of the functions. which is to add all the test averages separately. like test one avegare, etc.

    Here is what i have can some one tell me what im doing wrong?

    Code:
    #include <stdio.h>
    #define NUMBERofSTUDENTS 3
    #define EXAMSCORES 4
    
    double averagexam( const int [], int );
    
    int main(void){
    
     int exams;
    const int studentGrades[NUMBERofSTUDENTS][EXAMSCORES]=
    	{ {77,68,86,73},
    	 {96,87,89,78},
    	 {70,90,86,81}};
    for(exams=0;exams<EXAMSCORES;exams++){
    	printf("the avg grade for exam %d is%.2f\n",
    	exams, averagexam(studentGrades[exams],NUMBERofSTUDENTS));
    	}
    
    return 0;
    }
    
    
    double averagexam( const int setofexams[], int students)
    {
    	int i;
    	int j;
    	int total=0;
    	for (i=0;i<students;i++){
    	
    	 total += setofexams[i];
    	 
    	}
    	return (double) total/students;
    }
    Wrong.
    Swap EXAMSCORES and NUMBERofSTUDENTS in your for-loop.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    billy, which for loop? the one in the functions parameters or the one in main? hopefully you understood what i want to do, calculate the exam average's not students averages.

    well it seems the loop in the function parameters only loops through rows only.
    how will i loop through columns only, then?
    Last edited by newbc; 11-15-2010 at 10:16 PM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    well here an update on my code. I know im close, because it gives me the average for exam one which is 81, but it does it for all of them. I removed the inner loop because I thought it will change 0 everytime but i just confused me.

    Code:
    #include <stdio.h>
    #define NUMBERofSTUDENTS 3
    #define EXAMSCORES 4
    
    double averagexam( const int [][4], int );
    
    int main(void){
    
     int exams;
    const int studentGrades[NUMBERofSTUDENTS][EXAMSCORES]=
    	{ {77,68,86,73},
    	 {96,87,89,78},
    	 {70,90,86,81}};
    for(exams=0;exams<EXAMSCORES;exams++){
    	printf("the avg grade for exam %d is%.2f\n",
    	exams, averagexam(studentGrades, NUMBERofSTUDENTS));
    	}
    
    return 0;
    }
    
    
    double averagexam( const int setofexams[][4], int students)
    {
    	int i;
    	int j;
    	int total=0;
    	for (i=0;i<students;i++){
    	 //for(j=0;j<4;j++){
    
    	 total += setofexams[i][0];
    
    	 //}
    	}
    	return (double) total/students;
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The averageexam function is only totaling the scores for exam 0 every time. You need to pass in the exam number into average exam, like so:
    Code:
    double averagexam( const int setofexams[][4], int students, int exam_number)
    Then adjust your total +=... line accordingly.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Quote Originally Posted by anduril462 View Post
    The averageexam function is only totaling the scores for exam 0 every time. You need to pass in the exam number into average exam, like so:
    Code:
    double averagexam( const int setofexams[][4], int students, int exam_number)
    Then adjust your total +=... line accordingly.
    but do I need nested loops in the function? where should use int exam_number though?

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    but do I need nested loops in the function?
    Nope. You only need to change averagexam to take the exam_number, pass in the exam number when you call it, and change the total += line.

    where should use int exam_number though?
    Your more or less answered the question in your other post, with this comment:

    it gives me the average for exam one which is 81, but it does it for all of them
    You are always calculating the average for exam one (array location 0). So, adjust your total += line to use the exam_number variable instead of 0.

    That's it. Just a few very simple, short changes.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    thanks for the help, i wasnt updating my function call.

    now he wants us to do a buble sort in order to find the median of each set of exams, here is his example on how to sort them for one dimensional array.

    Code:
    // ************************************************************************
    // 
    //	bubble.c --- bubbleSort function, oddly enough does a bubble sort
    //                   on an array of ints.
    // 
    //	
    // 
    // ************************************************************************
    
    
    void bubbleSort(int A[], int num)
    {
        int i,j;
        int temp;
    
        for(i = 0; i < num-1; i++)
        {
            for(j = 0; j < num-i-1; j++)
            {
    	    if( A[j] > A[j+1] )
                {
    		// swap A[j], A[j+1]
                    temp = A[j];
                    A[j] = A[j+1];
                    A[j+1] = temp;
                }
            }
        }
    }
    and here is my code.


    Code:
    void bubbleSort(int A[][4], int students)
    {
        int i;
        int temp;
    
        for(i = 0; i <students -1; i++)
        {
                   
    	    if( A[i] > A[i+1] )
                {
    		// swap A[i], A[i+1]
                    temp = A[i];
                    A[i] = A[i+1];
                    A[i+1] = temp;
                }
        }
    }
    Last edited by newbc; 11-17-2010 at 07:30 AM.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your bubble sort left off one of the for loops. You will need two of them.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Ok im trying to call my bublesort function but it keeps saying im missing one argument. what does it mean?

    Code:
    
    #include <stdio.h>
    #define NUMBERofSTUDENTS 3
    #define EXAMSCORES 4
    
    
    void bubblesort( int[][EXAMSCORES], int, int);
    
    int main(void){
    
     int exams;
     int ex;
     int studs;
    const int studentGrades[NUMBERofSTUDENTS][EXAMSCORES]=
    	{ {77,68,86,73},
    	 {96,87,89,78},
    	 {70,90,86,81}};
    
    
    for(exams=0;exams<EXAMSCORES;exams++){
    	
    	bubblesort(studentGrades, NUMBERofSTUDENTS, exams);
    	
    }
    
    return 0;
    }
    // line 29
    
    
    
    void bubblesort( int a[][EXAMSCORES],int students, int exams){
    
    	int i;
    	int j;
    	int temp;
    
     for (i=0;i<students;i++)
     {
    
       for(j=0;j<students-1;j++){
    
    	if(a[j][exams] > a[j][exams+1])
    	{
    		temp=a[j][exams];
    		a[j][exams]=a[j][exams+1];
    		a[j][exams+1]= temp;
    
    	}
        }
     }
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on dynamic multidimensional arrays
    By TheLegend219 in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2010, 10:30 AM
  2. Need help with program using arrays
    By altrev in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2009, 01:25 PM
  3. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  4. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  5. Replies: 0
    Last Post: 10-29-2001, 11:40 PM

Tags for this Thread