Docsity

Homework Solution for CpSc 111 - Semester Average Calculation - Prof. Alan W. Madison, Assignments of Computer Science

Toggle sidebar
bg1
CpSc 111 -- Homework 1
Due: Monday, September 14 (11:59 PM)
The problem
Write a program that will input 5 test grades, and then the final exam grade, and then compute
the semester numerical average based on the criteria that the final average is computed using the
test grade average for 60% of the grade, and the final exam average as 40% of the grade. Each of
the 5 test grades counts equally. The program then prints the final letter grade based on the
standard grading scale (90-100 A, 80-less than 90 B, etc.).
And example of a dialogue with the program is (example user input is in red):
./hw2
Enter 5 test grades: 80 81 82.5 83.5 84
Enter exam grade: 95.5
Numerical average = 87.519997
Final grade is B
Requirements
Your program must satisfy the following requirements:
1. the program must prompt the user for input as illustrated above,
2. all input and results are of type float,
3. if the user enters an invalid number, the program should print "Invalid input"
and exit (see hints below),
4. your submitted source when compiled using the -Wall option must not generate
error or warning messages.
Hints
1. Conditional statements can be used to determine the final letter grade.
2. the scanf() function returns the number of values it successfully read. If you ask
to read 5 values, then if the scanf() is successful it should return 5. Anything
less means an error occured.
So you can write a statement like:
int code;
float x, y;
...
code = scanf("%f %f", &x, &y);
pf3

Partial preview of the text

Download Homework Solution for CpSc 111 - Semester Average Calculation - Prof. Alan W. Madison and more Assignments Computer Science in PDF only on Docsity!

CpSc 111 -- Homework 1

Due: Monday, September 14 (11:59 PM)

The problem

Write a program that will input 5 test grades, and then the final exam grade, and then compute

the semester numerical average based on the criteria that the final average is computed using the

test grade average for 60% of the grade, and the final exam average as 40% of the grade. Each of

the 5 test grades counts equally. The program then prints the final letter grade based on the

standard grading scale (90-100 A, 80-less than 90 B, etc.).

And example of a dialogue with the program is (example user input is in red):

./hw Enter 5 test grades: 80 81 82.5 83.5 84 Enter exam grade: 95. Numerical average = 87. Final grade is B

Requirements

Your program must satisfy the following requirements:

1. the program must prompt the user for input as illustrated above,

2. all input and results are of type float,

3. if the user enters an invalid number, the program should print "Invalid input"

and exit (see hints below),

4. your submitted source when compiled using the -Wall option must not generate

error or warning messages.

Hints

1. Conditional statements can be used to determine the final letter grade.

2. the scanf() function returns the number of values it successfully read. If you ask

to read 5 values, then if the scanf() is successful it should return 5. Anything

less means an error occured.

So you can write a statement like:

int code; float x, y; ... code = scanf("%f %f", &x, &y);

If the scanf() successfully reads 2 floating point numbers, then code will be set

to 2. If you were to enter "cat dog", which are obviously not numbers, code

would be set to 0.

You can use the return value (in this case the value "code" was set to), to test

whether the scanf() was successful or not. If the return value is not what you

expected, print the error message "Invalid input" and then execute the "exit(0);"

command.

Submission

We use the "handin" command to turn in lecture homework (NOT sendlab).

To submit your work your code MUST be in a file in your department account. If you

did your work on your own system you will need to upload your code to a department

machine (e.g. using "sftp").

To submit this homework enter on a department machine the command:

handin.111.305 2 hw2.c

If the submission is successful you will receive a message indicating that the file has been

submitted. You will also receive an email. If you see that 0 bytes were submitted, something

went wrong and you should try again. It is your responsibility to make sure that assignments are

successfully submitted and that they are submitted to the right course and section.

/** CpSc 111 -- sample solution to homework 2 */ #include <stdio.h> #include <stdlib.h> int main() { float grade1, grade2, grade3, grade4, grade5, exam; float final; int readcnt; / Input the 5 test gradds / printf("Enter 5 grades: "); readcnt = scanf("%f %f %f %f %f", &grade1, &grade2, &grade3, &grade4, &grade5); / Test for successfule read of the test grades / if (readcnt != 5) { printf("Error: only %d grades read\n", readcnt); exit(0); } / Input exam grade / printf("Enter exam grade:"); readcnt = scanf("%f", &exam); / Test for successful read of the exam grade */