Lab 6 r Operators l Relational Operators l
Lab 6 r Operators l Relational Operators l Equality Operators l Logical Operators r If statement l One Alternative l With Compound Statement l Nested If NOTE: Be Careful Read Chapter 4(All Self-Check exercises and all remaining exercises). C KEYWORDS and format must be learned by heart
Review of Rules of Evaluating Expressions Exercise 1: Include parentheses to show the order of performing the expressions. r a+b*c r a*b+c%d r -a /-b +c r -a/-(b+c)
Simple Conditions Exercise 2: Provide the English Meaning and Values. Given: x=-5, power=1024, y=7, MAX_POWER=1024, mor_or_dad=‘M’ SENTINEL=999 r x<=0 r x>=MAX_POWER r mor_or_dad == ‘M’ r x != SENTINEL
Operator Precedence ---------------------Function calls highest ! + - & (UP) * / % + < <= > >= == != %% || = Lowest
Exercise 3 (Step by Step Evaluation) r Provide the English Meaning and Values. Given : x=3. 0, y=4. 0, z =2. 0, and flag =5 r !flag r x + y / z <= 3. 5 r !flag || (y+z >= x-z) r !(flag || (y+z >= x-z)
Short-circuit evaluation and Logical Assignments r Short-circuit evaluation : stopping evaluation of a logical expression as soon as its value can be determined. l Exp : a ||b , a&&b r Logical Assignments: int age, char gender, int senior_citizen, in_range and is_letter, n, even l scanf (" %d ", &age); l senior_citizen=(age>=65); l ! senior_citizen; l senior_citizen && gender ==‘M’; l is_range = (n>-10 && n<10); • is_letter = ( (‘A’ <= ch && ch<=‘Z’) || (‘A’ <= ch && ch<=‘Z’) ); • even = (n%2 == 0) ;
Complementing a Condition and Morgan’s Theorem r !(item ==SENT) r !(status==‘s’ && age > 25) r Morgan’s Theorem l The complement of expr 1 && expr 2 as comp 1 || comp 2 l The complement of expr 1 || expr 2 is comp 1 && comp 2 r Exercise 4 : Given x=15. 0 and y = 25. 0, evaluate the following expressions : l x!=y l x<x l x >= y l x == y +x –y
r Exercise 5 : Given a =5, b=10, c=15 and flag =1. Use the short circuit evaluation : l c == a+b || !flag l a!= 7 && flag || c>=6 l !(b<= 12) && a%2 ==0 l !(a > 5 || c < a+b) l Complement each expression. l What is the value assigned to ans of type int if p=100 and q=50 ans = (p>95) +(q<95);
Programming r Write an expression to test the following : l age is from 18 to 21 inclusive. l water is less than 1. 5 and also greater than 0. 1 l year is divisible by 4. l speed is not greater than 55. l y is greater than x and less than z. l w is either equal to 6 or not greater than 3. l Assign 1 to between if n is the range –k through +k l Assign 1 to uppercase if ch is uppercase letter; otherwise assign a value of 0. l Assign a value of 1 to divisor if m is divisor of n; otherwise assign it the value of 0.
If Statement r What do the statements display ? l If(12 <12) printf (" less" ); else printf (" not less" ); l var 1= 25. 12, var 2= 15. 00 if( var 1 <= var 2) printf (" less or equal" ); else printf (" greater than" ); r What is the value assigned to x when y = 15. 0 ? l x =25. 0; If (y!= (x-10. 0)) x=x-10. 0; else x=x/2. 0;
r Write C statements : l If item is nonzero, then multiply product by item and save the result in product, otherwise skip the multiplication. In either case, print the value of product. l If x is 0, add to zero_count. If x is negative, add x to the minus_sum. If x is greater than 0, add x to plus_sum. r Compute the population growth from yesterday to today as a percentage of yesterday’s population. r Trace the following program given x =12. 5, y=5. 0, temp=? : if (x>y) { temp=x; x=y; y=temp; }
Exercise 6 r Write an if statement that might be used to compute and display the average of a set of n numbers whose sum is stored in variable total. This average should be only if n>0, otherwise an error message should be displayed.
Multiple-Alternative Decision r Write a C program that shows the relationship between noise and human perceptions of noises. Loudness in Decibels (db) Perception ---------------------------------50 or lower quiet 51 -70 intrusive 71 -90 annoying 91 -110 very annoying above 110 uncomfortable
Exercice 7 r A store clerk gains a raise of 10% of his salary if all the sales he has performed exceed 30, 000 DH worth of the merchandise each month, an only 5% raise if the sales are below 30, 000 DH. Write a C program that shows the clerk man how to figure out what is his raise. r Explain the difference between : If( x >= 0) x = x+1 else if (x>=1) x= x+2 And If (x >= 0) x = x+1 if (x>=1) x= x+2
Exercise 8 Write a program to show the expected brightness of a standard light bulb given its wattage. Display Unknown bulb if the watts input is not in the table. Watts Brightness (in Lumens) ----------------------- 15 125 25 215 40 500 41 60 880 42 75 1000 43 100 1675
- Slides: 15