Question: This example illustrates using an array to process a set of test scores. I need help on how i could set up the following functions:
This example illustrates using an array to
process a set of test scores.
I need help on how i could set up the following functions:
printScores findMinScore findMaxScore calculateAverage calcStdDev
Help would be much appreciated!
#include
#include
#include
#define MAX_TESTS 100
void printScores(int t[MAX_TESTS], int n);
int readTestsFromFile(char *fileName, int t[MAX_TESTS]);
float calcAverage(int t[MAX_TESTS], int n);
float calcStdDev(int t[MAX_TESTS], int n);
int findMaxScore(int t[MAX_TESTS], int n);
int findMinScore(int t[MAX_TESTS], int n);
void bubbleSort(int A[MAX_TESTS], int n);
int fillTestArray(int t[MAX_TESTS]);
int main()
{
int test[MAX_TESTS] = { 0};
int n = 0;//actual number of tests
float average;
n = readTestsFromFile("test_scores.txt", test);
//n = fillTestArray( test);
printf("%d tests read in. ", n);
printScores(test, n);
average = calcAverage(test, n);
printf("avg = %.2f ", average);
printf("standard deviation = %0.2f ", calcStdDev(test, n));
printf("maximum score = %d ", findMaxScore(test, n));
printf("minimum score = %d ", findMinScore(test, n));
printScores(test, 10);
bubbleSort(test,10);
printf("after sort ");
printScores(test, 10);
return 0;
}//main
/*
This function was written by Kevin Lee
it works, do not change!!!!
*/
int readTestsFromFile(char *fileName, int t[MAX_TESTS])
{
int count = 0;
int value;
FILE *filePtr;
filePtr = fopen(fileName, "r");
if( filePtr == NULL)
{
printf("Unable to find the data file ");
return 0;
}
do{
fscanf(filePtr, "%d", &value);
if(value > -1){
t[count] = value;
++count;
}
}while(value > -1 && count < MAX_TESTS);
return count;
}
float calcAverage(int t[MAX_TESTS], int n)
{
float average = 0;
float sum = 0;
printf("calcAverage under construction ");
return sum;
}
float calcStdDev(int t[MAX_TESTS], int n)
{
return 0;
}
void printScores(int t[MAX_TESTS], int n)
{
int i;
for(i = 0; i < n; ++i){
printf("%3d", t[i]);
}
printf(" ");
}
int findMaxScore(int t[MAX_TESTS], int n)
{
int i, max = -1;
max = t[0];
for(i = 1; i < n; ++i){
if (t[i] > max){
max = t[i];
}
}
return max;
}
int findMinScore(int t[MAX_TESTS], int n)
{
int min = -1;
printf("findMinScore under construction ");
return min;
}
void bubbleSort(int A[MAX_TESTS], int n)
{
int i, j, temp;
for(i= 0; i < n-1; ++i){
//inner for loop
for(j = 0; j < n-1; ++j ){
if(A[j] > A[j+1]){
//swap
temp = A[j+1];
A[j+1] = A[j];
A[j] = temp;
}
}//for j
printScores(A,n);
}//for i
}
int fillTestArray(int t[MAX_TESTS])
{
int data[MAX_TESTS] = {25,
40, 36, 26, 36, 33, 33, 34, 39, 4, 35,
38, 29, 33, 30, 41, 29, 40, 39, 39, 35,
38, 38, 28, 30, 28, 24, 16,-1};
int n = 0;
while(data[n] != -1){
t[n] = data[n];
n++;
}
return n;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
