Sunteți pe pagina 1din 4

# COLLEGE OF ENGINEERING & TECHNOLOGY

## Department: Computer Engineering (Cairo Branch)

Course Code: CC112 Sheet: 3

## Course : Structured Programming

Topics: Selection structures and switch statement

____________________________________________________________
Level 1:
Topic If statement 1) Program Find the error in the following statements If a == 5 printf (Hello); If ( a < b ); printf (ok); If ( x = y ) printf (equal); If ( t < r) x=2; t=r; else printf Float x; x=2; switch (x) { case ( 2 ) printf(Hello); break; } What is the purpose of the default part of the switch command The else and default parts of selection statements, are they optional or mandatory? Write a program that takes two integers and displays their minimum.

Switch Selection

2) 3) 4)

Level 2:
Topic Tracing 5) Program Find the value of x for each of the following program segments a) Program segment 1 int x = 2 ; if ( x >= 0 ) x++; else if ( x >= 1 ) x += 10; printf ( %d, x ) ; b) Program segment 2 int x = 2 ; if ( x >= 0 ) 1
Version 13a

x++; if ( x >= 1 ) x += 10 ; printf ( %d, x ) ; 6) a. b. c. What will be the value of x when y=12.0 a) Program segment 1 float x = 25.0, y ; scanf ( %d, &y ) ; if ( y != (x - 10.0 ) ) x = x - 10.0 ; else x = x / 2.0 ; printf ( x = %f, y = %f \n, x , y ) ; b) Program segment 2 float x = 25.0, y ; scanf ( %d, &y ) ; if ( y < 12.0 ) if ( y >= 0.0 ) x=5*y; else x=2*y; else x=3*y; printf ( x=%f,y=%f\n, x, y); Tracing / switch 7) What will be the value of y after the following switch statement is executed? x=3; switch (x+3) { case 6: y = 1 ; default: y += 1 ; } Use a switch statement to rewrite the following if statement if ( a == 1 ) x += 5 ; else if ( a == 2 ) x += 10 ; else if ( a == 3 ) x += 16 ; else if ( a == 4 ) x += 34 ; Write a nested if statement to display a message that indicates the educational level of a student based on his or her number of schooling: 0-none 1 through 5-Elementary School 6 through 8-Middle School 9 through 12-High School >12- College. Print a message to indicate invalid data as well. Write a program that may be used to compute the area of a square (area=side2) or a triangle (area=0.5*base*height) after prompting the user to type the first character of the figure name (S or T). Enter required data according to the figure being handled. 2
Version 13a

Switch

8)

If

9)

10)

Selection

11)

Write a C program read the student score and display the corresponding grade according to the following table: Score (x) 85<=x<=100 75<=x< 85 65<=x< 75 50<=x< 65 x<50 Grade A B C D F

12)

Credit Card Validator Takes in a credit card number of a common credit card vendor (Visa, MasterCard, American Express, Discoverer) and validates it to make sure that it is a valid number (look into how credit cards use a checksum).

Level 3:
Topic Selection/Arithmetic 13) Program Change Return Program The user enters a cost and then the amount of money given. The program will figure out the change and the number of L.E., Piaster needed for the change. Write a program that reads an integer and determines whether it is even or odd. You have just started a sales job in a department store. Your salary consists of a base salary and a commission. The base salary is \$5,000. The following scheme is used to determine the commission rate: Sales Amount \$1-\$5,000 \$5,001-\$10,000 \$10,001 and above Commission Rate 8% 10 % 12 %

14) 15)

Write a program that reads the amount of your sales and computes your salary. 16) Write a program that determines the state tax owed by an employee. The state charges a 4% tax on net income. Determine net income by subtracting a \$500 allowance for each dependent from gross income. Your program will read gross income, and number of dependents. It will then compute the actual tax owed and print this value on the screen. Develop a C program that will determine if a department store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available: a. Account number b. Balance at the beginning of the month c. Total of all items charged by this customer this month d. Total of all credits applied to this customers account this month e. Allowed credit limit The program should input each of these facts, calculate the New balance (=beginning balance + charges credit), and determine if the new balance exceeds the customers credit limit. For those customers whose credit limit is exceeded, the program should display the customers account number, credit limit, new balance, and the message Credit limit exceeded. The program should perform this calculation for one customer only. Write a program that may be used to convert either from entered distance in miles to kilometers or from entered distance in kilometers to miles (1 mile=1.5 kilometers) after prompting the user to type the first character of the unit of the measured distance (K or M). Enter the distance according to the entered unit. 3
Version 13a

17)

18)

4
Version 13a