<mosaic.cnfolio.com>

C programming refresher course



This website contains the study notes, practice questions and assessment questions for the summer C programming course. The practice questions are not marked, they are to help you put the concepts from the study notes into practice with writing C programs.

There are 2 assessed coursework questions highlighted in yellow at the bottom of this page. Your submission for those 2 questions will be marked as your assessment for this module. You must save at least one version of your programs for each question by the due date. If you saved multiple versions, then, your last saved version will be considered as your final submission.

The assessment is also submitted online at this website. In order to compile and save your programs, you need to login to the website.



The online compiler


Please login to save your program.



Notes about C programming





Practice question 1


Type the program below into the first textbox of the online compiler.
#include <stdio.h>

int main( void )
{
   int number1, number2;

   scanf( "%d %d", &number1, &number2 );
   printf( "The sum is %d", number1 + number2 );
}

Type the text below into the second textbox of the online compiler.
4 5

Click the Run program button to compile the source code, run the program and display the output in the third box of the online compiler.

What is displayed as the output? Why?

Click the Save your program button to save your work.



Practice question 2


  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    printf( "Hello world" )
  6. }

The program above is missing a semicolon at the end of line 5. Read the error message from the compiler. Look for:

Change the source code to fix the error. Test your program and save your source code.



Practice question 3


  1. #include <stdio.h>
  2.  
  3. int main( void
  4. {
  5.    printf( "Hello world" );
  6. }

The program above is missing a parenthesis at the end of line 3. Read the error message from the compiler. Look for:

Change the source code to fix the error. Test your program and save your source code.



Practice question 4


  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    print( "Hello world" );
  6. }

The program above is using a print() function in line 5, which does not exist. Read the error message from the compiler. Look for:

Change the source code to fix the error. Test your program and save your source code.



Syntax errors are caused by mistakes with the source code. The compiler will not run a program with syntax errors. Instead, it will try to display error messages as close as possible to the source of the syntax errors.



Practice question 5


  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    double furlong = 50;
  6.    double kilometers = furlong * 2.011680;
  7.    
  8.    printf( "%.0lf furlongs is equivalent to %.2lf kilometers", furlong, kilometers );
  9. }

The program above will compile and run successfully. But, the output is incorrect because there is a logic error in the source code.

Read this Wikipedia entry to find the correct conversion rate and fix the program:

Change the source code to fix the error. Test your program and save your source code.



Logic errors are caused by mistakes in the algorithm or steps implemented by the programmer. The program compiles and runs, but, the output or functions do not work as expected. These are often the most difficult to find and fix.



Use software development best practices





Practice question 6


Write a program that reads one input number and displays an output message to indicate whether it is an odd or even number.

For example, the following input:
36

Must produce this output:
36 is an even number

For example, the following input:
93

Must produce this output:
93 is an odd number

Test your program and save your source code.



Practice question 7


Write a program that reads 5 input integers and displays a message to indicate whether or not the numbers form a sequence that increases by one in order from the first to the fifth number.

For example, the following input:
1 2 3 4 5

Must produce this output:
The inputs form a sequence!

For example, the following input:
1 2 3 4 6

Must produce this output:
The inputs do NOT form a sequence!

For example, the following input:
56 57 58 59 60

Must produce this output:
The inputs form a sequence!

For example, the following input:
56 58 59 60 61

Must produce this output:
The inputs do NOT form a sequence!

Test your program and save your source code.



Practice question 8


The Apple iPhone started shipping on June 29, 2007. The Openmoko phone started shipping on July 9, 2007. Write a program that reads 3 integer numbers to represent the year, month and date. The program must display the following output messages:

For example, the following input:
2007 6 29

Must produce this output:
That is the same as the iPhone sale date!
That is before the Openmoko sale date.

For example, the following input:
2007 7 9

Must produce this output:
That is after the iPhone sale date.
That is the same as the Openmoko sale date!

For example, the following input:
2011 6 29

Must produce this output:
That is after the iPhone sale date.
That is after the Openmoko sale date.

For example, the following input:
2003 7 9

Must produce this output:
That is before the iPhone sale date.
That is before the Openmoko sale date.



Practice question 9


Let's consider a vending machine that has sells 10 different sweets and candy:
  1. £0.55  Mars
  2. £0.55  Snickers
  3. £0.55  Bounty
  4. £0.85  Peanut M&M
  5. £0.85  Chocolate M&M
  6. £0.65  Aero Bubbles
  7. £0.55  Fruit Pastilles
  8. £0.55  Wine Gums
  9. £0.45  Polo Mints
  10. £0.95  Haribo Gold Bears

Write a program that uses iterative loops to read an input money amount and displays a list of candy sweets that can be bought and the amount of change remaining. The list has no more than one of each type of candy.

For example, the following input:
1.70

Must produce this output:
You have enough money to buy Mars, Snickers, Bounty with change of �0.05

For example, the following input:
4.00

Must produce this output:
You have enough money to buy Mars, Snickers, Bounty, Peanut M&M, Chocolate M&M, Aero Bubbles with change of �0.00

For example, the following input:
0.25

Must produce this output:
You do not have enough money to buy any candy

Test your program and save your source code.



Assessed coursework question 1


Below is the list of the top 10 cities in the world based on population (as of 2011):
  1. Tokyo, Japan (34,400,000)
  2. Guangzhou, China (25,400,000)
  3. Seoul, Korea (25,200,000)
  4. Shanghai, China (24,900,000)
  5. Delhi, India (23,500,000)
  6. Mumbai, India (23,200,000)
  7. Ciudad de Mexico, Mexico (23,000,000)
  8. New York City, USA (22,000,000)
  9. Sao Paulo, Brazil (21,000,000)
  10. Manila, Philippines (20,400,000)

Source: Brinkhoff, T. (2011, Oct 15). The principal agglomerations of the world. Retrieved from http://www.citypopulation.de/world/Agglomerations.html

Write a program that reads 5 input numbers representing the cities in the list above. The program must display the following output:

For example, the following input values:
1 3 5 7 9

Must produce this output:
127100000


For example, the following input values:
2 4 6 8 10

Must produce this output:
115900000


For example, the following input values:
1 3 5 7

Must produce this output:
Missing input cities


For example, if there is no input, the program must produce this output:
Input cities not found


Test your program and save your source code.



Assessed coursework question 2


Write a program to calculate car parking fees according to the following schedule of rates:

The program must accept integer input values as comma separated pairs:

The program must display the following output:

For example, the following input values:
90,1

Must produce this output:
The parking fee is 9.00 GBP for 90 minutes using weekday rates


For example, the following input values:
90,0

Must produce this output:
The parking fee is 8.50 GBP for 90 minutes using weekend rates


For example, the following input values:
300,1

Must produce this output:
The parking fee is 23.50 GBP for 300 minutes using weekday rates


For example, the following input values:
300,0

Must produce this output:
The parking fee is 21.75 GBP for 300 minutes using weekend rates


For example, the following input values:
302,1

Must produce this output:
The parking fee is 28.00 GBP for 302 minutes using weekday rates


For example, the following input values:
302,0

Must produce this output:
The parking fee is 26.00 GBP for 302 minutes using weekend rates


For example, the following input values:
0,1

Must produce this output:
The parking duration is invalid


Test your program and save your source code.