0% found this document useful (1 vote)
109 views5 pages

Practical 11 of 2012 Solution

1. The document provides instructions for Practical 11, which is due on May 21, 2012. Students are instructed to upload their solutions to Question 1 and Question 2 to the Learn site before 5pm that day. 2. Question 1 involves writing a program to calculate student marks statistics like average, highest, and lowest marks. It also categorizes students as pass, fail, or distinction and displays the results. 3. Question 2 involves writing a basic calculator program that allows the user to select math operations like addition, subtraction, multiplication, and division to perform calculations on two numbers input by the user.

Uploaded by

Dominic Benedito
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
109 views5 pages

Practical 11 of 2012 Solution

1. The document provides instructions for Practical 11, which is due on May 21, 2012. Students are instructed to upload their solutions to Question 1 and Question 2 to the Learn site before 5pm that day. 2. Question 1 involves writing a program to calculate student marks statistics like average, highest, and lowest marks. It also categorizes students as pass, fail, or distinction and displays the results. 3. Question 2 involves writing a basic calculator program that allows the user to select math operations like addition, subtraction, multiplication, and division to perform calculations on two numbers input by the user.

Uploaded by

Dominic Benedito
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ONT1000 / SDS1000 Week starting 8 May 2012 PRACTICAL 11: DUE: The Program.

cs file containing the solution to Question 1 and Question 2 should be uploaded on the Learn site BEFORE 17:00 on Monday, 21 May 2012. No late submissions will be accepted. Create folders on your H drive to store your practical solutions: In the ONT1000/My Practicals (or SDS1000/My Practicals) folder that you created on your H drive create a folder called Practical 10.

QUESTION 1:
static void Main(string[] args) { Console.Title = "Student Marks"; string studentName = "", subjectCode; int testMark, sumOfMarks = 0, highestMark = 0, lowestMark = 100; double classAverage; int count = 1, failCounter = 0, passCounter = 0, distinctionCounter = 0; string result; string message = "NAME\tMARK\tRESULT\n"; message += "====\t====\t======\n";

subjectCode = GetSubjectCode(); Console.WriteLine();

while (studentName != "-X-") { studentName = GetStudentName(count); if (studentName != "-X-") { testMark = GetStudentMark(studentName); sumOfMarks += testMark; highestMark = Math.Max(highestMark, testMark); lowestMark = Math.Min(lowestMark, testMark); if (testMark < 40) { result = "Fail"; failCounter++; } else if (testMark < 75) { result = "Pass"; passCounter++;

Practical 11

Page 1 of 5

Compiled By: Yiota Moutzouris

ONT1000 / SDS1000 Week starting 8 May 2012


} else { result = "Pass with Distinction"; distinctionCounter++; } message += studentName + "\t" + testMark + "\t" + result + "\n"; count++; // in order to prompt correctly for the student name // (Enter name 1, enter name 2, etc.)

} }

if (count > 1) { classAverage = sumOfMarks / ((double)count - 1); message += "\n"; message += "\nNumber of students:\t\t" + (count - 1) + "\n"; message += "Subject code:\t\t" + subjectCode + "\n\n"; message += "Failed:\t\t\t" + failCounter + "\n"; message += "Passed:\t\t\t" + passCounter + "\n"; message += "Passed with distinction:\t" + distinctionCounter + "\n\n"; message += "Lowest mark:\t\t" + lowestMark + "\n"; message += "Highest mark:\t\t" + highestMark + "\n\n"; message += "Class average:\t\t" + classAverage.ToString("f2");

MessageBox.Show(message, "MARKS FOR " + subjectCode); } else MessageBox.Show("No student results have been captured!");

static string GetSubjectCode() { Console.Write("Please enter the subject code: "); return Console.ReadLine(); }

Practical 11

Page 2 of 5

Compiled By: Yiota Moutzouris

ONT1000 / SDS1000 Week starting 8 May 2012


static string GetStudentName(int count) { Console.Write("Please enter the name of student {0} or Type -X- to stop: ", count); return Console.ReadLine(); }

static int GetStudentMark(string studentName) { int testMark; do { Console.Write("Please enter the test mark for {0}: ", studentName); testMark = int.Parse(Console.ReadLine()); if (testMark < 0 || testMark > 100) Console.WriteLine("INVALID TEST MARK. Please re-enter below."); } while (testMark < 0 || testMark > 100); Console.WriteLine(); return testMark; }

QUESTION 2
static void Main(string[] args) { Console.Title = ""; char menuChoice= ' '; double number1, number2, answer; number1 = GetNumber("Please enter a number: "); number2 = GetNumber("Please enter another number: ");

while (menuChoice != 'x' && menuChoice != 'X') {

menuChoice = GetMenuChoice();

if (menuChoice == 'a' || menuChoice == 'A') { answer = Add(number1, number2); Display(number1, number2, answer, '+'); }

Practical 11

Page 3 of 5

Compiled By: Yiota Moutzouris

ONT1000 / SDS1000 Week starting 8 May 2012


else if (menuChoice == 'd' || menuChoice == { answer = Divide(number1, number2); Display(number1, number2, answer, '/'); } else if (menuChoice == 'm' || menuChoice == { answer = Multiply(number1, number2); Display(number1, number2, answer, '*'); } else if (menuChoice == 'r' || menuChoice == { answer = Remainder(number1, number2); Display(number1, number2, answer, '%'); } else if (menuChoice == 's' || menuChoice == { answer = Subtract(number1, number2); Display(number1, number2, answer, '-'); } } } 'D')

'M')

'R')

'S')

static double GetNumber(string prompt) { Console.Write(prompt); return double.Parse(Console.ReadLine()); } static char GetMenuChoice() { char menuChoice = ' '; bool validChoice = false; while (validChoice == false) { Console.Clear(); Console.WriteLine("MENU"); Console.WriteLine("===="); Console.WriteLine("A) Add"); Console.WriteLine("D) Divide"); Console.WriteLine("M) Multiply"); Console.WriteLine("R) Remainder"); Console.WriteLine("S) Subtract"); Console.WriteLine("X) Exit"); Console.Write("\nPlease enter your choice (A,D,M,R,S, or X): "); menuChoice = char.Parse(Console.ReadLine()); switch(menuChoice) { case 'a': case 'A': case 'd':

Practical 11

Page 4 of 5

Compiled By: Yiota Moutzouris

ONT1000 / SDS1000 Week starting 8 May 2012


case 'D': case 'm': case 'M': case 'r': case 'R': case 's': case 'S': case 'x': case 'X': validChoice = true; break; default : validChoice = false; break; } } return menuChoice; } static double Add(double number1, double number2) { return number1 + number2; } static double Divide(double number1, double number2) { return number1 / number2; } static double Multiply(double number1, double number2) { return number1 * number2; } static double Remainder(double number1, double number2) { return number1 % number2; } static double Subtract(double number1, double number2) { return number1 + number2; } static void Display(double number1, double number2, double answer, char calculation) { Console.Clear(); Console.WriteLine("{0} {1} {2} = {3}", number1, calculation, number2, answer); Console.Write("\nPlease press ENTER to continue..."); Console.ReadLine(); }

Practical 11

Page 5 of 5

Compiled By: Yiota Moutzouris

You might also like