Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

FAKULTI TEKNOLOGI DAN KEJURUTERAAN

ELEKTRONIK DAN KOMPUTER


UNIVERSITI TEKNIKAL MALAYSIA MELAKA

PROGRAMMING FUNDAMENTAL

BERC 1313 SEMESTER 2 SESI 2023/2024

LAB 2: SELECTION CONTROL TECHNIQUES

NO. STUDENTS' NAME MATRIC. NO.

1. AINA BALQIS BINTI MUHAMMAD SYAFIQ B122310078

2.

3.

1BERT S1/1 1BERT S1/2 1BERT S2/1 1BERT S2/2 1BERT S3/1
PROGRAMME/
SECTION
1BERE (Intake Sem2)

GROUP LAB G1 G2 G3 G4 G5 G6 G7 G8 G9 G10

DATE 3.4.2024

1. DAYANASARI BINTI ABDUL HADI

2. MA TIEN CHOON
NAME OF
INSTRUCTOR(S)
3. TS. IMRAN BIN HINDUSTAN

4. TS. DR. HASRUL 'NISHAM BIN ROSLY

EXAMINER’S COMMENT(S) TOTAL MARKS


Laboratory Assessment Rubrics
BERC1313: Fundamental Programming
2023/2024 SEM II
ACTUAL
Lab Title LAB 2: SELECTION CONTROL TECHNIQUES Matrix ID COURSE / SECTION
MARK
BERT 1/1
1.AINA BALQIS BINTI MUHAMMAD SYAFIQ B122310078 /6

Students' Names 2 /6

3 /6

Assessment Level Assessment Criteria STUDENT 0 1 2 3 4 Weightage MARK


1 /1
P2 Ability to shows proper standard
in C program. 2 0.25 /1
(Readiness)
3 /1
1 /2
Laboratory Construct program codes that free
P3
from syntax/logic or runtime 2 0.50 /2
Experiment (Attempt) errors.
3 /2
1 /3
P4
Display the output/result of
(Basic experiment. 2 0.75 /3
Proficiency)
3 /3

Total Student 1 /6

Total Student 2 /6

Total Student 3 /6

0.0

1
1.0 PRE-LAB SURVEY

Tick the relevant response that describe you for each statement below:

Strongly Strongly
Agree Neutral Disagree
Agree Disagree
I am familiar with C Integrated
Development Environment.
I able to produce flowchart and write
program based on problem given.
I able to apply basic selection using if-
else
I able to apply basic selection using two
way if-else if-else
I able to apply basic selection using
switch-case
I able to write program that receive
inputs from user, do decision making
base on few choise, and display output
to user.

2.0 OBJECTIVES
1. To familiarize with C programming control structure.
2. To create a flowchart based on software requirement.
3. To perform programming by creating a flowchart.
4. To work effectively in a given task in the group.

2.0 EQUIPMENT
1. Personal Computer / desktop
2. Software: CodeBlocks

1
3.0 SYNOPSIS & THEORY
3.1 Decision Making
Executable statements either perform actions (calculations, input/output of data)
or make decisions. We might make a decision in a program, for example, to determine
the student’s grade on an exam whether passed or failed. So that, it is necessary for the
program to make a decision based on the truth or falsity of a statement of fact called a
condition. If the condition is true (condition is met), the statement in the body of if
statement is executed. If the condition is false (condition isn’t met), the body of the
statement isn’t executed.
Condition in selection statements is formed by using tha equality operators and
relational operators as in Table 2.1. The relational operators all have the same level of
precedence and they associate left to right. The equality operators have a lower level of
precedence than the relational operators and also associate from left to right.

Table 2.1: Equality and relational operators.


Algebraic equality or C equality or Example of C Meaning of C condition
relational operator relational operator condition

Relational operators
> > x > y x is greater than y
< < x < y x is less than y
≥ >= x >= y x is greater than or equal to y
≤ <= x <= y x is less than or equal to y

Equality operators
= == x == y x is equal to y
≠ != x != y x is not equal to y

Conditions in terms of the relational and equality operators only allow each
decision to be precisely tested by one condition. However, to test multiple conditions in
the process of making decision, we had to perform these tests in separate statements or
in nested if or if...else statements. C provides logical operators that may be used to
form more complex conditions by combining simple conditions.

2
Logical C logical Example of C condition Meaning of C condition
operators operator

NOT ! if (!(CGPA >= 2.0)) reverse the meaning of a


condition

if (gender == 1 && age >= 65) true if and only if both of the
AND
&& simple conditions are true

if (physics >= 80 || math true either or both of two


OR
|| >=80) conditions are true

The && operator has higher precedence than ||. Both operrators associate from
left to right. However, both logical operators have lower precedence than equality and
relational operators. Except for NOT logical operator, it has a higher perecedence
among all other logical AND and OR operators as well as equality and relational
operators.

3.2 Selection Control Structure


C provides three types of selection structures in the form of statements as follows:
a) if selection
The if statement is called a single-selection statement because it selects or
ignores a single action. It will either selects (performs) an action if a condition is
evaluated as true or skips the action if the condition is false.

b) if...else selection
The if...else statement is called a double-selection statement because it
selects between two different actions. It will perform an action if a condition is
true and performs a different ation if the condition is false.

c) switch selection
The switch statement is called a multiple-selection statement because it
selects among many different actions. It will perform one of many different
actions, depending on the value of an expression.

3.2.1 The if selection statement

3
Selection statements are used to choose among alternative course of action. The
if selection statement performs an indicated action only when the condition is true,
otherwise the action is skipped. As an example, lets say the passing mark on an exam is
60. The pseudocode statement

If the student’s mark is greater than or equal to 60


Print “Passed”
determines whether the condition “student’s grade is greater than equal to 60” is true or
false. If the condition is true, then Passed is printed, and the next pseudocode statement
in order is performed. However, if the condition is false, the printing is ignored, and the
next pseudocode statement in order is performed. The C program segment reflecting this
pseudocode if statement may be wriitten as,

if (mark >= 60)


{
printf(“Passed”);
}

Flowchart for single-selection if statement is illustrated in Figure 2.1. This


flowchart contains what is perhaps the most important flowcharting symbol – the
diamond symbol, also called the decision symbol. The decision symbol contains an
expression, such as a condition, tat can be either true or false. The decision symbol has
two flowlines emerging from it. One indicates the direction to take when the expression
in the decision symbol is evaluated as true and the other direction to take when the
expression is false. Decisions can be based on conditions containing ralational or
equality operators. In fact, a decision can be based on any expression – if the
expression evaluates to zero, it’s treated as false, and if it evaluates to nonzero, it’s
treated as true.

true
mark >= 60 print “Passed”

4
Figure 2.1: Flowcharting the single-selection if statement.

3.2.2 The if...else statement


The if...else selection statement allows you to specify the different actions
are to be performed when the condition is true and when it’s false. Let consider the
following pseudocode statement,

If the student’s mark is greater than or equal to 60


Print “Passed”
else
Print”failed”

It will print Passed if teh student’s mark is greater than or equal to 60 and failed if the
student’s mark is less than 60. In either case, after printing occurs, the next pseudocode
statement in sequence is performed. Note that, the body of the else is also indented. The
if...else statement may be written in C as

if (mark >= 60)


{
printf(“Passed”);
}
else
{
printf(“failed”);
}

The flowchart in Figure 2.2 illustrates the flow of control in the if...else
statement.

true
mark >= 60 print “Passed”

false
print “Failed”

5
Figure 2.2: Flowcharting the double-selection if…else statement.

3.2.3 The switch statement


Ocassionally, an algorithm will contain a serie of decisions in which a variable
or expression is tested separately for each of the constant integral values it may assume,
and different actions are taken. This is called multiple selections. C provides the
switch multiple-selection statement to handle such decision making. The switch
statement consists of a series of case labels, and optional default case and
statements to execute for each case. Consider following C program in Figure 2.3.
1 #include <stdio.h>
2
3 int main ()
4 {
5 char choice;
6
7 printf ("\n\n\tMenu");
8 printf ("\n\ta. Coke ");
9 printf ("\n\tb. Sprite ");
10 printf ("\n\td. Soy Drink ");
11
12 printf ("\n\tPlease enter your choice : ");
13 choice = getchar();
14
15 switch (choice)
16 {
17 case 'A':
18 case 'a':
19 printf ("\n\tYou choose Coke");
20 printf ("\n\tPrice RM 2.00");
21 break;
22
23 case 'B':
24 case 'b':
25 printf ("\n\tYou choose Sprite");
26 printf ("\n\tPrice RM 1.90");
27 break;
28
29 case 'C':
30 case 'c':
31 printf ("\n\tYou choose Mirinda Orange");
32 printf ("\n\tPrice RM 1.50");
33 break;
34
35 default :
36 printf ("\n\n\tPlease enter the valid selection");
37
38 }
39
40 return 0;
41 }

6
Figure 2.3: Choosing drink with switch.
In the program, the user enters a letter to choose a drink. The getchar function reads
one character from the keyboard and stores that character in the chracter variable choice.
Keyword switch is followed by the variable name choice in parentheses. This is called
the controlling expression. The value of this expression is compared with each of the case
labels. Assume the user has entered the letter C as a choice. C is automatically compared to
each case in switch. If a match occurs (case ‘C’:) , the statements for that case are
executed. In the case of letter C, two printf statements are printed, and the switch
statement is exited immediately with the break statement.
The break statement causes the program control to continue with the first statement
after the switch statement. The break statement is used because the case in a switch
statement would otherwie run together. If a break is not used anywhere in a switch
statement, then each time a match occurs in the statement, the statements for all the remaining
cases will be executed. If no match occurs, the default case is executed, and an error
message is printed. Each case can have one or more actions. The switch statement is
different from all other control statements in that braces are not required around multiple
actions in the case of a switch. The general switch multiple-selection statement is
flowcharterd in Figure 2.4.

case a true
case a action(s) break

false
true
case b case b action(s) break

false

case b case b action(s) break

false

default action(s)

7
Figure 2.4: Flowcharting the multiple-selection switch statement.
4.0 PROCEDURE
Section A:
Using the if-else selection control structure, write a complete executable C program that
takes two non-negative numbers (exclude zero) as input from the user. The program is able to
determine which is the larger of the two numbers. The program also able to determine if both
numbers are equal.
The program should also tell which of the entered numbers is even or odd. For odd number/s,
find its square root and for even number/s calculate its value with power of 2 (i.e: 10 2 = 100).
Use the math function to find the square root and power of 2.

sqrt(a) - to find the square root of a.


pow(a,b) - to find the a with the raise of power b.

Hint: Include <math.h> library in your source file.

1. Draw the flowchart to represent the software requirements.


2. Based on the flowchart, write the program in CodeBlock.
3. Compile or build the project and run the program.
4. Kindly draw the appropriate table which includes the all possible inputs combination,
and also the expected result from your program. Please do verify that your program output
is exactly as per your table.
5. Record your observations.

8
Section B:
Write 2 complete executable C program to calculate and display the tax payment based on
given flowchart below. Display your tax in 2 decimal places.
First program using if statement.
Second program using if-else statement.

read age, marriage, salary

1. Based on the flowchart, write the program in CodeBlock.


2. Compile or build the project and run the program.
3. Verify your program with all possible inputs combinations (age, marriage, and salary).
4. Assume salary to be RM 1,000.00 as an input.
5. Record your observations.

9
Section C:
Using the case statement, write a program that displays the following menu:

Geometry Calculator If
1. Calculate the Area of a Circle.
2. Calculate the Area of a Rectangular.
3. Calculate the Area of a Triangle
4. Quit.

Enter your choice (1-4) :

the user enters 1, the program should ask for the radius of the circle and then display its area.
Use the following formula:
area = pi * r2
Use 3.14159 for pi and radius of the circle for r.

If the user enters 2, the program should ask for the length and width of the rectangle and then
display the rectangle’s area. Use the following formula:
area = length * width

If the user enters 3, the program should ask for the length of the triangle’s base and its height,
and then display its area. Use the following formula:
area = base * height * 0.5

If the user enters 4, the program should end.

Input Validation: Display an error message if the user enters a number outside the range of 1
through 4 when selecting an item from the menu. Do not accept negative values for the
circle’s radius, the rectangle’s length or width, or the triangle’s base or height.

1. Write the program in CodeBlock.


2. Compile or build the project and run the program.
3. Verify your program with all choices (1-4) with the appropriate input values.
4. Record your observations.

10
5.0 RESULTS
SECTION A
a. Write/Paste your flowchart inside a this block:

b. Write/Paste your code inside a this block:

11
c. All possible inputs & Prediction based on your logic: [you can edit table as desired]

12
Table 1

Input test Number 1 Number 2 Answer


Second number is larger
7 is the first number you entered , and it is odd.
1 7
15 Square root is 2.65
15 is the second number you entered, and it is odd.
Square root is 3.87
Second number is larger
20 is the first number you entered and it is
2 40
20 even.Raised to the power of 2 is 400
40 is the second number you entered and it is even
raised to the power of 2 is 1600
Second number is larger
25 is the first number you entered, and it
3 25
35 is odd.Square root is 5.00
35 is the second number you entered, and it is
odd.Square root is 5.92
4 -27 35 Please enter non-negative numbers

d. Paste your output program based on the Table 1 in this block:


Input
Output
Test

13
SECTION B
a. Write/Paste your code inside a this block (if statement):

b. Write/Paste your output program based on code in Section B (a) in this block for all
possible cases:
Input Marriage
Age Output
Test Status
1 25 no Your Age is: 25
Your Salary (RM) is: 1500
Are you married (yes(y) or no(n)): n

14
Your tax is RM 150.00

Your Age is: 35


Your Salary (RM) is: 2300
2 35 yes Are you married (yes(y) or no(n)): y

No Tax for you!!

Your Age is: 45


Your Salary (RM) is: 5000
3 45 yes Are you married (yes(y) or no(n)): y

No Tax for you!!

Your Age is: 22


Your Salary (RM) is: 1800
4 22 single Are you married (yes(y) or no(n)): single

Error input, Please enter yes or no.

c. Write/Paste your code inside a this block (if-else statement):

#include <stdio.h>
int main()

{
int age;
float salary, tax;
char married;

printf("Your Age is :");


scanf("%d", &age);

printf("Your Salary (RM) is :");


scanf("%f", &salary);

printf("Are you married (yes(y) or no(n)):");


scanf(" %c", &married);

if (age >= 30)


{
if(married == 'y' || married == 'Y')
{
printf("\nNo Tax for you!!\n");
}
else if(married == 'n' || married == 'N')
{
printf("\nYour tax is %.2f\n", salary*0.025);
}
else
{
printf("\nError input, Please enter yes or no.\n");
}
}
else
{

15
if(married == 'y' || married == 'Y')
{
printf("\nYour tax is %.2f\n", salary*0.05);
}
else if(married == 'n' || married == 'N')
{
printf("\nYour tax is %.2f\n", salary*0.1);
}
else
{
printf("\nError input, Please enter yes or no.\n");
}
}
return 0;
}

d. Write/Paste your output program based on code in Section B (c) in this block for all
possible cases:
Input Marriage
Age Output
Test Status

1 45 Y

2 21 no

3 50 no

4 34 single

16
SECTION C
a. Write/Paste your code inside a this block:
#include <stdio.h>
int main()
{
int choice;
float r, length, width, base, height;
char Quit;
const float pi = 3.14159;

printf("Geometry Calculator\n\n");
printf("\tCalculate the Area of a Circle.\n");
printf("\tCalculate the Area of a Rectangular.\n");
printf("\tCalculate the Area of a Triangle.\n");
printf("\tQuit.\n");

printf("\nEnter your choice (1-4) :");


scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Please enter the radius of the circle :");
scanf("%f",&r);
if(r < 0)
{
printf("Please enter the positive value!!\n");
}
else
{
printf("The area of circle is : %.2f\n",pi*(r*r));
}

break;

case 2:
printf("Please enter the length of the rectangle:");
scanf("%f",&length);
printf("Please enter the width of the rectangle :");
scanf("%f",&width);
if(length < 0 || width < 0)
{
printf("Please enter the positive value!!\n");
}
else
{
printf("The area of rectangle is : %.2f\n",length*width);
}

break;

case 3:
printf("Please enter the length of the triangle's base :");
scanf("%f", &base);
printf("Please enter the height of the triangle :");
scanf("%f", &height);
if(base <0 || height<0)
{

17
printf("Please enter the positive value!!");
}
else
{
printf("The area of triangle is : %.2f\n",base*height*0.5) ;
}

break;

case 4:
printf("Existing program. \n");
Quit;
break;

default:
printf("Invalid choice. Please enter a number between1 and
4. \n");
}
return 0;
}

b. Paste your output program in this block:


Input Output
Test

18
3

6.0 POST-LAB SURVEY


Tick the relevant response that describe you for each statement below:
Strongly Neutra Strongly
Agree Disagree
Agree l Disagree
I am familiar with C Integrated
Development Environment.
/
I able to produce flowchart and write
/
program based on problem given.
I able to apply basic selection using
/
if-else
I able to apply basic selection using two
/
way if-else if-else
I able to apply basic selection using
/
switch-case
I able to write program that receive
inputs from user, do decision making
/
base on few choise, and display output
to user.

7.0 ADDITIONAL (IMPROVISED) ASSESSMENT

19
Note for lab invigilator: Please fill the following table if student being asked additional
question(s) or tak(s) during the laboratory session.

No Question / Task Comment & Mark ( 0 to 5 ) Signature

20

You might also like