0% found this document useful (0 votes)
61 views6 pages

Lab Labnumber: Labtitle: Coursecode Coursename Semester Yoursemester, Session Yoursession

This document contains an answer sheet for a computer programming lab assignment, with the student's name, student ID, course code, and lab number. It includes worked examples and code snippets using for, while, and do-while loops to solve various problems, along with test outputs and modifications to the loop structures. The student is asked to trace code execution, modify loops, state differences between loops, and provide a problem specification and analysis.

Uploaded by

dharwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views6 pages

Lab Labnumber: Labtitle: Coursecode Coursename Semester Yoursemester, Session Yoursession

This document contains an answer sheet for a computer programming lab assignment, with the student's name, student ID, course code, and lab number. It includes worked examples and code snippets using for, while, and do-while loops to solve various problems, along with test outputs and modifications to the loop structures. The student is asked to trace code execution, modify loops, state differences between loops, and provide a problem specification and analysis.

Uploaded by

dharwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CourseCode CourseName

Semester yourSemester, Session yourSession

ANSWER SHEET

Lab LabNumber: LabTitle

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

ANSWER FOR LAB 04

/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

Please enter an integer: 3


You have entered 3 and the stored number is 7
The values do not match!

Please enter an integer: 4


You have entered 4 and the stored number is 7
The values do not match!

Please enter an integer: 5


You have entered 5 and the stored number is 7
The values do not match!

Please enter an integer: 6


You have entered 6 and the stored number is 7
The values do not match!

Please enter an integer: 7


You have entered 7 and the stored number is 7
The values match!

Please enter an integer: 8


You have entered 8 and the stored number is 7
The values do not match!

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

Your current salary is: RM 4008


Your current salary is: RM 4026
Your current salary is: RM 4054
Your current salary is: RM 4092
Your current salary is: RM 4140
Your current salary is: RM 4198

Your total salary is: RM 4198

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

(1) Acc (6) 3.6487


(2) Ts (7) 0
(3) Vel_0 (8) While
(4) Vel (9) (Ts<=20)
(5) Vel_0 (10) (Ts=Ts+2)

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

time (s) Velocity (m/s)


0 0
2 7.2974
4 14.5948
6 21.8922
8 29.1896
10 36.487
12 43.7844
14 51.0818
16 58.3792

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;

cout <<"time (s) \t \t Velocity (m/s) \n";


for ( Ts =0; Ts<=20; Ts=Ts+2)
{
Vel = Acc * Ts + Vel_0;
cout << Ts << " \t \t " << Vel << endl;
}

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;

cout << Ts << " \t \t " << Vel << endl;


(Ts=Ts+2);
}while (Ts<=20);

PROBLEM SOLVING:

YOUR SUGGESTION: /50


1. Requirement Specification: Mark:
Answer: /1.5

Page 5
Faculty of Electrical and Electronic Engineering
BEJ10102 Computer Programming

2. Analysis: Mark:
Answer: /2

Data Input :

Data Output :

Formula :

Constraint :

3. Design: Design the algorithm of the program in pseudo-code or flowchart. Mark:


Answer: /10

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

- END of LAB REPORT -

Page 6

You might also like