sorting columns in 2d array and find avgse

This is a discussion on sorting columns in 2d array and find avgse within the C Programming forums, part of the General Programming Boards category; hell I want to first sort my columns of the test grades entered. it should sort them like this exam ...

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

    sorting columns in 2d array and find avgse

    hell I want to first sort my columns of the test grades entered. it should sort them like this

    exam 0 exam 2 exam 3 exam4
    70 68 86 73
    77 87 86 78
    96 90 89 81

    but i get some weird sorting. I also want to find the average of all the total test /total number of students. I comment out the average for all because I kept getting an error.

    well here is my code. and thanks for all the help.

    Code:
    /* Code by 
    I don't think it compiles anymore it just too but I just make too many mistakes.
    run on csp03 machine*/
    
    #include <stdio.h>
    //#include "constants.h"
    #define NUMBERofSTUDENTS 3
    #define EXAMSCORES 4
    
    double averagexam( int [][EXAMSCORES], int, int );
    double studentavg( int [], int);
    void printarray(int grades[][EXAMSCORES], int , int);
    //double averageall(const int[][EXAMSCORES], int,int );
    void bubblesort( int[][EXAMSCORES], int, int);
    //void initialScores(int[][EXAMSCORES]);
    
    int main(void){
    
     int exams;
     int students; 
     
     int scores[NUMBERofSTUDENTS][EXAMSCORES]=
    	{{77,68,86,73},
    	{96,87,89,78},
    	{70,90,86,81}};
    
     //initialScores(scores);
    
    for(exams=0;exams<EXAMSCORES;exams++){
    	printf("the avg grade for exam %d is%.2f\n",
    	exams, averagexam(scores, NUMBERofSTUDENTS, exams));
    	}
    for(students=0;students<NUMBERofSTUDENTS;students++){
    	printf("the avg for student %d is %.2f\n",
    	students, studentavg(scores[students], EXAMSCORES));
    	}
    
    //printf("The avg for all the test/students is %d\n", 
    //	averageall(scores, NUMBERofSTUDENTS, EXAMSCORES));
    
    for(exams=0;exams<EXAMSCORES;exams++){
    	
    	bubblesort(scores, NUMBERofSTUDENTS, exams);
    }
    
    printarray(scores, NUMBERofSTUDENTS, EXAMSCORES);
    
    return 0;
    }
    
    
    
    double averagexam(  int setofexams[][EXAMSCORES], int students, int 
    exams)
    {
    	int i;
    	int total=0;
    
    	for (i=0;i<students;i++){
    	
    	 total += setofexams[i][exams];
    	 
    	}
    	return (double) total/students;
    }
    
    
    
    double studentavg( int grades[], int tests){
    
      int i;
      int total=0;
    
      for(i=0;i<tests;i++){
       total+=grades[i];
      }
      
      return (double) total/tests;
    }
    
    
    /*double averageall(const grades[][EXAMSCORES], int rows, int colm){
    
      int i;
      int j;
      int total;
    
      for(i=0;i<rows;i++){
    	for(j=0;j<colm;j++){
    	
    	total+=grades[i][j];
    	
    	}   
    
    
      }
    
      return(double) total/(rows*colm);
    
    }
    
    
    void initialScores(int scores[NUMBERofSTUDENTS][EXAMSCORE]){
    
    	int i;
    	int j;
    
    	for (i=0;i<NUMBERofSTUDENTS;i++){
    		for (j=0;j<EXAMSCORES;j++){
    		scanf("%d", &scores[i][j]);
    		}
    	}
    }
    */
    
    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-i-1;j++){
    
    	if(a[i][exams] > a[i][exams+1])
    	{
    		temp=a[i][exams];
    		a[i][exams]=a[i][exams+1];
    		a[i][exams+1]= temp;
    
    	}
        }
     }
    
    
    }	
    
    void printarray(int grades[][EXAMSCORES], int pupils, int tests)
    {
    
    	int i;
    	int j;
    
    	for(i=0;i<pupils;i++)
    	{
    	printf("\n");
    	
    		for(j=0;j<tests;j++){
    		printf("%-5d", grades[i][j]);
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    it says hell I meant hello, sorry.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    For the averages, I think it depends on what you need to look at. If you want to find the average score on a particular exam for the whole class, for example, you would add only those scores and divide by the number of students. The number of students is not row*col. It's the same story if you wanted the average final score and so forth.

    To sort by column, I would just transpose the matrix and then sort on the first dimension. Normally the first dimension is constant and the second dimension moves around, but you can transpose to avoid working with that.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    yeah the averages is for the whole class.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    ok figured out bubble sort, thanks.

    For some reason i can not find the whole class average.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Easiest way to find the max value stored in an array
    By criticalerror in forum C++ Programming
    Replies: 14
    Last Post: 01-22-2004, 03:35 PM
  4. sorting array efficiently...can't find the error
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-09-2002, 03:32 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 09:05 AM

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