ECE128_LAB Assignment chapter 1
ECE128_LAB Assignment chapter 1
COLLEGE OF ENGINEERING
UNIVERSITI TEKNOLOGI MARA
COMPUTER PROGRAMMING
(ECE128)
MODULE 1
PO5 Apply appropriate techniques, resources, and modern engineering and IT tools to
well-defined engineering problems with and awareness of the limitations.
OUTCOMES
1. To display the ability to write code, debugging, compiling, and executing C programming
language using CodeBlocks Integrated Development Environment (IDE).
2. To show the use of printf and scanf function in controlling the output and input
format.
3. To build computer programs that are able do basic arithmetic operations using variables
and different data types.
EQUIPMENT
1. Personal Computer (PC)/Laptop
2. CodeBlock IDE software
THEORY
The first few lines in the program are not actually part of the program. These are simply
lines of comment and are included to provide information on what the program does. A
comment begins with /* and end with */. A single line comment can also begin with //.
Basically, a C program consists of two sections, the pre-processor directives, and the main
function sections. The pre-processor section is the place where we specify what compiler
should do before compiling the source code. Usually, we would specify the included header file
which will be used in our program. A header file keeps all the information about functions
available in standard C library. All pre-processor directives begin with the # symbol.
The second section begins with the function main() definition. The function main() is
where the program starts its execution. The keyword int at the beginning indicates that function
main() returns the value of type integer. The returns value will be explained in Chapter 4:
Functions. For now, just include the keyword int before the main in each of your programs. The
body of the function main() is between the { (open) and } (close) braces. Within this body is
where we write all the statements that define what our program does. At the end of function
main() we have to include return 0 statement. This indicates that the program has
terminated successfully.
printf is a standard library function which can be used to output information to the
display screen. Its function is to output whatever information contained between the
parentheses. For example, let’s take the previous program’s printf statement:
printf("C program example\n");
The printf function is to print on the monitor screen the string of characters between
the quotation marks. You may notice that there is a ‘\n’ character at the end of the printf
statement. This character is called newline escape sequence. The function of this escape
sequence is to print a newline. Refer to textbook for more escape sequences. Now, refer to
another example:
printf statement.printf("%d\n", num);
The first argument consists of conversion specifier "%d" indicates that an integer is to be
printed on the monitor screen. The second argument contains the value to be printed
represented by a variable; in this case, the variable is num.
Formatted Output
printf can be controlled to display different format of information on the screen. Let’s revise
the printf function general form.
printf(format-control-string, other arguments);
The format-control-string field describes the output format that will be displayed
on the screen. This field is to be manipulated to get the desired output. For example, to output
an integer we specify the format-control-string with %d. The format-control-
string can be controlled by specifying the flags, field-width, precision, length and
the type of conversion. The following is the general form of format-control-string :
% flags field-width precision length type of conversion
The % character indicates the start of the specifier. The flags specifier is optional.
Table 1.1 lists the available flags that can be specified.
Flag Description
+ Display a plus sign preceding the positive value
- Left justify the output within the specified field-width
The field-width specifies the size of field in which output to be printed. If the output
value requires more character than the specified field-width, the field will be expanded.
Otherwise, the field will be padded with blank spaces. The field-width is optional.
The optional precision specifier for an integer indicates the minimum number of digits
to be displayed. precision for a floating-point value specifies the number of digit to appear
after the decimal point.
The length specifies the length of output value to be printed. h indicates a short value
and l indicates a long value.
The type of conversion defines the type of data to be printed and the following
Table 1.2 lists the available conversion.
Integer
Conversion Description
d Signed decimal integer
o Unsigned octal integer
u Unsigned decimal integer
x or X Unsigned hexadecimal integer. x displays the letters a – f and X
displays A - F
Floating-point
Conversion Description
f Signed floating-point with fixed-point notation
e or E Signed floating-point with exponential notation
g or G Signed floating-point with either fixed-point or exponential notation
depending on the magnitude of the value
Character and String
Conversion Description
c Single character
s Characters until a '\0' is encountered
scanf is a standard library function which can be used to input information from
keyboard. It takes whatever entered from the keyboard and interprets it as specified by the
conversion specifier and stores the result in a variable. The following is an example of scanf
function statement:
scanf("%d", &variable);
The first argument consists of conversion specifier "%d" indicates that an integer is to be
read from the standard input which is usually the keyboard. The second argument is where we
specify the variable to be used to store the input integer. Notice that the variable name starts
with the ampersand (&) character. The & is the address of operator in C programming. It tells
the scanf function the location (address) of the memory is stored in memory.
Formatted Input
scanf can be controlled to read precise input from the keyboard. Let’s revise the scanf
function general form:
scanf(format-control-string, other arguments);
The format-control-string field describes the format of the input. This field is to
be manipulated to get the desired input. The following is the format-control-string
general form:
% * field-width length type of conversion
The % character indicates the start of the specifier. The * is the assignment suppression
character. It indicates the input value that is read to be discarded. The * is optional. The
field-width specifies the number of characters to be read by the scanf function. The
field- width is optional. The length specifies the input is to be converted as short or long.
The length is optional. The type of conversion defines the type of data to be read.
Table 1.3 is a list the available conversion.
Conversion Description
d Signed decimal integer
i Signed decimal, octal or hexadecimal integer
o Octal integer
Conversion Description
u Unsigned decimal integer
x Hexadecimal integer
c Character
s String of characters
e, f, g Floating-point
Arithmetic Operators
Arithmetic operations can be performed in the C programming language. There are five
operators available in C which are “+” (addition), “-” (subtraction), “*” (multiplication),
“/” (division) and “%” (modulus). Multiplication, division, and modulus have higher precedence
over the addition and subtraction.
PROCEDURES
Part A: Welcome to C
1. Create a folder in My Document folder. Name the folder with your student number and
name. Example: 202xxxxxxx Aziyah
2. Save all your works in the created folder. At the end of the laboratory, copy the directory
into your USB drive for backup.
3. Open CodeBlocks. Go to All Programs 🡪 CodeBlocks 🡪 CodeBlocks. The workspace of
CodeBlocks can be seen in Figure 1.1.
4. Now go to File, choose New and click Project. A dialog box will be opened, choose
Console Application and press Go. Click Next on the next windows. Select C and press
Next. Give the project title 1-Welcome. In the ‘Folder to create project in’ field, navigate
to your created directory previously. Then press Next. The dialog box can be seen in
Figure 1.2.
5. Once the project title is specified, press Next button. Finally, on the next window, press
Finish button.
6. Once Finish button is pressed, you should have a workspace like the Figure 3. As can
be seen at the left pane of Figure 1.3, 1-Welcome project is created.
7. Double click the directory Sources, you can see main.c file. This is your C source file.
Double click main.c file to open the file. Once the file is open, the file’s content will be
displayed on editor window as shown in Figure 1.4. The editor window is the place
where you will be writing your C code.
Figure 1.1: The CodeBlocks workspace.
9. Follow these steps to execute (run) the source code (program) to see the output of the
program.
a. The code must be built (compiled) first. Compiling a C code is achieved by
pressing button Build as shown in Figure 1.6.
5. Use printf to output the content of the variables. The following statement outputs the
content of variable var1.
printf("The value of variable var1 is %d\n", var1);
6. Repeat the procedure to output the content of variables var2, var3 and var4 using
their corresponding conversion specifiers like %c, %f and %lf.
7. Execute the program and observe the output.
8.
9. In the previous procedures, each variable is declared and assigned a value with
statements such as:
int var1;
var1 = 5;
The declaration and assignment can be combined into a single statement and produces
the same result. This step is called initializing a variable while declaring it. Rearrange
step 3 above by typing:
int var1 = 5;
10. Repeat the procedure for var2, var3 and var4.
11. Execute the program. Observe the output and complete the result sheet.
EXERCISE
Write a program that converts the temperature 0, 100 and 212 in degree Fahrenheit to degree
Celsius. Use the formula:
Celsius = (5.0 / 9.0) * (Fahrenheit – 32);
Display the converted temperatures up to 3 decimal places. The output should be printed in two
right-justified columns as shown in Figure 1.7.
Apply appropriate techniques, resources, and modern engineering and IT tools to well-defined engineering
PROGRAMME OUTCOME (PO): PO5
problems, with an awareness of the limitations.
Output Response
Complete output Complete output Complete output
Ability to obtain output Incomplete output
Output response but with response but with response without x 0.75
response using modern response
major error minor error error
tools
PART B
PART D
PART F