using System; public static class Lab5 { public static void Main() { // declare variables int inpMark; string lastName; char grade = ' '; // enter the student's last name Console.Write("Enter the last name of the student => "); lastName = Console.ReadLine(); // enter (and validate) the mark do { Console.Write("Enter a mark between 0 and 100 => "); inpMark = Convert.ToInt32(Console.ReadLine()); } while (inpMark < 0 || inpMark > 100); // Use the method to convert the mark into a letter grade grade = MarkToGrade(); // print out the results Console.WriteLine("{0}'s mark of {1} converts to a {2}", lastName, inpMark, grade); Console.ReadLine(); } // Method: MarkToGrade // Parameter // mark: call by value parameter to be converted to a letter grade // Returns: the corresponding letter grade public static char MarkToGrade(int mark) { char letterValue; // multi-alternative If statement to determine letter grade if(mark > 0 && mark < 50) letterValue = 'F'; else if(mark >= 50 && mark < 60) letterValue = 'D'; else if(mark >= 60 && mark < 75) letterValue = 'C'; else if(mark >= 75 && mark < 85) letterValue = 'B'; else if(mark >= 85 && mark <= 100) letterValue = 'A'; else { Console.WriteLine("***Error - invalid mark"); letterValue = 'X'; } //return the letter grade back to Main return letterValue; } }
What error message did you receive and why?
Remove the extra line you added in Part (1). Now let’s change the method header to include a call by address (reference) parameter. Change Line 33 to be:
public static void MarkToGrade(int mark, ref char letterValue)Remove Lines 35 (char letterValue;) and 55 (return letterValue;))
Change Line 21 to be:
MarkToGrade(inpMark, ref grade);Run the program with the input of Smith and 98.What is the output?
What do you notice about the output relative to what you saw in Part (b)?
Why did we have to remove Line 35 (try putting it back in)?
What did we have to remove Line 55 (try putting it back in)?
Why did the method call (invocation) in Line 21 have to change?
What error message did you receive and why?
What if we change Line 21 to:
grade = MarkToGrade(inpMark, ref grade);What error message did you receive and why?
Finally, how about if we change Line 21 to:
MarkToGrade(ref grade, inpMark);What error messages did you receive and why?
1)
We recieve the following error because inpMark is not defined within the method
error
CS0103: The name `inpMark' does not exist in the current
context
Compilation failed: 1 error(s), 0 warnings
2)
What is the output?:
Why did we have to remove Line 35 (try putting it back in)? Because we are passing the letterValue as a parameter to the function. we cannot redeclare it in the same context.
What did we have to remove Line 55 (try putting it back in)? we changed the functions' return type to void, so it cannot have a return type
Why did the method call (invocation) in Line 21 have to change? we changed the function header to recieve 2 parameters, so it had to be changed.
3)
What error message did you
receive and why? we recieve the following the error
because MarkToGrade function expects 2 parameters and we only
passed one.
main.cs(21,17): error CS1501: No
overload for method `MarkToGrade' takes `1' arguments
What if we change Line 21 .....What error message did you receive and why? We recieve the following error because the MarkToGrade function is of type void and the variable "grade" is of type char, The conversion between the two types can only be explicit
Cannot implicitly convert type `void' to `char'
Finally, how about if we change Line 21......What error messages did you receive and why? We recive the following error because the function expects the parameters in order int, char. but we have swapped them.
The best overloaded method match for `Lab5.MarkToGrade(int, ref char)' has some invalid arguments
----------------------------------------------------------------------------------------
# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue
Get Answers For Free
Most questions answered within 1 hours.