SECJ 1013 PT 1 Lab 2 Group members

SECJ 1013 -PT 1 Lab 2 Group members: 1. AHMAD MAHYUDDIN BIN MD SAID A 20 EC 0178 2. KHAYRIN NABILA BINTI JAMAL A 20 EC 0196 3. MUHAMMAD NAJIB BIN JAMALUDIN A 20 EC 0215

1. Trace the i and j variables values at x and y points, output and relational expression result of the below flowchart. Continue the answer in the given table. Start i = 1, j = 0 x i *= 2 j++ y Display i + j i < 10 FALSE Stop y x TRUE i j output i < 10 1 0 2 1 3 2 < 10 - true 2 1 4 2 6 6 < 10 - true 4 2 8 3 11 8 < 10 – true 8 3 16 4 20 20 < 10 - false

2. Translate the following flowchart into a complete C++ program. Write your answer in the next slide and make sure the program precisely follows the algorithm described by the flowchart. 1 Start Get num count = 0, num = 0 even = 0, odd = 0 True count < 5 False Display odd & even False True 1 num < 0 || num > 9 End num % 2 == 0 2 False True even = even + num 2 count = count + 1 odd = odd + num

#include <iostream> using namespace std; Source code int main() { #include <iostream> using namespace std; int count=0, num=0, even=0, odd=0; . . . int main() { int count =0, num =0, even =0, odd =0; while( count<5 ) { do { return 0; } } cout << "Enter number 0 to 9: "; cin >> num; while (num < 0 || num > 9); { if ( num % 2 == 0) even = even + num; } count = count + 1; } else odd = odd + num; } cout << "sum of odd numbers: "<< odd << endl; cout << "sum of even numbers: "<< even << endl; return 0;
- Slides: 4