In the previous assignment, we calculated grades based on a memorized overall grade within each of the categories below, as in this example:
In this assignment, we are going to change the specification slightly to make the program a bit smarter. Instead of someone having to remember what their average grade was for each category, we will prompt the user for the number of items within each category (e.g. number of exams, number of labs, etc.), have the user enter individual grades, and have the program calculate the average for the category.
As usual, we will begin by specifying requirements. Whenever required, we will explain how you would approach the solution to the requirement in C#. User responses are shown bold faced.
Instead of bombing out if the weights don’t add up to 100, use a do { ... } while loop (see Miles p. 43-44) to prompt the user again for all of the weights until they do add up to 100. The do { ... } while loop is the right choice here, because you can test all of the weights at the end of the loop, after each time they have been entered in the loop.
Add code to prompt the user for the number of items in each category:
Please enter the number of exams: 3
Instead of prompting the user for an overall average exam grade, use a loop to read one grade at a time. The grades will be added together (on the fly) to give the grade for that category. For example, after you have asked for the number of exams, you’d prompt the user to enter each exam grade and have the program compute the sum. To make sure everyone understands what should be happening, you should also print a running total of the grade category you’re calculating:
Please enter the grade for exam 1: 100Total exam points: 100Please enter the grade for exam 2: 90Total exam points: 190Please enter the grade for exam 3: 80Total exam points: 270Calculated average exam grade = 90.0
Write a function, FindAverage, to do this, and that can be reused for each category. (Since it works for each category, the category name will need to be a parameter to FindAverage.)
Once you have read in the data for each of the items within a category, you’ll basically be able to reuse the code that you developed in the previous assignment to compute the weighted average and print the final letter grade.
This assignment will be the first one where you need to start using functions. Otherwise, you’ll find yourself getting tired within minutes of starting your work.`
You need to have two functions to prompt the user to input integer and double data. We’re going to give you the code for these here and in example PromptUser1.cs. Your job is to copy them into your program and make use of them in your solution. The first function is a supporting function to return a string in response to a prompt. These versions are sufficient for the assignment, though you are welcomed to replace these with more robust versions that get developed in class later:
/** Return a line from the keyboard
* after displaying the prompt. */
static string InputLine(string prompt)
{
Console.Write(prompt);
return Console.ReadLine();
}
/** Return an integer entered from the keyboard
* after displaying the prompt. */
static int InputInt(string prompt)
{
string nStr = InputLine(prompt).Trim(); //Trim removes enclosing blanks
return int.Parse(nStr);
}
/** Return a double entered from the keyboard
* after displaying the prompt. */
static double InputDouble(string prompt)
{
string nStr = InputLine(prompt).Trim(); //Trim removes enclosing blanks
return double.Parse(nStr);
}
[CamelCase] | http://en.wikipedia.org/wiki/CamelCase |
[UPennCSharp] | http://www.cis.upenn.edu/~cis193/csstyle.html |
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:
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:
Just omit the parts about a partner if you do not have one.
Note
Name the log file with the exact file name: “log.txt” and make it a plain text file. You can create it in a program editor or in a fancy document editor. If you use a fancy document editor, be sure to save it an a plain text file, usually indicated by the ”.txt” suffix.