Code:
/*
Dang Vo
Lab 2
[email protected]
*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
#define LEN 20
#define NUM_JUDGE 5
#define INCLUDE_JUDGE 3
int getData(FILE* spData, char name[][LEN], float difficulty[], float score[][NUM_JUDGE]);
void BubbleSort(float score[][NUM_JUDGE], int numDiver);
void Caltotal(float total[],float difficulty[], float score[][NUM_JUDGE]);
void printData(float total[], char name[][LEN], float score[][NUM_JUDGE], int size);
void printHeader();
int main(void)
{
// Global Declarations
FILE*spData;
char name[MAX][LEN];
float difficulty[MAX];
float score[MAX][NUM_JUDGE];
float total[MAX];
int numDiver = 0;
int size =4;
//statement
if((spData=fopen("DIV1.txt","r"))== NULL)
{
printf("\n Error opennning file!\n");
exit(100);
}
else
{
printf("opened file!\n");
}
// function calls
numDiver = getData(spData, name, difficulty, score);
Caltotal(total,difficulty, score);
printHeader();
printData(total, name, score, numDiver);
fclose(spData);
int i;
scanf("%d", &i);
return 0;
}
/*===============================getdata=====================================
Pre: SpData is oppened,
Post:
*/
int getData(FILE* spData, char name[][LEN], float difficulty[], float score[][NUM_JUDGE])
{
int i=0;
int j=0;
while (i < MAX && (fscanf(spData,"%c", &name[i][0]) != EOF))
{
for(j = 1; j < LEN; j++)
{
fscanf(spData,"%c", &name[i][j]);
}
fscanf(spData,"%f", &difficulty[i]);
for(j = 0; j < NUM_JUDGE; j++)
{
fscanf(spData,"%f\n", &score[i][j]);
}
i++;
}
return i;
BubbleSort(score, i);
}
/*===============================BubbleSort==================================
Pre:
Post:
*/
void BubbleSort(float score[][NUM_JUDGE], int size)
{
int current;
float temp;
int walk;
for(current = 0; current < size; current++)
{
for(walk = size; walk >= current; walk--)
if(score[current][walk] < score[current][walk-1])
{
temp = score[current][walk];
score[current][walk] = score[current][walk-1];
score[current][walk-1] = temp;
}
}
return;
}
/*===============================Caltotal======================================
Pre:
Post:
*/
void Caltotal(float total[],float difficulty[], float score[][NUM_JUDGE])
{
// Local Declarations
int i;
int j;
float temp = 0;
for(i = 0; i < MAX; i++)
for(j =0; j < INCLUDE_JUDGE; j++)
{
temp += score[i][j+1];
}
total[i] = temp * difficulty[i] / 3;
return;
}
/*===============================printHeader======================================
Pre:
Post:
*/
void printHeader()
{
// Local Declaration
FILE*fpOut;
// Local Statments
if(!(fpOut = fopen("output.txt", "w")))
printf("Error writing output.txt"); //checks to see if output.txt opens
fprintf(fpOut, "Dang Khoa Vo [email protected] Lab#2\n\n");
fprintf(fpOut, "NAME Scores Total Score\n");
fprintf(fpOut, "==================== === === === ===========\n");
fclose(fpOut);
return;
}
/*===============================PrintData=====================================
Pre:
Post:
*/
void printData(float total[], char name[][LEN], float score[][NUM_JUDGE], int numDiver)
{
// Local Declaration
FILE*fpOut;
int i,n;
// Local Statments
if(!(fpOut = fopen("output.txt", "a")))
printf("Error writing output.txt"); //checks to see if output.txt opens
for (i=0; i< numDiver;i++)
{
for(n = 0; n < LEN; n++)
fprintf(fpOut, "%c", name[i][n]);
for(n = 0; n < 3; n++)
fprintf(fpOut, " %3.1f", score[i][n]);
fprintf(fpOut, " %5.2f", total[i]);
fprintf(fpOut, "\n");
}
fprintf(fpOut, "\n");
fclose(fpOut);
return;
}
so i wanted to sort the last 5 number of each row of the input data from the attach file