Write a program to compute average grades for a course. The course records are in a single file and are organized according to the following format: each line contains a student's first name, then one space, then the student's last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program will read data from this file and write its output to a second file. The date in the output file will be nearly the same as the data in the input file except that you will print the names as last-name, first-name; each quiz score, and there will be one additional number at the end of each line: the average of the student's ten quiz scores. Both files are parameters. You can access the name of the input file with argv[1]. and the name of the output file with argv[2]. The output file must be formatted as described below: 1. First and last names appear togeter in a left-justified column that is 20 characters wide where the last name comes first and a space and then the first name. Read each name separately and then put them together into a larger correctly formatted string before trying to output them (you can use the concatenate two strings example in class as a reference). 2. Each quiz score should be listed in a right-justified column that is 4 characters wide. Note that if a student has fewer than 10 scores (they have missed one or more of the quizzes), your program will need to display the missing score(s) using 0 for each one. 3. The average should appear in its own column that is 10 characters wide. Note that if a student has fewer than 10 scores, the average is still the sum of the quiz scores divided by 10.
Description
Write a program to compute average grades for a course. The course records are in a single file and are organized according to the following format: each line contains a student's first name, then one space, then the student's last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program will read data from this file and write its output to a second file. The date in the output file will be nearly the same as the data in the input file except that you will print the names as last-name, first-name; each quiz score, and there will be one additional number at the end of each line:
the average of the student's ten quiz scores.
Both files are parameters. You can access the name of the input file with argv[1]. and the name of the output file with argv[2].
The output file must be formatted as described below:
1. First and last names appear togeter in a left-justified column that is 20 characters wide where the last name comes first and a space and then the first name. Read each name separately and then put them together into a larger correctly formatted string before
trying to output them (you can use the concatenate two strings example in class as a reference).
2. Each quiz score should be listed in a right-justified column that is 4 characters wide. Note that if a student has fewer than 10 scores (they have missed one or more of the quizzes), your program will need to display the missing score(s) using 0 for each one.
3. The average should appear in its own column that is 10 characters wide. Note that if a student has fewer than 10 scores, the average is still the sum of the quiz scores divided by 10.
4. The average should be computed with an accuracy of two decimal places.
You should use at least two functions that include FILE handles in their parameter lists.(These functions may have other parameters as well.) Suggestion: One very useful strategy is to write a function that, given a FILE pointer, will extract out exactly one person's name and write it to the out put file. Write another function that, given a FILE pointer will extract out one person's quiz scores and list them in the output file along with the average score. Then use these two functions in a loop that will keep doing this until there is no more data in the input file. The feof function may be helpful for determining if you have reached the end of the input file
You may not use %s together with scanf (or fscanf) or gets, which are not safe as we mentioned in class. Don't use any library functions related to c-strings, e.g. strcpy, strlen, atoi,itoa, for the purpose of practice. You don't need to and should not use any non-string arrays.
Some test cases you may want to consider are the following:
• What if the input file is empty?
• What if a student does not have any quiz grades at all?
• What if multiple students in a row don't have any quiz grades?
• What if the last record in the file does not have a new line after it but rather ends with end of file?
• What if there are extra new lines at the end of the file?
When you run the program, because the output is written to a file, don't be surprised to see nothing in the output dialog. In order to see your results, you may use the technique that we learned in class to assign stout to your output file pointer temporarily so that you can check the result. After you finish debugging, change your code back.
A sample file, quiz_test. txt is available for download. The input file is shown below for your reference. This is NOT the file that will test your program. Don't assume that the input file will be exactly like this. Your program should be able to handle arbitrary numbers of records and you should consider all of the test cases listed above (or more).
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images