N
nkhan
Need some help on this please:
EECE1207 Spring 2005
Computer Assignment # 1
Due Wednesday, March 23rd
The file ‘grades.txt’, as shown below, contains the results of a
true-false exam given to a group of students. You may assume that the
number of students will always be less than 20. The first line contains
the key answer representing the 10 correct answers. Starting from the
second line, each line of the data file contains the student
identification number and the student’s answer to 10 true-false
questions.
Write a program that reads as input the file ‘grades.txt’. The program
should start by readng the correct answer from the first line into a 1-D
character array named correct_answers, then do the following tasks:
Read the students ID numbers into a 1-D integer array named ID, and read
each student’s answers into a 1-D character array containing the answers
for the current student, named current_answers. The number of students,
num_students, should be determined in the process.
While reading the data in (1), compute and store the number of correct
answers for each student in a 1-D integer array named scores.
After reading all the data, determine the average score, AvgScore.
Finally, print a three-column table on the screen that displays the ID
number, the score, and the grade (Pass or Fail) for each student.
The grades should be determined as follows: If the score is larger than
AvgScore then the grade is Pass; otherwise the grade is Fail.
Contents of the file ‘grades.txt’:
FTFFTFFTFT
1080 FTTFTFTTFT
1340 FTFTFTTTFF
1341 FTTFTTTTTT
1401 TTFFTFFTTT
1462 TTFTTTFFTF
1464 FTFFTFFTFF
1465 TTTTTTTTTT
1466 FFFFFFFFFF
1467 FTFTFFTFTF
1468 FFTTFFTTFF
1469 TTTTTFFFFF
1470 FFFFFTTTTT
Note:
Your program must be written so that it could process a file with any
number of students.
Adequate comments should be placed throughout your program to briefly
describe it statements.
This is what I have so far:
#include<stdio.h>
AvgScore(int score[],int i){
int total;
int avg;
int j;
for(total = 0, j = 0; j < i; i++)
{
total += score;
}
avg = total/i;
return avg;
}
int main (void)
{
#define NUMSTUDENTS 20
#define NUMANSWERS 10
int ID[NUMSTUDENTS];
char correct_answers[NUMANSWERS+2];
char current_answers[NUMANSWERS+2];
int i=0;
int score[NUMSTUDENTS];
char *ptr1, *ptr2;
int avg;
FILE* fpi=fopen("grades.txt", "r");
//read correct answers
fscanf(fpi,"%s",&correct_answers);
//check for valid file
if(!fpi) printf("Did not open file");
while( !feof(fpi) ){
// read student-id
fscanf(fpi,"%d", &ID);
// read answers
fscanf(fpi,"%s",current_answers);
i++;
// process data not shown
//i= num of students
}
fclose(fpi);
score[NUMSTUDENTS] = 0;
ptr1 = &correct_answers[0];
ptr2 = ¤t_answers[0];
for (i = 0; i < 10; i++){
if (*ptr1++ == *ptr2++)
score++;
}
avg=AvgScore(score,i);
printf("%d\n",ID);
printf("%d\n",score);
for (score;score<avg;score++){
printf("Fail");}
for (score;score>avg;score++){
printf("Pass");}
return 0;
}
I keep getting a steam!=null error upon trying to execute the file, the
compiler shows no errors or warnings however. Any help would be greatly
appreciated. Thank you.
EECE1207 Spring 2005
Computer Assignment # 1
Due Wednesday, March 23rd
The file ‘grades.txt’, as shown below, contains the results of a
true-false exam given to a group of students. You may assume that the
number of students will always be less than 20. The first line contains
the key answer representing the 10 correct answers. Starting from the
second line, each line of the data file contains the student
identification number and the student’s answer to 10 true-false
questions.
Write a program that reads as input the file ‘grades.txt’. The program
should start by readng the correct answer from the first line into a 1-D
character array named correct_answers, then do the following tasks:
Read the students ID numbers into a 1-D integer array named ID, and read
each student’s answers into a 1-D character array containing the answers
for the current student, named current_answers. The number of students,
num_students, should be determined in the process.
While reading the data in (1), compute and store the number of correct
answers for each student in a 1-D integer array named scores.
After reading all the data, determine the average score, AvgScore.
Finally, print a three-column table on the screen that displays the ID
number, the score, and the grade (Pass or Fail) for each student.
The grades should be determined as follows: If the score is larger than
AvgScore then the grade is Pass; otherwise the grade is Fail.
Contents of the file ‘grades.txt’:
FTFFTFFTFT
1080 FTTFTFTTFT
1340 FTFTFTTTFF
1341 FTTFTTTTTT
1401 TTFFTFFTTT
1462 TTFTTTFFTF
1464 FTFFTFFTFF
1465 TTTTTTTTTT
1466 FFFFFFFFFF
1467 FTFTFFTFTF
1468 FFTTFFTTFF
1469 TTTTTFFFFF
1470 FFFFFTTTTT
Note:
Your program must be written so that it could process a file with any
number of students.
Adequate comments should be placed throughout your program to briefly
describe it statements.
This is what I have so far:
#include<stdio.h>
AvgScore(int score[],int i){
int total;
int avg;
int j;
for(total = 0, j = 0; j < i; i++)
{
total += score;
}
avg = total/i;
return avg;
}
int main (void)
{
#define NUMSTUDENTS 20
#define NUMANSWERS 10
int ID[NUMSTUDENTS];
char correct_answers[NUMANSWERS+2];
char current_answers[NUMANSWERS+2];
int i=0;
int score[NUMSTUDENTS];
char *ptr1, *ptr2;
int avg;
FILE* fpi=fopen("grades.txt", "r");
//read correct answers
fscanf(fpi,"%s",&correct_answers);
//check for valid file
if(!fpi) printf("Did not open file");
while( !feof(fpi) ){
// read student-id
fscanf(fpi,"%d", &ID);
// read answers
fscanf(fpi,"%s",current_answers);
i++;
// process data not shown
//i= num of students
}
fclose(fpi);
score[NUMSTUDENTS] = 0;
ptr1 = &correct_answers[0];
ptr2 = ¤t_answers[0];
for (i = 0; i < 10; i++){
if (*ptr1++ == *ptr2++)
score++;
}
avg=AvgScore(score,i);
printf("%d\n",ID);
printf("%d\n",score);
for (score;score<avg;score++){
printf("Fail");}
for (score;score>avg;score++){
printf("Pass");}
return 0;
}
I keep getting a steam!=null error upon trying to execute the file, the
compiler shows no errors or warnings however. Any help would be greatly
appreciated. Thank you.