Four short programs. All four are about the boundary between text and numbers — the thing Activity 1.9 spent a whole session on.
Three kinds of mistake show up in C#, the same ones from dc-arithmetic. A compiler error stops the program before it ever runs — nothing prints, not even a heading. A runtime error runs until it hits something it cannot recover from, so you get partial output and then a crash. A logic error runs cleanly to the end and gives the wrong answer, which is the one that will cost you marks on an exam.
Two of these four are logic errors. If you find yourself waiting for an error message on those, that is exactly the habit this challenge exists to break.
This should add two numbers the user types. It prints Sum: 127 for 12 and 7.
string a = Console.ReadLine();string b = Console.ReadLine();
Console.WriteLine("Sum: " + a + b);This should show the average of three scores to one decimal place. It refuses to build.
double s1 = double.Parse(Console.ReadLine());double s2 = double.Parse(Console.ReadLine());double s3 = double.Parse(Console.ReadLine());
double average = (s1 + s2 + s3) / 3;Console.WriteLine("Average: " + average.tofixed(1));This should report a total price. For most inputs it works. Typed exactly as shown below, it crashes instead of printing anything.
double price = double.Parse(Console.ReadLine());int qty = int.Parse(Console.ReadLine());
Console.WriteLine("Total: " + (price * qty));Also: The program is correct. The user typed $4.50 at the first prompt. Explain why that crashes the program instead of just producing a wrong number, and say what the program should do differently.
This should show a price with two decimals and then add a shipping charge. The total comes out as 19.995.
double price = 19.99;double shipping = 5;
string shown = price.ToString("F2");Console.WriteLine("Price: " + shown);Console.WriteLine("Total: " + (shown + shipping));For each program, give:
Work them out by reading first. Running the code tells you that something is wrong; it rarely tells you why, and on the exam there is no computer.