Exercise 3.4

You should write a program to display the grade of a student with a particular mark in an exam. The mark is always a whole number – no fractional marks are allowed. The user will enter the mark and the program should display the grade. The grades are: See image.

If the user enters a mark that is below 0 or above 100, the program should display an appropriate error message.

Exercise 4.5

Write a program that asks the user to enter a series of non-zero integers, one at a time. The program should keep asking the user for a number until they enter a value of 0. The program should then display the largest of the numbers that have been entered. Be aware that the user could enter all negative integers and your program has to be able to handle this.

Exercise 6.2

This program should simulate tossing a coin, i.e. it should display “heads” or “tails”. It should then ask the user if they want to toss the coin again. If they enter “y” or “Y”, the coin should be tossed again. If any other value is entered, the program should stop. For example, a run of your program might look like the following: See image.

To do this exercise, you need to produce a random number of either 1 or 2. Treat 1 as heads and 2 as tails.

Exercise 8.3

Write a program that asks the user to enter an item’s wholesale cost and its markup percentage. It should then display the retail price for the item. For example:

• If the wholesale cost is £3.00 and the markup percentage is 100%, the retail price will be £6.00
• If the wholesale cost is £5.00 and the markup percentage is 50%, the retail price will be £7.50.

The program should contain a method named CalculateRetailPrice that takes the wholesale cost and markup percentage as arguments and returns the retail price.

Exercise 9.4

Write a program that randomly generates 100 integer values in the range 1 to 50. Your program should display how many numbers of the numbers generated occurred in the range 1..10, 11.. 20, etc. For example, your program might display: See image.

The easiest way to do this is to use if statements to determine if the number that is entered is within a particular range. This is acceptable for a first attempt. However, you should then try modifying your program so that you can easily change the number of integers generated and the ranges of valid numbers (for example, the ranges could be changed to be 1..6, 7..12, 13..31, 32..50 or any other set of ranges). To do this, you would use two arrays that store the upper and lower bound of each of the ranges.

Exercise 10.5

Write an application that reads a text file. The program should read the entire file and, at the end, display a count of how many words occurred in the file that were 1, 2, 3, 4 or 5 letters long and how many words were longer than 5 letters. For example, if the text file contains the lines: See image.

The output from the program would be; See image.

Punctuation characters are not treated as special characters for this exercise. Hint. You might find the Split method from the string class useful for this exercise. This method takes as a parameter a character array containing a set of separation characters. It returns a string array containing the components of the line split at the separation characters. For example,

string[] stringBits = myString.Split(new char[] { ',' });

If myString contains the following before this line of code:

aaaaa,bbbbb,cc,dddd

Then the string array stringBits will contain the following after the line has been executed: See image.