Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CPT109 C programming and SW engineering 1

Lab Practice 2 – Basic C Exercises

Objectives

The main objective of this laboratory session is to help you review and understand the some of the fundamental C programming concepts introduced during the Week 2 Lecture. It is also important for you to start to familiarise yourself with writing and compiling programmes. The laboratory involves copying simple programs to aid your understanding and some problem solving exercises where you need to write your own.

General Guidelines

Write the code for each exercise using your preferred compiler.

DO NOT copy and paste the code into the compiler, this will usually fail.

Add comments to your code as necessary this is a good habit.

Save the code for each exercise as exercise1.c, exercise2.c, etc. for later reference.

If compilation fails, check the failure messages for clues as to why and fix the problems.

Run the executable file and check you understand what it does.

If a program involves entering data on the keyboard, try inputting incorrect data to see what happens.

Exercise 1: (Using Escape Characters)

Copy, compile and run the following program:

#include

int main(){

printf(“I am writing this text on one line”);

return 0;

}

Modify the program as follows

#include

int main(){

printf(“I am still writing this text”); printf(“ on one line”);

return 0;

}

Did you notice any changes?

The output should appear the same. The reason is that you need to request explicitly if you want something to be printed on a new line. Here's how you do it

#include

int main(){

printf("Now I am writing this text\n"); printf(" on two lines!");

return 0;

}

The secret is to insert two characters, \n (read "backslash n") at the point where you wish the text to appear on a new line. The "backslash" character \ is known as the escape character because it lets the character that follows “escape” being printed on screen. The complete sequence of characters \n is known as an escape sequence.

Exercise 2: (Writing a simple program)

Write a program that displays your name vertically i.e. each letter on a new line. Use printf() only once. Compile and run your program to check if you were right.

Exercise 3: (Writing simple programs)

Write a program that displays your name on two lines inside a rectangle of asterisks as follows:

******************

*       First name        *

*        Surname           * ******************

Make sure the asterisks are properly aligned. To do this you will need to insert spaces (blanks) in the right place. Use printf() exactly four times. Test your program.

Here are some more Escape Sequences:


\a bell \0        null character

\r carriage return \b        backspace

\n newline \t         horizontal tab


To print the ‘ \ or “ characters you must use the \ first

\ ’         single quote                \\         backslash

\"         double quote

%%     sometimes needed to print a % character

Write a program that displays your name on a single line and makes the computer beep at the end! Find which escape sequence from the previous list you need to use. Test your program to see and hear if it works!

Exercise 4 (Use of #define directive)

The following example shows how you can use the #define preprocessor directive to introduce  constants  in  your  program.  Copy,  compile  and  run  this  program.  What happens?

#include

#define SPEEDLIMIT 70

int main(){

printf(“The UK SPEEDLIMIT is %d miles per hour”, SPEEDLIMIT); return 0;

}

The preprocessor - which prepares the code to be compiled - has replaced all occurrences of your constant SPEEDLIMIT in your source code file with 70 before passing the text to the compiler.

Notes:

.    The same name (SPEEDLIMIT) in between the quoted string (“”) has not been altered.

.    Since SPEEDLIMIT is an integer value we used the format specifier %d.

If you have a program that uses the same constant value many times you can use this #define preprocessor. If you later need to change the value, you only need to change the number and then recompile.

Exercise 5 (Variable declarations and initialisation)

What is the value of a variable once you have declared it? Copy, compile and run this program.

#include

int main(){

int my_int;

printf("Variable my_int is: %d", my_int);

return 0;

}

What did you see?

Was it what you expected?

Why do you think you saw this?

Try other variable types.

Exercise 6: (variable initialisation)

Let's declare and initialise our variables in one go:

#include

int main(){

int my_int = 120;

printf(“Variable my_int is: %d”, my_int);

return 0;

}

Exercise 7: (variable initialisation)

Write program that declares and initialises the value of a char variable. Prints the variable on the screen using the format specifier %c and %d. Make sure you understand what happens.

Exercise 8: (Some simple maths)

Let’s try a simple example that involves some simple arithmetic operations

#include

int main(){

int miles, yards;

float kilometres;

miles = 26;

yards = 385;

kilometres = l.609 * ( miles + (yards/1760.0) );

printf(“\n To win a marathon you must run %fkilometres!”, kilometres); return 0;

}

Exercise 9: (Some simple maths)

Write a similar program that can:

Convert 3 hours and 25 minutes into seconds.

Display the result on the screen.

Try using the #define preprocessor to define then use these in your calculation:

SECONDS_PER_HOUR 60

MINUTES_PER_HOUR 60