Calculate the cost of a carpet.
Select the button below to open the C# program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
Casts (converts) the string y into a 32-bit integer x. Integers are whole numbers, positive, negative or zero.
Converts y into a double x. Doubles are also called floats, real numbers or decimals.
Assigns x to be an input from the keyboard.
Returns the value x from a method so that it can be used elsewhere in the program.
+ add
- subtract
* multiply
/ divide
he concatenator '+' joins strings and variables into a single output. E.g. Console.WriteLine("Hello " + "World");
Questions to think about with this program to check your understanding:
Explain the purpose of the Convert.ToInt32 command in lines 25, 28 and 31.
Convert.ToInt32 is a function that converts the input from the keyboard that will always be text, known as a "string" into a whole number, known as an "integer" so that it can be used for mathematical calculations. This is known as "casting".
Explain why a subprogram was used in lines 10-16 instead of writing the code from line 32 onwards.
Breaking a program down into subprograms is called decomposition. It makes it easier to write different parts of the program a step at a time. It is easier to read the code. It allows a section of code to be resused more easily.
The cost of the grippers are 50p per metre around the perimeter of the room which is why we can add the length to the width to calculate the total price.
Change the program below so that it:
Enter the width of the room to nearest meter: 2
Enter the length of the room to nearest meter: 3
Enter the price of the carpet per m2: 2
The total cost is: £ 79.0
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
Width of the room: 2
Length of the room: 3
Price of the carpet: 2
The total cost is: £ 79.0