0% found this document useful (0 votes)
21 views

ECE128_LAB Assignment chapter 1

Lab assignment chapter in computer engineering

Uploaded by

2024230722
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

ECE128_LAB Assignment chapter 1

Lab assignment chapter in computer engineering

Uploaded by

2024230722
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

ELECTRICAL ENGINEERING STUDIES

COLLEGE OF ENGINEERING
UNIVERSITI TEKNOLOGI MARA

COMPUTER PROGRAMMING
(ECE128)

MODULE 1

INTRODUCTION TO C PROGRAMMING AND FORMATTED INPUT/OUTPUT

COURSE OUTCOME (CO) - PROGRAMME OUTCOME (PO)


CO2 Construct computer language programs using a standard programming tool. (P4)

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

C is a general-purpose programming language developed in 1972 by Dennis Ritchie. It


was originally designed and implemented for UNIX operating system. CodeBlocks IDE is
software for developing, debugging, compiling and executing C or C++ program.

Now let’s look at an example of C program:


/*
This program was written by Siti Aminah in Mei 2013.
This program displays the sentence “C program example” on computer
screen.
*/
#include<stdio> // to include standard input/output library

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.

Variables and Data Types


Variable is a memory location that can store value. The stored value can be retrieved or
modified when necessary. Initialization of a variable means a value is given to the variable
during its declaration. When a variable is declared, its data type is given to specify what type of
data can be stored in the variable. There are four basic data types in C programming
language: char, int, float and double. Each data type has its own size. The size of a variable is
the memory space allocated in the memory required to store its value.

Outputting Information using printf

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.

Table 1.1: Flags in C

Flag Description
+ Display a plus sign preceding the positive value
- Left justify the output within the specified field-width

# Display 0 preceding an octal output value and 0x or 0X preceding an


hexadecimal output value
blank Print a blank space before a positive value not preceded with a plus sign

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.

Table 1.2: Type of conversion in C

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

Inputting Information using scanf

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.

Table 1.3: Type of conversions in C

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.

Figure 1.2: Creating a new project.


Figure 1.3: 1-Welcome project is created.

Figure 1.4: Editor window.


8. As can be seen on the editor, the source file contains a few lines of C code. Figure 1.5
shows the explanation of the code.

Figure 1.5: Explanation of the C code in 1-Welcome.

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.

Figure 1.6: Building the source code.


b. Compiling a code can also be achieved by selecting Build menu and choose
Build. Upon pressing the Build button, the source code will be compiled, linked
and an executable (*.exe) file will be made.
c. The result of building the program can be seen at the output window (below
editor window).
d. If there are errors during the build process, the errors in the code must be
identified and corrected.
e. Assume that the output window reporting zero error and warning, press the Run
button to execute the code. Observe the output.
10. Now change the “Hello world!” to “Welcome to ECE128 course”. Build and execute the
program. Observe the output and complete the result sheet.

Part B: Variables and Data Types


1. Create a new project named 1-DataType.
2. Include the stdio.h header file and define the main() function.
3. Type the following statements to declare the variables.
int var1; // declare variable var1 type int
var1 = 5; // store value of 5 into variable var1
4. Repeat the procedure in step 3 for the following variables in Table 1.4. Note that single
quotes (‘ ’) are required to assign a value to a char type variable (var2).
Table 1.4

Variable Data Type Value

var2 char 'a'

var3 float 10.54

var4 double 205.7526

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.

Part C: Use Scanf to Read Integers


1. Create a new project named 1-Input.
2. Include the stdio.h header file and define the main() function.
3. Declare two variables named var1 and var2 type int.
4. Use printf to display "Enter two numbers: " on the monitor screen.
5. Type the following statement:
scanf(“%d”, &var1);
6. Repeat the above statement for var2.
7. Use printf to output the contents of variables var1 and var2.
8. Execute the program and observe the output.
9. Replace both scanf statements with the following statement:
scanf(“%d%d”, &var1, &var2);
10. Execute the program. Observe the output and complete the result sheet.

Part D: Using Conversion Specifiers

1. Create a new project named 1-ConvSpecifier.


2. Include the stdio.h header file and define the main() function.
3. Declare a variable named var1 type char.
4. Declare three variables named var2, var3 and var4 type int.
5. Declare a variable named var5 type float.
6. Type the following statements. The first scanf statement reads a character. The next
three scanf statements read decimal, octal and hexadecimal integers. The last scanf
statement reads a floating-point number.
printf("Enter a character:\n");
scanf("%c", &var1);
printf("Enter three same decimal number:\n");
scanf("%d", &var2);
scanf("%o", &var3);
scanf("%x", &var4);
printf("Enter a floating-point number:\n");
scanf("%f", &var5);
7. Output var1 using printf statement and %c specifier.
8. Output var2, var3 and var4 variables using printf statement and their %d
conversion specifiers since var2, var3 and var4 are type int.
9. Output var5 using printf statement and %f specifier.
10. Execute the program. Use these data as inputs:
a. T for character
b. 155 for decimal numbers
c. 55.135 for floating-point number
11. Observe the output and complete the result sheet.

Part E: Reading Input with Field-Width


1. Create a new project named 1-InputFieldWidth.
2. Include the stdio.h header file and define the main() function.
3. Declare three variables named var1, var2 and var3 type int.
4. Type the following statements:
printf("Enter an eight digit integers: "); scanf("%3d%2d
%d",&var1,&var2,&var3);
5. Output variable var1, var2 and var3 using:
printf("%d %d %d",var1,var2,var3);
6. Execute the program.
7. Observe the output and complete the result sheet.
Part F: Filtering Character from the Input Stream
1. Create a new project named 1-InputFilter.
2. Include the stdio.h header file and define the main() function.
3. Declare three variables named var1, var2 and var3 type int.
4. Type the following statements:
printf("Enter date(e.g., 151113): ");
scanf("%2d%2d%2d",&var1,&var2,&var3);
printf("Day %d, Month %d, Year %d\n", var1,var2,var3);
5. Execute the program and observe the output.
6. Modify the above statements into the following statements:
printf("Enter date(e.g., 15-11-13): ");
scanf("%2d-%2d-%2d",&var1,&var2,&var3);
printf("Day %d, Month %d, Year %d\n", var1,var2,var3);
7. Execute the program. Observe the output.
8. Modify the above statements into the following statements:
printf("Enter date(e.g., 15/11/13): ");
scanf("%2d%*c%2d%*c%2d",&var1,&var2,&var3);
printf("Day %d, Month %d, Year %d\n", var1,var2,var3);
9. Execute the program.
10. Observe the output and complete the result sheet.

Part G: Printing with Field-Width and Precision


1. Create a new project named 1-OutputFormat.
2. Include the stdio.h header file and define the main() function.
3. Declare a variable named var1 type int.
4. Declare two variables named var2 and var3 type float.
5. Use printf to display "Enter three numbers: " on the monitor screen.
6. Use scanf to read and store three values in variables var1, var2 and var3.
7. Type the following statements:
printf("%10d\n", var1);
printf("%5f\n", var2);
printf("%20f\n", var3);
8. Execute the program and type the following for input:
a. 505
b. 505.505
c. 505.505
9. Observe and relate the output with %10d, %5f and %20f in the printf statements.
10. Modify the printf statements into the following statements:
printf("%.9d\n", var1);
printf("%.6f\n", var2);
printf("%.2f\n", var3);
11. Execute the program and type the following for input:
a. 592
b. 251.8658
c. 1.5268
12. Observe and relate the output with the %.9d, %.6f and %.2f in the printf statements.
13. 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.

Figure 1.7: Right justified column.


RUBRIC
COURSE OUTCOME (CO): CO2 Construct computer language programs using a standard programming tool. (P4)

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.

KNOWLEDGE PROFILE (DK): - -

PROBLEM SOLVING (DP): - -

ENGINEERING ACTIVITIES (NA): - -

POOR FAIR GOOD EXCELLENT


CRITERIA SUB-ATTRIBUTE SCORE WEIGHT MARKS
1 2 3 4

Handling Handle the Handle the


Unable to handle Handle the
Software/Equipment software/ software/
the software/ software/
Ability to demonstrate a equipment with equipment with x 0.75
equipment even equipment without
Experime proper use of moderate minimal
with assistance assistance
ntal/ software/equipment assistance assistance
Demonstr Complete
Construct Computer Unable to Complete
ation constructing Complete
Program construct constructing
setup computer program constructing
Ability to construct computer program computer program with minimal x1
computer program
computer program using C even with with moderate assistance without assistance
programming language assistance assistance

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

TOTAL (MARKS) /10


RESULT SHEET
PART A

Screenshot of source code

Screenshot of computer program

PART B

Screenshot of the source code

Screenshot of the computer program


PART C

Screenshot of the source code

Screenshot of the computer program

PART D

Screenshot of the source code


Screenshot of the computer program
PART E

Screenshot of source code

Screenshot of computer program

PART F

Screenshot of the source code


Screenshot of the computer program
PART G

Screenshot of the source code


Screenshot of the computer program
EXERCISE

Screenshot of the source code

Screenshot of the computer program

You might also like