Create a program file grade_calc.cs for this assignment. You are going to be putting together your first programming assignment where you will be taking the various concepts we have learned thus far from class and to put together your first meaningful program on your own.
This program will incorporate the following elements:
As weve mentioned earlier in class, our focus is going to be on learning how to write computer programs that start with a Main() function and perhaps use other functions as needed to g e t a p a r t i c u l a r j o b d o n e . Eventually, we will be incorporating more and more advanced elements, such as classes and objects. For now, we would like you to organize your program according to the guidelines set forth here.
Our first program is based on a common task that every course professor/instructor needs to do: make grades. In any given course, there is a g r a d i n g s c a l e and a set of c a t e g o r i e s .
Here is sample output from two runs of the program. The only data entered by the user are show in boldface for illustration here.
One successful run with the data used above:
Enter weights for each part as an integer
percentage of the final grade:
Exams: 40
Labs: 15
Homework: 15
Project: 20
Participation: 10
Enter decimal numbers for the averages in each part:
Exams: 50 Labs: 100
Homework: 100
Project: 100
Participation: 5
Your grade is 70.5%
Your letter grade is C-.
A run with bad weights:
Enter weights for each part as an integer
percentage of the final grade:
Exams: 30
Labs: 10
Homework: 10
Project: 10
Participation: 10
Your weights add to 70, not 100.
This grading program is ending.
Make your program file have the name grade_calc.cs .
This is based on the idea of Dr. Thiruvathukals own legendary course syllabus. Were going to start by assuming that there is a fixed set of categories. As an example we assume Dr. Thiruvathukals categories.
In the example below we work out for Dr. Thiruvathukals weights in each category, though your program should prompt the user for these integer percentages:
Your program will prompt the user for each the weights for each of the categories. These weights will be entered as integers, which must add up to 100.
If the weights do not add up to 100, print a message and end the program. You can use an if - else construction here. An alternative is an if statement to test for a bad sum. In the block of statements that go with the if statement, you can put not only the message to the user, but also a statement:
return;
Recall that a function ends when a return statement is reached. You may not have heard that this can also be used with a void function. In a void function there is no return value in the return statement.
Assuming the weights add to 100, then we will use these weights to compute your grade as a double , which gives you the best precision when it comes to floating-point arithmetic.
Well talk in class about why we want the weights to be integers. Because floating-point mathematics is not 100% precise, it is important that we have an accurate way to know that the weights r e a l l y a d d u p to 100. The only way to be assured of this is to use i n t e g e r s . We will actually use floating-point calculations to compute the grade, because we have a certain tolerance for errors at this stage. (This is a fairly advanced topic that is covered extensively in courses like COMP 264/Systems Programming and even more advanced courses like Numerical Analysis, Comp 308.)
We are going to pretend that we already know our score (as a percentage) for each one of these categories, so it will be fairly simple to compute the grade.
For each category, you will define a weight (int) and a score (double). Then you will sum up the weight * score and divide by 100.0 (to get a double-precision floating-point result).
This is best illustrated by example.
George is a student in COMP 170. He has the following averages for each category to date:
The following session with the csharp interpreter shows the how you would declare all of the needed variables and the calculation to be performed:
csharp> int exam_weight = 40;
csharp> int lab_weight = 15;
csharp> int hw_weight = 15;
csharp> int project_weight = 20;
csharp> int participation_weight = 10;
csharp> double exam_grade = 50.0;
csharp> double lab_grade = 100;
csharp> double homework_grade = 100;
csharp> double project_grade = 100;
csharp> double participation_grade = 5;
This is intended only to be as an example though. Your program must ask the user to enter each of these variables.
Once we have all of the weights and scores entered, we can calculate the grade as follows. This is a long expression: It is continued on multiple lines. Recall all the > symbols are csharp prompts are not part of the expression:
csharp> double grade = (exam_weight * exam_grade +
> homework_weight* homework_grade +
> lab_weight * lab_grade + project_weight * project_grade +
> participation_weight * participation_grade) / 100.0;
Then you can display the grade as a percentage:
csharp> Console.WriteLine("Your grade is {0}%", grade);
Your grade is 70.5%
Now for the fun part. We will use if statements to print the letter grade. You will actually need to use multiple if statements to test the conditions. A way of thinking of how you would write the logic for determining your grade is similar to how you tend to think of the b e s t grade you can h o p e f o r in any given class. (We know that we used to do this as students.)
Here is the thought process:
Well come to see how l o g i c plays a major role in computer sciencesometimes even more of a role than other mathematical aspects. In this particular program, however, we see a bit of the best of both worlds. Were doing a r i t h m e t i c calculations to c o m p u t e the grade. But we are using l o g i c to determine the grade in the cold reality that we all know and love: the bottom-line grade.
This assignment can be started after the data chapter, because you can do most all of it with tools learned so far. Add the parts with if statements when you have been introduced to if statements. (Initially be sure to use data that makes the weights actually add up to 100.)
You should be able to write the program more concisely and readably if you use functions developed in class for the prompting user input.