Anda di halaman 1dari 3

B.

Eng (Hons) Electrical and Electronic Engineering LAB3 : MULTIPATH DECISION LOGIC Learning Outcomes/Assessment Criteria: LOC 2 : Recognise when to use decision logic Lab Objectives
This Lab is designed to reinforce programming concepts. In this lab you will practice: 1. logical expression 2. nested if and switch statement

Lab Instruction
PART A Nested if is used when multiple condition exists in controlling the flow of the program. When the condition is tested against a series of constant, we can use switch statement. #include <iostream.h> main() { char ch = b ; if ( ch = = a ) { cout << Your grade is A\t; cout << Excelent << ch<<endl ; } else if ( ch = = b ) {cout << Your grade is B \t ; cout << Very Good <<endl ; } else { cout << You have to study harder;} } #include <iostream.h> main() { char ch = b ; switch( c ) { case 'a': cout << Your grade is A \t cout << Excelent <<endl ; break ; case 'b': cout << Your grade is B \n; cout << Very Good <<endl ; break default : cout << You have to study harder; }

B. Eng (Hons) Electrical and Electronic Engineering Exercise 1.


a. Compile both of the programs above using Visual C++ . b. Fix the error and excecute the program, write the output.

Exercise 2.
a. Using the same program, change the variable declaration statement in both programs into ch =a, write the output.

b. Remove all break statements at switch program, write the output.

c. Change the value of ch into A from the declaration of variable statement and run the modified program, write the output.

B. Eng (Hons) Electrical and Electronic Engineering


d. From the program with switch statement add statement caseA: right after the statement case a : and for program with if else statement change the conditional statement if ( ch = = a) to if ( ch = = a || ch = = A). Run both programs and compare with the previous result and give explanation if the output is different.

e. Modify both programs so that if the value of ch is c or d the program will also display proper message (assuming the value of ch is your mark). Attach the program on your report. PART B For part B exercises you prepare the program at home, you only demonstrate your program correctly in the next lab session.

Exercise 3.
Write a program that will prompt the user to enter a number from 1 to 12. Your program will display the name of the month that corresponds to the number entered and provide proper message if the user keys in incorrect number.

Exercise 4.
Write a program to display the value of commonly available resistor with proper prefix. If the value is less than 1000 ohms it will display without any prefix. If the value is between 1000 to 999999 ohms it will display with prefix K and if the value of the resistor is one million or above it will display with prefix M. The value of the resistor is given interactively.

Exercise5.
Write a program to ask the user to guess a number (1-100). Guide the user by giving a message either the attempted number is smaller or bigger than the correct number. Give the user 5 times to guess. If the user can guess correctly before the chances run out, display good compliment.

Anda mungkin juga menyukai