/***************************************************************************
(c) Copyright 2002 Jeremy Russell., All Rights Reserved.
ITU CSN381, Project 3, 2 http://homepage.mac.com/hteric/Project3.pdf
****************************************************************************
File : score.c
Author : Jeremy Russell
Date : October 17, 2002
Description : Project 3, problem 2
***************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Trying some global enumerated types for fun...
enum {NAME=30, SCORES=3, STUDENTS=3};
// Sub routine predeclaration.
double calculateTotal (double *, double *);
void sortByFinalTotal (double *, char [][]);
/***************************************************************************
Function : main
Author : Jeremy Russell
Date : October 17, 2002
Description : The main() function is the main part of the progrem.
Arguments : void
Returns : 1 on fail and 0 un success.
Notes :
See Also :
***************************************************************************/
int main ( void )
{
char name[STUDENTS][NAME];
double percent[SCORES] = {0.3, 0.3, 0.4};
char tmp[NAME];
double scores[SCORES];
double final[STUDENTS];
char bad[] = "\n";
int x, y;
// An array which stores the percentage of the
// final score each individual scores counts toward.
float scoreIndex[SCORES][2] = { 0.3, 0.3, 0.4};
// Iterate through the class asking for scores.
for (x = 0; x < STUDENTS; x++)
{
printf ("Input student %d's name : ", x + 1);
/* I could have used some other method to get
the name string, but I wanted to experiment
dumping a selected element from a name.*/
fgets( name[x], NAME, stdin );
name[x][strcspn(name[x], bad)] = '\0';
//Get the project score.
printf ("Input %s's project score: ", name[x]);
fgets( tmp, NAME, stdin );
tmp[strcspn(tmp, bad)] = '\0';
scores[0] = atof(tmp);
//Get the midterm score.
printf ("Input %s's midterm score: ", name[x]);
fgets( tmp, NAME, stdin );
tmp[strcspn(tmp, bad)] = '\0';
scores[1] = atof(tmp);
//Get the final score.
printf ("Input %s's final test score: ", name[x]);
fgets( tmp, NAME, stdin );
tmp[strcspn(tmp, bad)] = '\0';
scores[2] = atof(tmp);
final[x] = calculateTotal( scores, percent);
printf ("%s's total: %.2f\n", name[x], final[x]);
}
sortByFinalTotal(final, name);
printf ("\nThese are the scores sorted highest to lowest:\n");
for(x = 0; x < STUDENTS; x++)
{
printf ("%s's score is %.2f\n", name[x], final[x]);
}
return 0;
}
/***************************************************************************
Function : calculateTotal
Author : Jeremy Russell
Date : October 16, 2002
Description : The calculateTotal() function takes a list of numbers and an
associtaed list of how to weigh those numbers (i.e. a
percentage) and returns a double
Arguments : double [], double[]
Returns : The calculateTotal returns a double.
Notes :
See Also :
***************************************************************************/
double calculateTotal ( double scoreList[], double percentList[])
{
int pass, x, y;
double total = 0.0;
for (x = 0; x < SCORES; x++)
{
total += percentList[x] * scoreList[x];
}
return total;
}
/***************************************************************************
Function : sortByFinalTotal
Author : Jeremy Russell
Date : October 17, 2002
Description : The sortByFinalTotal() funcion does a bubble sort of
the final scores. The score array has an associated
name array that must also be sarranged to match the
final score array.
Arguments : double FinalScores[], char nameList[][NAME]
Returns :
Notes :
See Also :
***************************************************************************/
void sortByFinalTotal( double finalScores[], char nameList[][NAME])
{
int pass, j;
double holdScore;
char holdName[NAME];
for ( pass = 1; pass < STUDENTS; pass++ )
{
for ( j = 0; j < STUDENTS - 1; j++ )
{
if ( finalScores[j] < finalScores[j + 1])
{
// Switch the scores.
holdScore = finalScores[j];
finalScores[j] = finalScores[j + 1];
finalScores[j + 1] = holdScore;
// Switch the students to match the scores.
memcpy (holdName, nameList[j], NAME * sizeof (char));
memcpy (nameList[j], nameList[j + 1], NAME * sizeof (char));
memcpy (nameList[j + 1], holdName, NAME * sizeof (char));
}
}
}
}