Question

[15 marks] Suppose that students enrolled in one course are required to take four tests, and...

[15 marks] Suppose that students enrolled in one course are required to take four tests, and each student’s final grade for this course is the average of his/her grades of these four tests. This question asks you to write a program that can be used to compute the lowest final grade, highest final grade and the average final grade.

General Requirements: Use a5q1.c as the name of your C source code file. We will use the following command on bluenose to compile your program:

gcc -std=c99 -o grades a5q1.c

Your program should NOT print anything to prompt for user input. As many numbers have to be read from stdin to test your programs, you are expected to use input redirection to read the input from a file. More precisely, you can run your program using the following command:

./grades < grades.1
In the above command, the file grades.1 is a plain text file of the following format:

(a) The first line has one integer. If this integer is 1, 2, or 3, then you are asked to compute the lowest, highest, or average final grade, respectively.

(b) The second line also has one integer, and it is the number of students.

(c) Starting from the third line, each line stores the grades of one student. More precisely, it contains four integers (separated by spaces) in the range [0,100], which are grades this student obtained in the four required tests.

For example, consider the following input file:

1
3
90 80 77 79
65 78 81 88
92 91 95 93

It means that you are asked to find out the lowest final grade. There are three students. The first student got 90, 80, 77, 79 as grades in his four tests, and so on.

Your program should then compute the lowest, highest or average final grade according to the first integer read, and print the result as a floating point number, keeping two digits after the decimal point. A trailing newline is also printed. For this input, the first student’s final grade is (90 + 80 + 77 + 79)/4 = 78, which is the lowest among the three students. Thus the output is:

78.00

Error Handling: You can assume that user input contains integers only. However, your program should print an appropriate error message and terminate in each of the following cases:

(a) The first number of the input is not 1, 2, or 3 (i.e., an incorrect option is given). (b) The second number is not positive.

2

(c) Each grade in the input is not an integer in the range [0, 100].
If there is more than one problem with user input, your program just has to detect one

of them.

Testing: To test your program automatically, we will use it as a UNIX filter.
For the example above, we will test it using the following command in the directory

containing the executable program (assume that grades.1 is the input file)

  ./grades < grades.1

To help you make sure that the output format of your program is exactly what this questions asks for, several files are provided in the following folder on bluenose to show how exactly your program will be automatically tested:

  /users/faculty/prof2132/public/a5test/

In this folder, open the file a5q1test to see the commands used to test the program with three test cases. The output files, generated using output redirection, are also given.

To check whether the output of your program on any test case is correct, redirect your output into a file, and use the UNIX utility diff (learned in Lab 3) to compare your output file with the output file in the above folder, to see whether they are identical.

Since these output files are given, we will apply a penalty to any program that does not strictly meet the requirement on output format. This includes the case in which your output has extra spaces, or has a missing newline at the end.

We will use different test cases to test your program thoroughly. Therefore, construct a number of test cases to test your program thoroughly before submitting it.

Hint: You are not required to organize your program using functions yet for this assignment (it will be required for Assignment 6). However, if you do use functions, then, when handling errors, you may need to terminate the program when a function that is not main is being executed. Read page 203 of the text book to find out how to use the exit function to do this.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question.

1. Save the code in a file name a5q1.c

2. Compile using
gcc -std=c99 -o grades a5q1.c

3. Save the following input lines in a file named grade1.txt
1
3
90 80 77 79
65 78 81 88
92 91 95 93

4. Run the program using input redirection from file
./grades < grades1.txt


Please do rate the answer if it helped. Thank you.


#include <stdio.h>
int main()
{
  
int statsChoice;
int numStudents;
float grade, final, lowest = -1, highest = 0, average = 0;
int error = 0;
int i, j;
  
//get the choice 1, 2, 3 which calcualation needs to be done lowest, highest or average
scanf("%d", &statsChoice);
if(statsChoice < 1 || statsChoice > 1)
{
printf("Invalid choice! Should be 1, 2 or 3\n");
}
else
{
//get the number of students
scanf("%d", &numStudents);
if(numStudents <= 0)
{
printf("The number of students should be positive\n");
}
else
{
for(i = 1; !error && i <= numStudents; i++) //for each student
{
final = 0;
for(j = 1; !error && j <= 4; j++) //get 4 grades
{
scanf("%f", &grade);
if(grade < 0 || grade > 100)
{
printf("Grades should be in the range of [0,100]\n");
error = 1;
}
else
{
final += grade;
}
}
  
final /= 4;
  
switch(statsChoice)
{
case 1: //find lowest grade
if(lowest == -1 || final < lowest) //if lowest is not yet set or this new grade is lesser
lowest = final;
break;
case 2: //find highest grade
if(final > highest) //this new grade is higher
highest = final;
break;
case 3:
average += final;
break;

}
}
  
  
//display the output
  
if(statsChoice == 1)
printf("%.2f\n", lowest);
else if(statsChoice == 2)
printf("%.2f\n", highest);
else if(statsChoice == 3) //average calculation?
{
average /= numStudents;
printf("%.2f\n", average);
}
}
}
return 0;
}

--------output--------

amoeba-2:Test1 rajis gcc -std-c99 -o grades a5q1.c amoeba-2:Test1 rajis ./grades < grades1.txt 78.00 amoeba-2:Testl rajis|

Add a comment
Know the answer?
Add Answer to:
[15 marks] Suppose that students enrolled in one course are required to take four tests, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • C++: Create a grade book program that includes a class of up to 20 students each...

    C++: Create a grade book program that includes a class of up to 20 students each with 5 test grades (4 tests plus a Final). The sample gradebook input file (CSCI1306.txt) is attached. The students’ grades should be kept in an array. Once all students and their test scores are read in, calculate each student’s average (4 tests plus Final counts double) and letter grade for the class. The output of this program is a tabular grade report that is...

  • Write the definition of the class Tests such that an object of this class can store...

    Write the definition of the class Tests such that an object of this class can store a student's first name, last name, five test scores, average test score, and grade. (Use an array to store the test scores.) Add constructors and methods to manipulate data stored in an object. Among other things, your class must contain methods to calculate test averages, return test averages, calculate grades, return grades, and modify individual test scores. The method toString must return test data...

  • Write a C program to compute average grades for a course. The course records are in...

    Write a C 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...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

  • use  JOptionPane to display output Can someone please help write a program in Java using loops, not...

    use  JOptionPane to display output Can someone please help write a program in Java using loops, not HashMap Test 1 Grades After the class complete the first exam, I saved all of the grades in a text file called test1.txt. Your job is to do some analysis on the grades for me. Input: There will not be any input for this program. This is called a batch program because there will be no user interaction other than to run the program....

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

  • C Programming QUESTION 9 Write a program that prompts for and reads four integer input values,...

    C Programming QUESTION 9 Write a program that prompts for and reads four integer input values, then a single character command. Submit your program as a .c file named midterm_prog2.c. Note that, similarly to your programming assignments, some percentage of your grade will depend on proper programming style. Your program will calculate and print a new value based on the command that's entered, as follows: 'A' or 'a': Calculate the average of the four input values 'S' or 's': Calculate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT