Question: This program determines the winners at a particular talent competition. Your task: 1. Code review: read and understand this program (**** Please code this program
This program determines the winners at a particular talent competition.
Your task:
1. Code review: read and understand this program (**** Please code this program in C language ***)
2. Call the following functions in main:
calculateScore
insertionSort
writeFile
displayWinners
3. Call getMinMax in calculateScore
4. Define and call the displayWinners function
5. Run the program.
6. Save its output at the end of the source file as a comment.
performers.txt
8 John 70 78 71 79 75
David 83 29 98 92 97
Mary 83 80 89 79 85
Andy 91 89 90 87 91
Ann 90 88 91 97 93
Lucy 82 84 89 93 85
Dan 90 56 89 99 73
Sue 79 82 76 81 80
contestants.txt
11 Linda 93 96 99 99 90
Steve 83 29 98 92 97
Mary 83 80 89 79 85
Tom 91 89 90 87 91
Dana 90 88 91 97 93
James 99 96 99 90 93
Marie 96 90 99 99 93
Bob 82 84 89 93 85
Kevin 83 92 98 29 97
Tina 90 96 99 99 93
Jonathan 99 96 90 99 93
test.txt
7 John 83 75 89 99 86
David 83 89 99 86 75
Mary 83 75 89 86 99
Andy 75 83 89 99 86
John 99 83 75 89 86
Ann 83 89 99 75 86
Dan 83 99 75 89 86
*/
#include
#include
#include
#define NUM_JUDGES 5
#define NUM_PERFRM 100
#define NAME_SIZE 32
typedef struct {
char name[NAME_SIZE];
int scores[NUM_JUDGES];
int final;
}PERFORMER;
typedef struct
{
int size; // the actual number of performers
PERFORMER list[NUM_PERFRM]; // the array of performers
} LIST;
void printInfo(void);
void printEnd(void);
int readPerfData(LIST *perfData, char fileName[]);
int getOutFileName(const char fileName[], char outFileName[]);
void writeFile(const LIST *perfData, char outFileName[]);
void calculateScore(LIST *perfData);
void getMinMax(const int scoreList[], int *min, int *max);
void insertionSort(LIST *perfData);
void displayWinners(const LIST *perfData);
void showErrorMessage(int errorCode, char s[]);
int main (void)
{
char inFileName[][NAME_SIZE] =
{"performers.txt", "contestants.txt", "abc.txt", "test", "test.txt", ""};
LIST perfData;
int i, success;
char outFileName[32];
// call printInfo
printInfo();
for ( i = 0; *(inFileName[i]) != '\0'; i++)
{
if (!getOutFileName(inFileName[i], outFileName))
showErrorMessage(0, inFileName[i]);
else
{
success = readPerfData(&perfData, inFileName[i]);
if (success != 1)
showErrorMessage(success, inFileName[i]);
else // everything is OK
{
printf("Reading from \"%s\" . . . Writing to \"%s\" ", inFileName[i], outFileName);
// call calculateScore
// call insertionSort
// call writeFile
// call displayWinners
}
}
}
// call printEnd
printEnd();
return 0;
}
/* *******************************************************
Displays the winner(s)
PRE : perfData
POST : one or more winners displayed
*/
void displayWinners(const LIST *perfData)
{
}
/* *******************************************************
Prints information about the project.
PRE : nothing
POST : info printed
*/
void printInfo(void)
{
printf(" \t\tArrays, Strings, Structures, Sorting, Pointers, and "
" \t\t\t\tDynamic Allocation of Memory ");
printf("This program determines the winners at a particular talent competition. ");
putchar(' ');
}
/* *******************************************************
Prints a farewell message.
PRE : nothing.
POST : farewell message printed
*/
void printEnd(void)
{
printf(" \t\tThank you for using the program,"
" \t\tHave a great day! ");
}
/* *******************************************************
Reads data from the input file into an array of structures
PRE : perfData - pointer to an empty structure
fileName - the name of the input file
POST : perfData - filled with data from the input file
*/
int readPerfData(LIST *perfData, char fileName[])
{
int success = 1;
int num, i, j;
FILE *fpIn;
fpIn = fopen(fileName, "r");
if (!fpIn)
{
success = -2; // file not found
}
else
{
fscanf(fpIn, "%d", &num);
if (num > NUM_PERFRM)
{
success = -1; // file too big
}
else
{
perfData->size = num;
for(i = 0; i < num; i++)
{
fscanf(fpIn, "%s", perfData->list[i].name);
for(j = 0; j < NUM_JUDGES; j++)
fscanf(fpIn, "%d", &(perfData->list[i].scores[j]) );
}
fclose(fpIn);
}
}
return success;
}
/* *******************************************************
Generates the output file's name from the inputFile's name
"test.txt" => "test_out.txt"
PRE : fileName - input file name
POST : outFileName - output file name
*/
int getOutFileName(const char fileName[], char outFileName[])
{
char temp[NAME_SIZE];
char *ptr;
int valid = 1;
strcpy(temp, fileName);
ptr = strchr(temp, '.');
if (ptr)
{
*ptr = '\0';
strcpy(outFileName, temp);
strcat(outFileName, "_out.txt");
}
else
valid = 0; // invalid file name: it does not contain '.'
return valid;
}
/* *******************************************************
Writes the array to an output file.
PRE : perfData
outFileName
POST : output file contains data
*/
void writeFile(const LIST *perfData, char outFileName[])
{
FILE *fpOut;
int i, j;
fpOut = fopen(outFileName, "w");
fprintf(fpOut, "%d ", perfData->size);
for (i = 0; i < perfData->size; i++)
{
fprintf(fpOut, "%-20s ", perfData->list[i].name);
for (j = 0; j < NUM_JUDGES; j++)
fprintf(fpOut, "%4d", perfData->list[i].scores[j]);
fprintf(fpOut, " %4d ", perfData->list[i].final);
}
fclose(fpOut);
}
/* *******************************************************
Determines the lowest and highest score for one performer
PRE : scoresList[] - a list of 5 scores
POST : lowest, highest scores determined
and passed back to the caller as output parameters
*/
void getMinMax(const int scoreList[], int *min, int *max)
{
int j;
*min = *max = scoreList[0];
for (j = 1; j < NUM_JUDGES; j++)
{
if (scoreList[j] < *min)
*min = scoreList[j];
if (scoreList[j] > *max)
*max = scoreList[j];
}
}
/* *******************************************************
Calculates the final score for each performer:
average of 3 scores with lowest and highest eliminated
PRE : perfData - without the final score
POST : perfData - with the final score calculated
*/
void calculateScore(LIST *perfData)
{
int i, j;
int finalScore, lowest, highest;
for (i = 0; i < perfData->size; i++)
{
finalScore = 0;
for (j = 0; j < NUM_JUDGES; j++)
{
finalScore += perfData->list[i].scores[j];
}
// call getMinMax
perfData->list[i].final = finalScore - lowest - highest;
}
}
/* *******************************************************
Sorts the array in descending order by the final score
PRE : perfData - has data, including final score
POST : perfData - sorted
*/
void insertionSort(LIST *perfData)
{
int curr, walk;
for (curr = 1; curr < perfData->size; curr++)
{
PERFORMER temp = perfData->list[curr];
walk = curr - 1;
while (walk >= 0 && temp.final > perfData->list[walk].final)
{
perfData->list[walk + 1] = perfData->list[walk];
walk--;
}
perfData->list[walk + 1] = temp;
}
}
/* *******************************************************
Displays the error message for the given error code
PRE : errorCode
POST : message displayed
*/
void showErrorMessage(int errorCode, char s[])
{
switch (errorCode)
{
case 0: printf("\"%s\" - Invalid file name ", s);
break;
case -1: printf( "\"%s\" - too big ", s);
break;
case -2: printf( "\"%s\" - not found ", s);
break;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
