<mosaic.cnfolio.com>
Introduction to Algorithms and Programming ENG421

ENG421 Practice worksheet 1 – Reading source code, compiling programs and debugging

Academic year 2015/16


The online compiler






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?



The main() function


The main() function is required for all C programs.



Question 2


How could the program from Question 1 be modified to perform subtraction of two numbers?

Test your program.



Source code comments


Source code may include comments to describe and explain the design and usage of the program. Comments start with /* and continue until it reaches a matching */ end. For example:
/* Comment text do not affect the functionality of the C program */

Do not use C++, Java or PHP style comments, which use a double forward slash at the indicate the start of comments. For example:
// Do not use comments in this style. They reduce portability between C compilers.



Question 3


  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.



Question 4


  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.



Question 5


  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.



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.



Question 6


  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.



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.



Question 7


The cost to stay at a hotel is 24.99 per night. You arrive at the hotel at 4pm on 5 Jan and depart at 8am on 11 Jan. Write a C program to calculate and display the total cost of your stay at the hotel.

Test your program.