Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct {
char name[100];
int scores[7];
}STUDENT;
// function declarations
void getFileName(char *filename);
int howManyLines(char *filename);
void readFileIntoArray(char *filename, STUDENT students[], int *size);
void getStats(STUDENT students[], int size);
int main(void) {
char filename[30];
STUDENT *students;
int size;
getFileName(filename); // finished
students = malloc( (howManyLines(filename)+1) * sizeof(STUDENT)); // finished
readFileIntoArray(filename, students, &size); // i have to finish this function
getStats(students, size); // i have to finish this function
return EXIT_SUCCESS;
}
/*
* This function goes through the student array and prints out who scored highest
* on each of the seven assignments. If there is a tie, print out all the students
* who tied for the highest score.
*
* size is the size of the array.
*
* You can use fmax in the math.h library if you want.
*/
// this function i do not have a clue on how to do this, but know what i want from it.... i need help
void getStats(STUDENT students[], int size){
int i, hold_score, same_score, x;
char hold_name, same_name1, same_name2;
i = 0;
for(i = 0; i < size; i++)
{
if(students[i].scores > students[i-1].scores)
{
x = i;
hold_score = students[i].scores;
strcpy(hold_name, students[i].name);
printf("The student named, %s, had the highest score on assignment%d, which was a %d", students[i].name, x, hold_score);
}
else if( (hold_score == students[i-1].scores) && (hold_name != students[i-1].name) )
{
same_score = students[i].scores;
strcpy(same_name1, hold_name);
strcpy(same_name2, students[i-1].name);
printf("Both these students, %s and %s, both had the highest score on assignment%d, which was a %d", hold_name, same_name1, x, same_score);
}
}
}
/*
* opens file named filename
*
* read from the file while fscanf returns something != EOF <- end of file
*
* for each struct in the array:
* reads students names into struct
* reads the 7 scores into struct
* counts the structs and puts the result into *size.
*
* The lines in the file come in this form:
* Wallace, Christine (userid) | studentid int1 int2 int3 int4 int5 int6 int7
*
* for extra credit your code should also handle lines like this:
* Mc Donald, Ronald (userid) | studentid int1 int2 int3 int4 int5 int6 int7
*
* In other words, some last names may have 2 strings. But they will always end in ,
*
* In either case, this function concatenates lastName, firstName (use strcat) and
* stores the resulting string in the name member of the STUDENT struct.
* Throw away the user id, |, and the student id. Don't store them anywhere in the struct.
*/
// this function i have no idea how to do it...., but i know what i want...
void readFileIntoArray(char *filename, STUDENT students[], int *size){
FILE *fp;
char bam[100];
int i, hold_score, same_score, x;
char hold_name, same_name1, same_name2;
char first, last;
fp = fopen(filename, "r");
do
{
fscanf(fp, "%s", )
}
while(fscanf(fp, "%s", bam) != EOF);
{
printf("%s", bam);
fflush(stdout);
}
}
/*
* Gets a valid filename from the user. A valid filename ends in .xls
* filename will point to the valid filename.
*/
void getFileName(char *filename){
int len;
do {
printf("Where are the grades? Enter a filename that ends with .xls ");
fflush(stdout);
scanf("%s", filename);
len = strlen(filename);
}
while(filename[len-1] != 's' || filename[len-2] != 'l' ||
filename[len-3] != 'x' || filename[len-4] != '.');
}
/*
* Counts the newlines but consecutive newlines will not be counted.
*/
int howManyLines(char *filename){
FILE *fp;
char c;
int count=0, newLineflag=0;
fp = fopen(filename, "r");
while(fscanf(fp, "%c", &c) != EOF){
if(c=='\n' && !newLineflag){
count++;
newLineflag = 1;
}
else if (c != '\n')
newLineflag = 0;
}
fclose(fp);
return count;
}