I have to turn this code in tomorrow by 4:00. It will not run right. I found out it is not even going through the sort loop and I can't figure it out. Please, can anyone help me out?
Problem: Write a program to compute the arithmetic mean (average) median, and mode for up to 50 test scores. The data are contained in a text file.
Code:
#include <stdio.h>
#include<math.h>
#define NULL 0
FILE *scoresFile;
int scores[50];
/*Function Definitions*/
int readFile (int);
void calculateMean (int);
void sort (int);
void calculateMedian(int);
void calculateMode ();
main()
{
int numberOfScores = 0;
numberOfScores = readFile(numberOfScores);
calculateMean(numberOfScores);
sort(50);
calculateMedian(numberOfScores);
calculateMode();
}
/*Read the file with scores*/
//*************************read file*********************
int readFile (int numberOfScores)
{
/*bool flag = TRUE;*/
scoresFile = fopen("scores.txt","r");
if(scoresFile == NULL)
{
printf("\nERROR-cannot open the file\n");
}
/*loop to read scores from file*/
/*read each entry from file*/
while ( fscanf(scoresFile, "%d", &scores[numberOfScores]) != EOF && numberOfScores < 50)
{
printf("\n%d",scores[numberOfScores]);
numberOfScores++;
if(numberOfScores > 50)
{
printf("\nERROR-More than 50 scores available");
}
}
fclose(scoresFile);
return numberOfScores;
}
/*===============================================================*/
void calculateMean( int numberOfScores)
{
int i, total;
int mean;
total = 0;
printf("\n%d",numberOfScores);
for(i = 0; i <= numberOfScores; i++)
{
total += scores[i];
printf("\n%d",total);
}
mean = total/numberOfScores;
printf("\nMean of the Scores: %d" , mean);
}
/*================================================================*/
void sort(int array_size)
{
int i, j, temp, x;
for (i = array_size-1; i>= 0; i-- )
{
for (j=1; j<= i; j++)
{
if(scores[j-1] > scores[j])
{
temp = scores[j-1];
scores[j-1] = scores[1];
scores[1] = temp;
}
}
}
printf("\n\n\n");
for (x=0;x <= 20; x++){
printf("\n%d",scores[x]);
}
}
/*================================================================*/
void calculateMedian(int numberOfScores)
{
float median;
median = (scores[numberOfScores/2 - 1 ] + scores[numberOfScores/2])/2;
printf("\nMedian: %f", median);
}
/*================================================================*/
void calculateMode()
{
int multi[50][2];
int j, k;
int higher;
int score;
/*initialize the array second element to 0*/
for(k=0; k<50; k++)
{
multi[k][0] = 0;
multi[k][1] = 0;
}
/*pass the original array and store array into multidimensional if there are no entries for that value*/
for(j=0; j<50; j++)
{
score = scores[j];
for(k = 0; k< 50; k++)
{
if(score == multi[k][0])
{
multi[k][1] = multi[k][1] + 1;
}
}
}
higher = multi[0][1];
for(j=0; j<50; j++)
{
if(higher , multi[j][1])
higher= multi[j][1];
}
printf("\nMode of the scores: %d", higher);
}
Here is my text file:
98
100
96
92
91
89
87
86
85
83
82
81
83
85
65
61
62
63
64
65
66
67
68
69
70
80
81
83
72
73
74
75
76
77
78
79
85
84
83
82
100
89
96
97
88
67
68
69
70
80