0% found this document useful (0 votes)
35 views

ENG3202 Lab Report 4 - Switch

ENG3202 Lab Report 4 - Switch UPM , LAB REPORT 4 , COMPUTER PROGRAMMING This is example of Lab report that I sent to my Lecturer, Hopes it Helps.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

ENG3202 Lab Report 4 - Switch

ENG3202 Lab Report 4 - Switch UPM , LAB REPORT 4 , COMPUTER PROGRAMMING This is example of Lab report that I sent to my Lecturer, Hopes it Helps.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Lab-Report 4

ENG3202
Name: Fakhrul Hakim bin Anwar
No. Matrik: 228724

November 13

COMPUTER PROGRAMMING
LAB 4: SWITCH

Objective To study and apply the concept of selection statement


To distinguish between the decision making tool in the form of if..else and
switch..case

Equipment Personal Personal Computer with C Program Pre-Installed

General guideline for all lab exercises and reports:


a. Generate a pseudocode for the program.
b. Generate a flow chart for the program
c. Provide relevant comments on the codes
d. In the case of compiling given program, run the program and record any errors
encountered.
e. Debug the error, compile and run the program. Record the program output.
f. If you are required to design a program, design working codes, debug and run aswell
as show the output.
g. Provide discussion and analysis of your results.
h. You may copy the program and results images or screenshot from the IDE and
paste it into your report
Questions 1

1. Write a program using switch..case that will display a corresponding color code
according to a user input number. The corresponding color code is given in Table
4.1 below.

Table 4.1
0 = Black 3 = Orange 6 = Blue 9 = White
1 = Brown 4 = Yellow 7 = Violet
2 = Red 5 = Green 8 = Gray

Pseudocode :
1) Start The program.
2) Set a variable to be user input numerical value between 0 to 9
3) If the variable is equal to 0, Then print the text ‘Black’
4) If the variable is equal to 1, Then print the text ‘Brown’
5) If the variable is equal to 2, Then print the text ‘Red’
6) If the variable is equal to 3, Then print the text ‘Orange’
7) If the variable is equal to 4, Then print the text ‘Yellow’
8) If the variable is equal to 5, Then print the text ‘Green’
9) If the variable is equal to 6, Then print the text ‘Blue’
10) If the variable is equal to 7, Then print the text ‘Violet’
11) If the variable is equal to 8, Then print the text ‘Gray’
12) If the variable is equal to 9, Then print the text ‘White’
13) If the user input the variable not the number 0 to 9 , print " Please choose 0 to 9 only, program
will restart "
14) End the program
Flow Chart
Debug
Output
2. Rewrite the program in Question 1 using nested if..else statement

Pseudocode :
1) Start The program.
2) Set a variable to be user input numerical value between 0 to 9
3) If the variable is equal to 0, Then print the text ‘Black’
4) If the variable is equal to 1, Then print the text ‘Brown’
5) If the variable is equal to 2, Then print the text ‘Red’
6) If the variable is equal to 3, Then print the text ‘Orange’
7) If the variable is equal to 4, Then print the text ‘Yellow’
8) If the variable is equal to 5, Then print the text ‘Green’
9) If the variable is equal to 6, Then print the text ‘Blue’
10) If the variable is equal to 7, Then print the text ‘Violet’
11) If the variable is equal to 8, Then print the text ‘Gray’
12) If the variable is equal to 9, Then print the text ‘White’
13) If the user input the variable not the number 0 to 9 , print " Please choose 0 to 9 only, program
will restart "
14) End the program
Flow Chart
Debug
Output
Question 3

Table 4.2
Selangor/Faderal Territory of Kuala Lumpur & Price per m3
Putrajaya

Home (H)
0-20 m3 RM 0.57
More than 20 m3 RM 0.91
Minimum Charge RM 5.00

Industrial (I)
0-35 m3 RM 1.80
More than 35 m3 RM 1.92
Minimum Charge RM 30.00

Commercial (C)
0-35 m3 RM 2.00
More than 35 m3 RM 2.87
Minimum Charge RM 20.00

3. Write a program that will calculate and print out bills for Syarikat Air Selangor
Sdn. Bhd. (SYABAS). The water rates vary depending on whether the bill is for
home use, commercial use, or industrial use (refer to Table 4.2). A code of h means
home use, a code of c means commercial use and a code ofI means industrial use.
Any other code should be treated as an error. Thewater rates are computed as
given in the table.

Your program should prompt the user to enter an account number (type int), the
code (type char) and the volume (m3) of water used (type float). Your program
should echo the input data and print the amount due from the user. You must use
both if and switch statements.
Pseudocode:
Start the program
Prompt the user to enter 4 Variable
- Account number, A set as Float Value
- Code for Home(h) , or Industrial(I) or Comercial(c) and set as Character
- Volume of Water, V used set as float Value

if the user choose (h) , Then if (V<8.77193) then print ‘ Charges is = RM5.00 ‘
else if (V>8.77193 && V<=20) print “ Charges is : RM(calculate Value of charges = V*0.57) “
else if (V>20) then Print “ Charges is : RM(calculate Value of charges = 11.4 + (V-20)*0.91)”

if the user choose (I), Then if (V<16.66667) then print ‘Charges is = RM30.00’
else if (V>16.66667 && V<=35) print “ Charges is : RM(calculate Value of charges = V*1.80) “
else if (V>35) print “ Charges is : RM(calculate Value of charges = 63 + (V-35)*1.92) “

if the user choose (c), Then if (V<=10) then print ‘ Charges is = RM20.00 ‘
else if (V>20 && V<=35) Then print “ Charges is : RM(calculate Value of charges = V*2.00) “
else if (V>35) , Then print “ Charges is : RM(calculate Value of charges = 70 + (V-35)*2.87) “

If the user choose neither h,I,c then print ‘( Invalid Code )’


End Program

Flow Chart
Debug
Output
Discussion and Analysis

Question 1: Color Code Program Using switch..case


The first program leverages the switch..case statement to map integers from 0 to 9 to their respective
color codes. The structure of the switch statement ensures clear and straightforward logic. Each case
explicitly defines the output for a specific input, while the default case provides feedback for invalid
entries.
• Strengths:
o Readability: The program is well-organized, with each case corresponding directly to a
color code. This makes the code easy to follow and maintain.
o Efficiency: The switch statement directly jumps to the matching case without checking
unnecessary conditions, which makes it faster than sequential if..else comparisons.
o Error Handling: The default case ensures that invalid inputs outside the range of 0-9 are
handled gracefully by prompting the user to enter a valid number.
• Limitations:
o Input Validation: While the program informs the user to input numbers within the valid
range, it does not prevent or handle non-integer or non-numeric inputs. This could lead
to unpredictable behavior if invalid input is provided (e.g., a character or floating-point
number).
o
Question 2: Color Code Program Using if..else
The second program replicates the functionality of the first but uses a series of if..else statements. While
the logic remains functionally identical, the differences in structure and execution highlight the trade-
offs between the two approaches.
• Strengths:
o Versatility: The if..else structure is more flexible and can accommodate complex
conditions, such as ranges or combined logic, making it more adaptable than
switch..case for such scenarios.
o Functionality: The output matches the requirements, correctly displaying the color code
for valid inputs and handling invalid inputs with a message.
• Limitations:
o Readability: The sequential nature of if..else makes the code longer and harder to read,
especially for a larger set of conditions. Adding new conditions in this format may
increase the potential for errors and reduce maintainability.
o Performance: The program evaluates conditions sequentially, which could lead to
inefficiencies for large-scale cases. While negligible for this program, this could impact
performance in more complex scenarios.
o
Question 3: Water Billing Program Using switch..case and if..else
The third program uses a hybrid approach, combining switch..case to handle customer types (h, I, c) and
if..else to calculate water charges based on usage volumes. The program also incorporates minimum
charges and progressive rates, effectively mirroring real-world billing systems.
• Strengths:
o Hybrid Approach: By using switch..case to handle customer types, the program avoids
redundant checks and ensures clear categorization. The subsequent if..else structure
allows for detailed computations of water charges.
o Correctness: The logic for calculating charges aligns with the given table, including
applying minimum charges for low usage. Progressive rates are accurately computed,
reflecting the incremental cost for usage beyond thresholds.
o Output Formatting: The use of %.2f ensures that monetary values are displayed with
two decimal places, to make sure it use money format RM0.00 which is 2 decimal only
o Error Handling: The default case in the switch ensures that unrecognized codes are
flagged, prompting the user to input valid data.
• Limitations:
o Input Validation: The program does not validate the account number or ensure that
volume usage is positive. Negative or non-numeric inputs could disrupt program
execution or produce incorrect results.

You might also like