calculate meian

Published on February 2017 | Categories: Documents | Downloads: 35 | Comments: 0 | Views: 250
of 4
Download PDF   Embed   Report

Comments

Content

#include <stdio.h>
#define STUDENTS 100
#define EXAMS 4
int minimum(const int scores[][EXAMS], int pupils, int test );
int maximum(const int scores[][EXAMS], int pupils, int test );
double mean(const int setOfScores[], int test);
void printArray(const scores[][EXAMS], int pupils, int tests);
void median( int setOfScores[], int test );
void bubbleSort( int a[][EXAMS], int, int );
int main(void)
{
int student;
int i, j, pupils, tests;
int studentScores[ STUDENTS][EXAMS];
FILE *infile;
infile = fopen ("data.txt", "r");
fscanf (infile, "%d %d", &pupils, &tests);
for (i=0; i<pupils; i++) {
for (j=0; j<tests; j++) {
fscanf (infile, "%d", &studentScores[i][j]);
}
}
/* output array studentScores */
printf( "The array is:\n");
printArray( studentScores, STUDENTS, EXAMS );
/**************************************************
** determine smallest and largest grade values
*****
**************************************************/
printf("\n\nLowest grade: %d\nHighest grade: %d\n",
minimum( studentScores, STUDENTS, EXAMS ),
maximum( studentScores, STUDENTS, EXAMS ));
/***************************************************
**** calculate average grade for each student
************************************************/
for (student = 0; student<STUDENTS; student++ ) {
printf( "mean=The average grade for student %d is %.2f\n",
student, mean(studentScores[student], EXAMS));
/**************************************************
******** calculate median for each student **************
*************************************************/
for (student =0; student<STUDENTS; student++) {
printf( "median= %d is %.2f\n",
student, median(studentScores[student], EXAMS));
}
return 0;

}
/***********************************************************************
*********
Find the minimum grade
*******
***********************************************************************/
int minimum( const int scores[][EXAMS], int pupils, int tests )
{
int i; /* student counter */
int j; /* exam counter */
int lowGrade = 100; /* initialize to highest possible grade */
/* loop through rows of scores */
for (i=0; i < pupils; i++) {
/* loop through columns of scores */
for (j=0; j < tests; j++) {
if (scores[i][j] < lowGrade) {
lowGrade = scores[i][j];
}
}
} /*
return lowGrade;
}
/**********************************************************
********
Find the maximum grade
*******
*********************************************************/
int maximum( const int scores[][EXAMS], int pupils, int tests)
{
int i;
int j;
int highGrade = 0; /*initialize to lowest possible grade */
/* loop through rows of scores */
for (i=0; i < pupils; i++) {
/* loop through columns of scores */
for (j=0; j < tests; j++) {

}

if (scores[i][j] > highGrade ) {
highGrade = scores[i][j];
}

}
}

return highGrade;

/******************************************************
*** Determine the average score for a particular student ********
*******************************************************/
double mean( const int setOfScores[], int tests )
{
int i;
int total = 0;

/* total all scores for one student */
for (i=0; i < tests; i++) {
total += setOfScores[i];
}
return (double) total / tests;
}
/******************************************************
********
Print the array
********
******************************************************/
void printArray(const int scores[][EXAMS], int pupils, int tests)
{
int i;
int j;
for (i=0; i<pupils; i++) {
/* output label for row */
printf("\nstudentScores[%d] ", i);
/* output scores for one student */
for (j = 0; j < tests; j++) {
printf( "%-5d", scores[i][j] );
}
}
void median( int setOfScores[], int tests)
{
bubbleSort( studentScores[][EXAMS], int, int); /* sort array */
printf("\n\nThe sorted array is ");
printArray(setOfScores, int tests); /* output sorted array */

}

/*display median element */
printf("\n\nThe median is %d\n\n", scores[][ EXAMS / 2] );
/* end function median */

/*******************************************************
** function that sorts an array with bubble sort algorithm ***
*******************************************************/
void bubbleSort(int studentScores[][EXAMS], int hold, int test)
{
int pass; /* pass counter */
int i, j;
/* comparism counter */
int hold; /* temporary location used to swap elements */
/* loop to control number of passes */
for (pass = 1; pass < tests; pass++)

{

/* loop to control number of comparisons per pass */
for (j = 0; j < tests -1; j++)
{
/* swap elements if out of order */
if (studentScores[i][j] > studentScores[i][j + 1]) {
hold = studentScores[i][j];
studentScores[i][j] = studentScores[i][j + 1];

studentScores[i][j + 1] = hold;
}
}
}

}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close