Lab Labnumber: Labtitle: Coursecode Coursename Semester Yoursemester, Session Yoursession
Lab Labnumber: Labtitle: Coursecode Coursename Semester Yoursemester, Session Yoursession
ANSWER SHEET
Lecturer’s Name:
yourLecturer’sFullName
Prepared by:
yourFullName (yourMatricID)
Section:
yourSection
Submission Date:
dd/mm/yyyy
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
/50
Practice 01: use the for loop
2. Compile and run the program using random numbers as the inputs. Write the output of the Mark:
program printed on the console screen.
Answer: /3.5
/0.5
3. Is the body of for loop executed 5 times? Give the reason if your answer is No. Mark:
Answer: /1.5
No, because the looping started at 21, the increment is by 5 and suppose the
instruction repeat 5 time instead of 6
4. Modify the for statement to let the loop executes 7 times, where the value of looping decreases Mark:
by 3 in each repetition (provide the fragment of modified code only).
Answer: /1.5
for (looping=21; looping<55; looping+=5
Practice 02: use the for loop and modify to the while, and do…while loops
1. Based on the program of secondfor.cpp in Figure 2, trace the looping manually and fill in Table Mark:
/9.5
Page 2
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
2.1.
Answer: Table 2.1 /2.5
Table 2.1
trip = ? trip<62? salary = salary + trip trip+=10
(True or
False)
Loop 1 trip= 8 true salary = 4000.00+8 = 4008.00 trip= 8+10=18
Loop 2 trip= 18 true salary = 4008.00+18= 4026.00 trip= 18+10= 28
Loop 3 trip= 28 true salary = 4026.00+28= 4054.00 trip= 28+10= 38
Loop 4 trip= 38 true salary = 4054.00+38= 4092.00 trip= 38+10=48
Loop 5 trip= 48 true salary = 4092.00+48= 4140.00 trip= 48+10=58
Loop 6 trip= 58 true salary = 4140.00+58= 4198.00 trip= 58+10=68
2. Copy the program of secondfor.cpp from Figure 2 to a new file or online programming Mark:
editor/compiler. Then, compile and run the program. Write the output of the program printed on
the console screen.
Answer: /0.5
3. Modify the for statement into the while statement (make sure that the new version program Mark:
produces the same output with the previous program. Provide fragment of the modified code
only).
Answer: /2.5
trip=8;
while ( trip<62)
{
salary = salary + trip;
trip+=10;
cout<<"Your current salary is: RM "<<salary<<endl;
}
cout<<endl;
cout<<"Your total salary is: RM "<<salary<<endl;
Page 3
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
4. Modify the for statement into the do…while statement (make sure that the new version program Mark:
produces the same output with the previous program. Provide fragment of the modified code
only).
/3
trip=8;
do {
salary = salary + trip;
trip+=10;
cout<<"Your current salary is: RM "<<salary<<endl;
}
while ( trip<62);
cout<<endl;
5. State the difference between while loop and do..while loop. Mark:
Answer: /1
Practice 03: use the while loop and modify to the for, and do…while loops
1. Fill in the blanks (number 1 until 10) with correct C++ statements for the program in Figure 3. Mark:
Pseudocode 1 is given as a reference.
Answer:
/12
/5
2. Copy the program of firstwhile.cpp from Figure 3 to a new file or online programming Mark:
editor/compiler. Then, compile and run the program. Write the output of the program printed at
the console screen.
Answer: /0.5
Page 4
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
18 65.6766
20 72.974
3. Based on the output in step 2, does the program produces the correct results? Provide the reason Mark:
for your answer.
Answer /1
Yes
4. Modify the while statement to the for statement (make sure that the new version program Mark:
produces the same output with the previous program. Provide fragment of the modified code
only).
Answer: /2.5
float Acc,Ts,Vel_0,Vel;
Vel_0 = 0.0;
Acc = 3.6487;
5. Modify the while statement to the do…while statement (make sure that the new version program Mark:
produces the same output with the previous program. Provide fragment of the modified code
only).
Answer: /3
do {
Vel = Acc * Ts + Vel_0;
PROBLEM SOLVING:
Page 5
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming
2. Analysis: Mark:
Answer: /2
Data Input :
Data Output :
Formula :
Constraint :
4. Implementation: Write down your complete source code. Include documentation for the Mark:
program at appropriate lines of code.
Answer: /10
5. Testing and verification: Compile the program. Enter different inputs as follows. Record what is Mark:
printed in the Console Window.
Answer: /1.5
Input
BMI type, Output in Console Window
weight, height
BMI type = 1
Weight = 76
Height =1.96
BMI type = 1
Weight = 61
Height =1.92
BMI type = 2
Weight = 198.5
Height = 69.6
Page 6