# Grade Calculation in C#

Program Summary

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 grading scale and a set of categories.

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.

18.5.2. Details

Make your program file have the name grade_calc.cs.

This is based on the idea of Dr. Thiruvathukal's own legendary course syllabus. We're going to start by assuming that there is a fixed set of categories. As an example we assume Dr. Thiruvathukal's categories.

In the example below we work out for Dr. Thiruvathukal's weights in each category, though your program should prompt the user for these integer percentages:

exams - 40% (integer weight is 40)

labs - 15% (weight 15)

homework - 15% (weight 15)

project - 20% (weight 20)

participation - 10% (weight 10)

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.

We'll 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 really add up to 100. The only way to be assured of this is to use integers. 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:

exams: 50%

labs: 100%

homework: 100%

project: 100%

participation: 5%

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 best grade you can hope for in any given class. (We know that we used to do this as students.)

Here is the thought process:

If my grade is 93 (93.0) or higher, I'm getting an A.

If my grade is 90 or higher (but less than 93), I am getting an A-.

If my grade is 87 or higher (but less than 90), I am getting a B+.

And so on...

Finally, if I am less than 60, I am unlikely to pass.

We'll come to see how logic plays a major role in computer science-sometimes even more of a role than other mathematical aspects. In this particular program, however, we see a bit of the best of both worlds. We're doing arithmetic calculations to compute the grade. But we are using logic 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.

18.5.3. Grading Rubric

Warning

As a general rule, we expect programs to be complete, compile correctly, run, and be thoroughly tested. We are able to grade an incomplete program but will only give at most 10/25 for effort. Instead of submitting something incomplete, you are encouraged to complete your program and submit it per the late policy. Start early and get help!

25 point assignment broken down as follows:

Enter weights, with prompts [3]

End if the weights do not add to 100: [5]

Enter grades, with prompts: [3]

Calculate the numerical average and display with a label: [5]

Calculate the letter grade and display witha label: [5]

Use formatting standards for indentation: [4]

Sequential statements at the same level of indentation

Blocks of statements inside of braces indented

Closing brace for a statement block always lining up with the heading before the start of the block.

18.5.4. Logs and Partners

You may work with a partner, following good pair-programming practice, sharing responsibility for all parts.

Only one of a pair needs to submit the actual programming assignment. However both students, independently, should write and include a log in their Homework submission. Students working alone should also submit a log, with fewer parts.

Each individual's log should indicate each of the following clearly:

Your name and who your partner is (if you have one)

Your approximate total number of hours working on the homework

Some comment about how it went - what was hard ...

An assessment of your contribution (if you have a partner)

An assessment of your partner's contribution (if you have a partner).

Just omit the parts about a partner if you do not have one.

https://brainmass.com/computer-science/c-sharp/grade-calculation-624418

#### Solution Preview

Attached is the program that'll help you do your task. It has a class called gradeCalculation containing a "main" function. It asks the users to input the weights and checks whether the weights sum up to 100. IF not, the program finishes, otherwise it continues to ask for the grades. It then calculates the percentage and assigns a letter grade using if...else statements.

Please feel free to modify the program as per your requirements. You may also create separate functions to input weights and grades from the user, and to do the calculation for computing the grade. Feel free to let me know if you require help with that as well. Currently, everything is done under the main function to make it simple for you to understand.

I am also attaching the main.cs file as well as pasting the code here. You can compile and run the code either in C# compiler/editor on your machine or use freely available online compilers.

Best,

using System.IO;

using System;

class gradeCalculation

{

static void Main()

{

Console.WriteLine("-------------------");

...

#### Solution Summary

Easy to understand C# code for calculating exam grades. Inline comments are not included as the code is self-explanatory.