You are on page 1of 17

Faculty of Engineering Technology Page No.

1 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 25/02/2018

FACULTY OF ENGINEERING TECHNOLOGY


DEPARTMENT OF ELECTRICAL ENGINEERING TECHNOLOGY

MICROCOMPUTER LABORATORY
(MAKMAL MIKROKOMPUTER)

WORKING INSTRUCTION AND REPORT

Course Code & Name /


BNR 20803 : COMPUTER PROGRAMMING
Kod & Nama Kursus
Code & Title of Experiment/
Lab 6: Control Structure 1
Kod & Tajuk Ujikaji
Date of Experiment/
Tarikh Ujikaji
Programme/Program BND / BNF / BNF
Group/ Kumpulan
Matrix No./
Name/Nama
No. Matrik
Group Members/
1.
Ahli Kumpulan
2.
3.
4.
Instructor Name / 1.
Nama Instruktor 2.
Result
/80 %
Keputusan
Discussion
/10%
Perbincangan
Conclusion
Assessment / Penilaian /10%
Kesimpulan

TOTAL / JUMLAH /100%


Faculty of Engineering Technology Page No. 2 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

1. EXPERIMENT LEARNING OUTCOME

At the end of the lab session, students should be able to:


1.1. Able to understand the basic structure of C++ program.
1.2. Able to familiarize with the codes, compiling and running a program.
1.3. Able to understand how input and output works in C++ programming

2. INTRODUCTION / THEORY

CONTROL STRUCTURES:

2.1 Sequence structure


2.1.1 Programs executed sequentially by default

2.2 Selection structures


2.2.1 if (select true statement or proceed next line)
2.2.2 if/else ( with/no range, with/no fix result)
2.2.3 If/else (more than 2 selection, true or false)
2.2.4 Nested if/else (more than 3 selection, true or false)
2.2.5 switch (more than 3 selection, true or false, no range, fix result)

2.3 Repetition structures


2.3.1 while (selection true statement or skip next line)
2.3.2 do/while (do statement and then selection next line or do statement again)
2.3.3 for (initialize value and then selection skip nest line or do statement and
increment/decrement)
Faculty of Engineering Technology Page No. 3 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

Selection
Flowchart syntax
structures

if

if/else

Nested
if/else
statement
Faculty of Engineering Technology Page No. 4 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

switch
statement

while

do/while

for
Faculty of Engineering Technology Page No. 5 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

3. EQUIPMENT / REQUIREMENT

3.1. A computer with Microsoft Office and C/C++ program

4 PROCEDURES (TASKS)

4.1 Selection if [6 marks]

#include <iostream>
using namespace std;
main ()
{
int x;
cout <<"\n Please enter number '5'\n";
cout <<"\n ";
cin >>x;

if(x == 5)
{
cout << "\n you enter no ="<<x<<endl;
cout << " yes, that is the number!!\n"<<endl;
}
cout << "\n See you again"<<endl;
return 0;
}

4.1.1 explain what the program does? [2 marks]

4.1.2 What is the output. [4 marks]


Faculty of Engineering Technology Page No. 6 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.2 Selection if/else [8 marks]

#include <iostream>
using namespace std;
main ()
{
int x;
cout <<"\n Please enter number '5'\n";
cout <<"\n ";
cin >>x;

if(x == 5)
{
cout << "\n you enter no ="<<x<<endl;
cout << " yes, that is the number!!\n"<<endl;
}
else
{
cout << "\n you enter no ="<<x<<endl;
cout << "\n wrong, that not the number!!\n"<<endl;
}
return 0;
}

4.2.1 explain what the program does?. [2 marks]

4.2.2 Predict the output. [4 marks]


Faculty of Engineering Technology Page No. 7 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.2.3 What the different between program 4.1 and 4.2. [2 marks]

4.3 Selection nested if/else [6 marks]

#include <iostream>
using namespace std;
int main()
{
int number;
cout<<" Enter a number between 0 and 30"<<endl;
cout<<"\n ";
cin>>number;
cout<<"\n";

if (number <= 10)


{
cout<<" Your number was between 0-10"<<endl;
}
else if (number <= 20)
{
cout<<" Your number was between 11-20"<<endl;
}
else if (number <= 30)
{
cout<<" Your number was between 21-30"<<endl;
}
else
{
cout<<" Your number was larger then 30!!!"<<endl;
cout<<" You Must have not read the directions!"<<endl;
}
return 0;
}
Faculty of Engineering Technology Page No. 8 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.3.1 Explain what the program does?. [2 marks]

4.3.2 Predict the output. [4 marks]

4.4 Given table 1 is UTHM student score versus grade. Please create flowchart and C++ code that
will read the student score, grade it and then display Operators [16 marks]

Table 1 : UTHM student score vs grade

SCORE GRADE DISPLAY


100 - 85 A+ Your Grade is A+
80 - 84 A Your Grade is A
75 - 79 A- Your Grade is A-
70 - 74 B+ Your Grade is B+
65 - 69 B Your Grade is B
60 - 64 B- Your Grade is B-
55 - 59 C+ Your Grade is C+
50 - 54 C Your Grade is C
45 - 49 C- Your Grade is C-
40 - 44 D Your Grade is D
0 - 39 E Fail
Faculty of Engineering Technology Page No. 9 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.4.1 Flowchart [6 marks]


Faculty of Engineering Technology Page No. 10 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.4.2 C++ program [10 marks]


Faculty of Engineering Technology Page No. 11 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.5 Selection small number using if/else [6 marks]

#include <iostream>
using namespace std;

main ()
{
float x, y, min;
cout << " Enter two different integer numbers:\n";
cout << "\n 1st integer number x: ";
cin>>x;
cout << "\n 2nd integer number y: ";
cin>>y;

if( x && y) // If both inputs are


{
if( x < y )
min = x;
else
min = y;
cout << "\n The smaller number is: " << min << endl;
}
else
cout << "\n Invalid Input!" << endl;

return 0;
}

4.5.1 Explain what the program does? [2 marks]

4.5.2 Predict the output. [4 marks]


Faculty of Engineering Technology Page No. 12 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.6 Create C++ programming base on output Figure 4.6.1 & 4.6.2 [8 marks]

Figure 4.6.1 : if inserted number is positive

Figure 4.6.2 : if inserted number is negative

4.6.1 Create C++ Programming [8 marks]


Faculty of Engineering Technology Page No. 13 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021


Faculty of Engineering Technology Page No. 14 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.7 Create flowchart and C++ programming using if/else selection [28 marks]

Table 4.7.2: Food to bring home

Table 4.7.1: Menu Warung Pak Tam Food to bring


No Menu home
No Menu Price/ RM Ahmad Harun
1 Nasi Goreng 5.00 1 Nasi Goreng 2 2
2 Mee Goreng 5.00 2 Mee Goreng 1 1
3 Mee Racun 5.50 3 Mee Racun 1 1
4 Mee Bandung 5.50 4 Mee Bandung 2 2
5 Mee Hailam 5.50 5 Mee Hailam 3 3
6 Mee Hongkong 6.00 6 Mee Hongkong 2 2
7 Nasi Ayam 5.50 7 Nasi Ayam 4 4
8 Air Sirap Ais 1.50 8 Air Sirap Ais 10 10
9 Air Teh 1.00 9 Air Teh 2 2
10 Air Kopi 1.20 10 Air Kopi 3 3

Ahmad and Harun want to buy some food from Warung Pak Tam. Since Ahmad is the cousin of
Pak Tam, he will get 20% discount but Harun still remain to normal price. But if the total price
over than Rm100.00, the buyer will get 30% discount. From menu table 4.7.1 and 4.7.2:

4.7.1 Calculate the exacly price that Ahmad and Harun need to pay. [8 marks]
Faculty of Engineering Technology Page No. 15 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.7.2 Create flowchart. [8 marks]


Faculty of Engineering Technology Page No. 16 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

4.7.3 Using if/else selection, create C++ programming. [14 marks]


Faculty of Engineering Technology Page No. 17 / 17

Department of Electrical Engineering Technology Revision No. 1

Title: Lab 6: Control Structure 1 Effective Date 01/10/2021

5. DISCUSSION
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
___________________________________________________________________ (10 marks)

6. CONCLUSION
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
____________________________________________________________________ (10 marks)

Prepared by / Disediakan oleh : Approved by / Disahkan oleh :

Signature / Tandatangan : Signature / Tandatangan :


Name / Nama : Dr. Mohd Shamian Bin Zainal Name / Nama : Ts. Dr. Hairulazwan Bin Hashim
Date / Tarikh : 1st October 2021 Date / Tarikh : 1st October 2021

You might also like