You are on page 1of 23

Exercise 1 : concept understanding

M/S 82

1. Deterrmine whether the following expression is True or False, assuming that: a = 2, b = 3, c = 4 i. ii. iii. iv. v. a>2 a + b == b + c ( a + 2) ! = c b <= a * 3 a * c >= a * b -False -False -False -True -True

2. Examine the expression below and show the order of how the operator works. Assuming: Bool sval , tval , int x =4 , y =8; i. ii. iii. iv. v. sval = 7 +x > 6 + y ; False tval = x == y ; False sval = x ! = y ; True tval = ! (x > y) ; True sval = y x <= y + x ; True

3. Answer the question whether it is Yes or no. i. If it is true that a > b and it is also true that a < c, does mean b < c is true. Yes If it is that a >= b and it is also true that c == a does mean c == b is true? No If it is true that a ! = b and it is also true that a ! = c, mean c != b is it true? Yes

ii.

iii.

4. Given the following declarations, evaluate the expression below and assign true or false value for each expression bool found = true ; bool flag = false; int number = 5 ; int xint = 12 ; int yint = 100 ; double xdouble = 50.3 ; double ydouble = 1.3 ; char ch = D ; i. ii. iii. iv. v. vi. vii. viii. ! found False ! number False ( number >= xint ) && (number<=yint) False (found | | flag) True (xint ! = yint) True (ydouble * 10) <= xdouble True A <= ch && ch <=Z True number-5 == xint) && !flag False

5. write a C++ expression for each the following conditions. i. ii. iii. Number greater than 1 smaller than 9. (Number >1) && (Number <9) Variable ch not character q or k. (ch != q) // zf (ch != k) Number between 1 to 9 but not number 5. (Number >= 1) && (Number <= 9) && (Number !=5) Not the number between 1 to 9. ( Number < 0 ) && (Number >9)

iv.

6. Write an if statement that displays the message excellent if the variable named mark is grater or equals to 90. Draw flowchart.
FLOW CHART

Start

End

If statement if (marks >=90) cout << excellent;

7. write an if statement that assigns 10 to a variable named ivalue if the variable jvalue is equal to 200. Draw flowchart. If statement if (jvalue == 200) cout<< ivalue = ivalue + 10 ;
Start

jvalue==200 Yes

No

ivalue = ivalue + 10

Display ivalue

End

8. Write an if statement that multiplies pointsearn by 20 if the variable spend is greater than 500. Draw the flowchart. If statement if (spend > 500) cout<< pointsearn *20 ;

Start

Spend > 500

Yes

Display pointsearn * 20

No

End

9. Write an if statement that displays the message Not valid number if the variable value is outside the range of 1 to 10. Draw the flow chart. If statement IF ((value number < 1) && (value number > 10)) cout << Not valid number ;

FLOW CHART
Start

NO Display Not Valid Number

End

10. Write an if statement that displays the message Valid number. Proceed to next input if the variable value is within the range of 20 to 50. If statement if ((value => 20) && (value =<50)) cout<< Valid number. Proceed to next input ;

FLOW CHART
Start

value number >= 20 && number <=50 YES Display Valid number. Proceed to next input NO

End

11. Given Program 7.1. Study the code and run the program.

i. ii.

Explain the function or objective the program. Draw the flowchart. To give a Score or Grade for example to student Draw a flow chart for the program.

Start

(mark >=75)

No

No

No

(mark<45) && (mark>=30) yes

No

(mark <30)

No

yes

yes

End

iii.

Given the input mark =90.6, identify which if statement will be evaluated and what are the results of the evaluated expression. Int mark; mark =90.6; If (mark >= 75) cout<<Your score : A

12. write an if..else statement that assigns P to the variable named gred if the totalMarks is greater than or equal to 60. Otherwise, gred is assigned to L. Draw flowchart.

If else statement if (totalMark >=60) cout<< gred P ; else cout<< gred L ;

Start

totalMarks >= 60

No

yes

Display gred p

Display gred L

End

13. Given Program 7.2, study the code and run the program.

i.

Explain the function or objective of the program. Draw the flow chart. To determine whether the grade is Fail or Pass
Start

(mark >=30)

No

(mark<30)

No

yes Display TEST 1 -> Pass Display TEST 1 -> Fail

yes

End

ii.

Change the usage of the 2 if statements (Line 11 and Line 13) to an if.. else statement. if (mark >=30) cout<<TEST 1 -> Pass; else cout<<TEST 1 -> Fail ; cout<<Try your best in your TEST2

iii.

Show the difference between the usage of the 2 if statement and the if..else statement using flowchart.

Start

Start

(mark >=30)

No

(mark >=30)

No

(mark<30)

No

yes

yes
Display TEST 1 -> Pass Display TEST 1 -> Fail

Display TEST 1 -> Pass

End

IF..else

IF

End

14. Rewrite the following conditional expressions as if.. else statements. Draw the flowchart. i. a =b > 60 ? 8 : 10; if (a = b > 60) 8; else 10; FLOW CHART

Start

(a=b>60)

Yes

Display 8

No

Display 10

End

ii.

totalBil = count == 2 ? bilItem : bilItem * 5; if (totalBil = count ==2) bilItem; else bilItem *5; FLOW CHART

Start

(totalBil = count == 2 No

Yes

Display billitem

Display bilitem *5

End

iii.

cout << (number % 5) == 0) ? Blue : Red if ( (number % 5) == 0) cout << Blue; else cout <<Red; FLOW CHART

Start

(number % 5) == 0 No

Yes

Display Blue

Display Red"

End

15.Rewrite the following if..else statements as conditional expressions. Draw the flowchart. i. If (minute > 59) cout << Not valid ; else cout << Okay, valid; minute >59 ? Not valid : Okay, valid FLOW CHART

Start

minute > 59

Yes

No Display Okay, valid"

End

ii.

If (marks >90) cout << You are qualified; else cout << You are not qualified ; marks > 90 ? You are qualified : You are not qualified

FLOW CHART

Start

marks > 90

Yes

No Display You are not qualified"

End

16. Given Program 7.3, study the code and run the program.

i.

Start

val = 10

Wal = 3

End

ii.

i.

Start

(mark >=75)

No

No

No

(mark<45) && (mark>=30) yes

No

(mark <30)

No

yes

yes

End

ii.

Given the input mark = 90.6 identify which if statement will be evaluated and what are the results of the evaluated expression. Int mark; mark =90.6; If (mark >= 75) cout<<Your score : A Explain the difference between the if statement in Program 7.3 with the if.. else.. if statement in program 7.4. In program 7.3 it have many open and close brace. Compared with program 7.4 which only have one (1) open brace and one(1) close brace.

iii.

18. write need if statement that perform the following test : if the variable employed is equal to is equal to Y and if worklength is equal or greater than 5, then display the message Your credit card application is accepted However, if work length is less than 5, then display the essage please provide guarantor. Otherwise, if the variable employed is equal to N the display the message Your credit card application is rejected..

Nested if statement if (employed ==Y) { if (work length =>5) cout<<Your credit card application is accepted; if (worklength <=5) cout<<Please provide guarantor; } if (employed == N) cout<<Your credit card application is rejected

Draw Flow chart.

Start

yes

Employe == Y

No

Employe == N

No

yes yes Worklength >= 5 No Display Your credit card application is rejected Display Please provide a guarantor

End