Course: CPE 102B Experiment No.
: 4
Name: Delos Reyes, Reynan Allan I. Section: IE21S1
Date Performed: September 15, 2020
Date Submitted: September 16,2020
Instructor: Engr. Cherry D. Casuat
LABORATORY ACTIVITY NO. 4
CONTROL STRUCTURE
(Selective Structure)
1. Objective(s):
The activity aims to introduce a selective structure using analytical tools such as algorithm and flowchart to
evaluate the output based on the requirement whether true or false.
2. Intended Learning Outcomes (ILOs):
The students shall be able to:
1. Understand on how to use and apply selective structure in constructing a program.
2. Construct a program using switch statement.
3. Convert a nested if-else statement into another program applying switch statement.
3. Discussion:
A switch statement is used in place of many if statements. The variable to be tested comes after the
switch statement, and it can be variable, an expression or the result of a function. It is built multiple branch
decision statement in C++. A variable is successively tested against a list of integer or character
constants. When a match is found, a statement or block of statement is executed.
The use of break and default
Break is used to limit the execution of the program for a specific case only. The break statement forces
an early with from one layer of looping constructs. While default is used when no match was found among
the case stated, the default operation will be executed.
Syntax:
switch (expression)
{
case valueOne: statement;
break;
case valueTwo: statement;
break;
....
case valueN: statement;
break; default:
statement;
}
4. Resources:
DevC++
5. Procedure and Output
EXERCISE No.1
Mary Jane would like to invest her money amounting 100,000 at the bank. Before investing her
money, she would like to determine which type of account that she will chose. The amount of interest that her
money earns depends on which type of account the money is in. The bank has 6 different types of accounts
and they earn interest as follows:
Account type Interest earned
personal financial 1.5%
personal homeowner 2.0%
personal gold 2.5%
small business 3.0%
big business 3.5%
gold business 4.0%
Mary Jane as a programmer would like to make a program that computes automatically the interest earned of her
money in each type of account.
REQUIREMENTS:
1. Write the corresponding algorithm
a. Narrative
b. Pseudocode
2. Create the equivalent flowchart based on the algorithm of the given problem.
3. Construct the program and record your screen display result.
NARRATIVE ALGORITHM PSEUDOCODE
To determine which type of Step 1: Start 1. Start
account that Mary Jane will chose
before investing her money. Step 2: Input asks the user’s 2. Input the user’s account
balance and account type type, and balance.
Step 3: If account type is 3. If account type is
“personal financial” and balance “personal financial”, then
>= 1500, then print “Interest Rate print “Interest Rate is
is 1.5%” else, 1.5%” else,
If account type is If account type is
“personal homeowner” “personal homeowner”,
and balance >= 2000, then print “Interest Rate
then print “Interest Rate is 2.0%” else,
is 2.0%” else,
If account type is
If account type is “personal gold, then print
“personal gold” and “Interest Rate is 2.5%”
balance >=2500, then else,
print “Interest Rate is
2.5%” else, If account type is “small
business”, then print
If account type is “small “Interest Rate is 3.0%”
business” and balance else,
>=3000 , then print
“Interest Rate is 3.0%” If account type is “big
else, business”, then print
“Interest Rate is 3.5%”
If account type is “big else,
business” and balance
>= 3500, then print If account type is “gold
“Interest Rate is 3.5%” business”, then print
else, “Interest Rate is 4.0%”.
If account type is “gold Step 4: Print the account type.
business” and balance
>= 4000, then print Step 5: End
“Interest Rate is 4.0%”.
Step 4: Print the account type.
Step 5: End
FLOWCHART
CODES FOR TYPE OF ACCOUNTS
#include <iostream>
using namespace std;
int main()
{
char Type_Account;
float Total;
cout<<" Type of Account"<<endl;
cout<<"A - Personal Financial "<<endl;
cout<<"B - Personal Homeowner "<<endl;
cout<<"C - Personal Gold "<<endl;
cout<<"D - Small Business "<<endl;
cout<<"E - Big Business "<<endl;
cout<<"F - Gold Business "<<endl;
cout<<"What Type Of Account: ";
cin>>Type_Account;
switch(Type_Account)
{
case'A':
case'a':
cout<<"Your interest earned will be 1.5%"<<endl;
break;
case'B':
case'b':
cout<<"Your interest earned will be 2.0%"<<endl;
break;
case'C':
case'c':
cout<<"Your interest earned will be 2.5%"<<endl;
break;
case'D':
case'd':
cout<<"Your interest earned will be 3.0%"<<endl;
break;
case'E':
case'e':
cout<<"Your interest earned will be 3.5%"<<endl;
break;
case'F':
case'f':
cout<<"Your interest earned will be 4.0%"<<endl;
break;
default :
cout << "Invalid" << endl;
}
return 0;
}
Record your screen display.
QUESTIONS
1. What are the data needed for the solution of the program to produce the desired output?
The data needed for the solution of the program is the input which is the Mary Jane’s account
type and balance.
2. What statements in the program that determine interest earned of each type of account?
The statements in the program are:
a. totalPersonalFinancial= amount * 0.015;
b. totalPersonalHomeOwner= amount * 0.02;
c. totalPersonalgold= amount *0.025;
d. totalSmallBusiness= amount * 0.03;
e. totalBigBusiness= amount * 0.035;
f. totalgoldBusiness= amount * 0.04;
3. Omit break statements in your program. What did you observe?
I observe that at the end of each case, the program will not execute all consecutive case
statements until it finds the next break statement, meaning that without break statements, the
execution continues evaluating the following case statements.
4. Omit default statement in your program. What did you observe?
I observe that if there’s no default statement, no case match is found and none of the
statements in the switch body will get executed. Therefore, there should be at most one default
statement
5. What have you observed in using switch statement in this program compare to if-else
statement?
I observed that switch statements allows a value to change control of execution. The switch
statements select the execution of the statement on the basis of keyboard command that if-else
statement can’t do. In using switch statement, all the statements following a matching case execute
until a break statement is reached.
EXERCISE No.2
A student took four quizzes in a term and would like to compute their average. He also would like
to know what will be the remarks. Follow the range of grades and its equivalent remarks, and then fill-up
the table below:
100-95 - Excellent
94-90 – Very Satisfactory
89-85 - Satisfactory
84-80 - Fine
79-75 - Fair
74 and below – Poor
REQUIREMENTS:
1. Write the corresponding algorithm
a. Narrative
b. Pseudocode
2. Create the equivalent flowchart based on the algorithm of the given problem.
3. Construct the program and record your screen display result.
NARRATIVE ALGORITHM PSEUDOCODE
Step 1: Start 1. Start
To determine the average and Step 2: Input Q1, Q2, Q3, Q4, 2. Input a set of 4 quizzes
corresponding remarks of a respectively. scores.
student’s quiz scores.
Step 3: Find the average of the 3. Calculate their average
scores: Q1, Q2, Q3 and Q4 and by adding the test scores
divide to the number of items which and dividing it to the
is 4. number of items.
Step 4: If (grade < 100, but > 95), 4. If average is greater
then print “Excellent” else than 100, but less than 95,
then print "Excellent” else,
If (grade < 94, but > 90),
then print “Very • If average is above 94,
Satisfactory”, else but below 90, then print
"Very Satisfactory” else,
If (grade < 89, but > 85),
then print “Satisfactory” If average is above
else, 89, but below 85,
then print
If (grade < 84, but > 80), "Satisfactory” else,
then print “Fine” else,
If average is above
If (grade < 79, but > 75), 84, but below 80,
then print “Fair” else, then print "Fine”
else,
If (grade < 74), then print
“Poor”. If average is above
79, but below 75,
Step 5: Display the final average of then print "Fair”
the scores. else,
Step 6: End If average is below
74, then print
"Poor”.
5. Display the average score.
6. End
FLOWCHART
CODES FOR GRADE REMARKS
#include<iostream>
#include<stdlib.h>
using namespace std;
int main ()
{
double a,b,c,d, Average=0;
system ("cls");
cout<<"quiz 1: ";
cin>>a;
cout<<"quiz 2: ";
cin>>b;
cout<<"quiz 3: ";
cin>>c;
cout<<"quiz 4: ";
cin>>d;
a=a*1;
b=b*1;
c=c*1;
d=d*1;
Average=(a+b+c+d)/4;
cout<<"Average grade: "<<Average<<endl;
if(Average>=100&&95){
cout <<"Excellent";
}
else if(Average>=94&&90){
cout <<"very satisfactory";
}
else if(Average>=89&&85){
cout <<"satisfactory";
}
else if(Average>=84&&80){
cout <<"fine";
}
else if(Average>=79&&75){
cout <<"fair";
}
else if(Average>=74&&0){
cout <<"fair";
}
else{
cout << "no grade";
}
return 0;
}
Record your screen display.
QUESTIONS
1. What are the data needed for the solution of the program to produce the desired output?
The data needed for the solution of the program is the input which is the four scores of the
student’s quizzes.
2. What statements in the program that determines the average grade remarks?
The statements are:
a. if (ave)== 100 ll ave >=95) else;
b. if (ave)==94 ll ave>=90) else;
c. if (ave)== 89 ll ave>85) else;
d. if (ave)==84 ll ave>=80) else;
e. if (ave)==79 ll ave>=75) else;
f. if (ave)==74 ll ave>=0) else;
3. What was the condition applied to satisfy the requirement?
The condition applied is “if-else statement” to satisfy the requirement.
EXERCISE No. 3
Do the following conversion. Numbers are from 1 – 20. The program should display “Invalid Input”
Once the user entered an invalid input.
a. Word to Number
b. Roman Numeral to Word
c. Number to Word
REQUIREMENTS:
1. Write the corresponding algorithm
a. Narrative
b. Pseudocode
2. Create the equivalent flowchart based on the algorithm of the given problem.
3. Construct the program and record your screen display result.
NARRATIVE ALGORITHM PSEUDOCODE
The objective of the study is to Step 1: Start 1. Start
convert numbers and display an Step 2: Input Word to Number 2. Input Word to Number
invalid input.
Step 3: If conversion = one, then 3. If conversion = one, then print 1;
print 1; If conversion = two, then
If conversion = two, then print 2;
print 2; If conversion= three, then
If conversion= three, then print 3;
print 3; If conversion = four, then
If conversion = four, then print 4;
print 4; If conversion =five, then
If conversion =five, then print 5.
print 5. 4. Input Roman Numeral to Word
Step 4: Input Roman Numeral to 5. If conversion = I, then print one;
Word
If conversion = II, then print
Step 5: If conversion = I, then print two;
one;
If conversion = III, then
If conversion = II, then print print three;
two;
If conversion = IV, then
If conversion = III, then print four;
print three;
If conversion = V, then print
If conversion = IV, then five.
print four;
6. Input Number to Word
If conversion = V, then print
five. 7. If conversion = 1, then print one;
Step 6: Input Number to Word If conversion = 2, then print
two;
Step 7: If conversion = 1, then print
one; If conversion = 3, then print
three;
If conversion = 2, then print
two; If conversion = 4, then print
four;
If conversion = 3, then print
three; If conversion = 5, then print
five.
If conversion = 4, then print
four; 8. Display “You have entered an
invalid value. Please try again.”
If conversion = 5, then print
five. 9: End
Step 8: Display “You have entered
an invalid value. Please try again.”
Step 9: End
FLOWCHART
CODES FOR THE CONVERSIONS
#include <iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int menu_value;
string conversion_value;
cout << "Conversion: " << endl;
cout << "1 - Word to Number" << endl;
cout << "2 - Roman Numeral to Word" << endl;
cout << "3 - Number to Word" << endl << endl;
cout << "Choose your Conversion Type: ";
cin>> menu_value;
switch(menu_value)
{
case 1:
cout << "Enter value here: ";
cin>> conversion_value;
if(conversion_value == "one" || conversion_value == "One" || conversion_value ==
"ONE"){
cout << "one = 1";
}else if (conversion_value == "two" || conversion_value == "Two" || conversion_value ==
"TWO"){
cout << "two = 2";
}else if (conversion_value == "three" || conversion_value == "Three" || conversion_value
== "THREE"){
cout << "three = 3";
}else if (conversion_value == "four" || conversion_value == "Four" || conversion_value
== "FOUR"){
cout << "four = 4";
}else if (conversion_value == "five" || conversion_value == "Five" || conversion_value ==
"FIVE"){
cout << "five = 5";
}else if (conversion_value == "six" || conversion_value == "Six" || conversion_value ==
"SIX"){
cout << "six = 6";
}else if (conversion_value == "seven" || conversion_value == "Seven" ||
conversion_value == "SEVEN"){
cout << "seven = 7";
}else if (conversion_value == "eight" || conversion_value == "Eight" || conversion_value
== "EIGHT"){
cout << "eight = 8";
}else if (conversion_value == "nine" || conversion_value == "Nine" || conversion_value
== "NINE"){
cout << "nine = 9";
}else if (conversion_value == "ten" || conversion_value == "Ten" || conversion_value ==
"TEN"){
cout << "ten = 10";
}else if (conversion_value == "eleven" || conversion_value == "Eleven" ||
conversion_value == "ELEVEN"){
cout << "eleven = 11";
}else if (conversion_value == "twelve" || conversion_value == "Twelve" ||
conversion_value == "TWELVE"){
cout << "twelve = 12";
}else if (conversion_value == "thirteen" || conversion_value == "Thirteen" ||
conversion_value == "THIRTEEN"){
cout << "thirteen = 13";
}else if (conversion_value == "fourteen" || conversion_value == "Fourteen" ||
conversion_value == "FOURTEEN"){
cout << "fourteen = 14";
}else if (conversion_value == "fifteen" || conversion_value == "Fifteen" ||
conversion_value == "FIFTEEN"){
cout << "fifteen = 15";
}else if (conversion_value == "sixteen" || conversion_value == "Sixteen" ||
conversion_value == "SIXTEEN"){
cout << "sixteen = 16";
}else if (conversion_value == "seventeen" || conversion_value == "Seventeen" ||
conversion_value == "SEVENTEEN"){
cout << "seventeen = 17";
}else if (conversion_value == "eighteen" || conversion_value == "Eighteen" ||
conversion_value == "EIGHTEEN"){
cout << "eighteen = 18";
}else if (conversion_value == "nineteen" || conversion_value == "Nineteen" ||
conversion_value == "NINETEEN"){
cout << "nineteen = 19";
}else if (conversion_value == "twenty" || conversion_value == "Twenty" ||
conversion_value == "TWENTY"){
cout << "twenty = 20";
}else{
cout << "You have entered an Invalid Value. Please try again.";
}
break;
case 2:
cout << "Enter value here: ";
cin>> conversion_value;
if(conversion_value == "I" || conversion_value == "i"){
cout << "I = one";
}else if (conversion_value == "II" || conversion_value == "ii"){
cout << "II = two";
}else if (conversion_value == "III" || conversion_value == "iii"){
cout << "III = three";
}else if (conversion_value == "IV" || conversion_value == "iv"){
cout << "IV = four";
}else if (conversion_value == "V" || conversion_value == "v"){
cout << "V = five";
}else if (conversion_value == "VI" || conversion_value == "vi"){
cout << "VI = six";
}else if (conversion_value == "VII" || conversion_value == "vii"){
cout << "VII = seven";
}else if (conversion_value == "VIII" || conversion_value == "viii"){
cout << "VIII = eight";
}else if (conversion_value == "IX" || conversion_value == "ix"){
cout << "IX = nine";
}else if (conversion_value == "X" || conversion_value == "x"){
cout << "X = ten";
}else if (conversion_value == "XI" || conversion_value == "xi"){
cout << "XI = eleven";
}else if (conversion_value == "XII" || conversion_value == "xii"){
cout << "XII = twelve";
}else if (conversion_value == "XIII" || conversion_value == "xiii"){
cout << "XIII = thirteen";
}else if (conversion_value == "XIV" || conversion_value == "xiv"){
cout << "XIV = fourteen";
}else if (conversion_value == "XV" || conversion_value == "xv"){
cout << "xv = fifteen";
}else if (conversion_value == "XVI" || conversion_value == "xvi"){
cout << "XVI = sixteen";
}else if (conversion_value == "XVII" || conversion_value == "xvii"){
cout << "XVII = seventeen";
}else if (conversion_value == "XVIII" || conversion_value == "xvii"){
cout << "XVII = eighteen";
}else if (conversion_value == "XIX" || conversion_value == "xix"){
cout << "XIX = nineteen";
}else if (conversion_value == "XX" || conversion_value == "xx"){
cout << "XX = twenty";
}else{
cout << "You have entered an Invalid Value. Please try again.";
}
break;
case 3:
cout << "Enter value here: ";
cin>> conversion_value;
if(conversion_value == "1"){
cout << "1 = one";
}else if (conversion_value == "2"){
cout << "2 = two";
}else if (conversion_value == "3"){
cout << "3 = three";
}else{
cout << "You have entered an Invalid Value. Please try again.";
}
break;
default:
cout << "You have entered an Invalid Value. Please try again.";
}
return 0;
}
Record your screen display:
CONCLUSION:
I therefore conclude that conditional structures are really quite important aspect of
programming because without them, we can’t block the codes and test one condition by
another condition, and programs would be unable to react to any changes. By using and
applying selective structure in constructing a program, it allows one set of statements to
be executed if a condition is true and another set of actions to be executed if a condition
is false. It enables including more than one route in a program, any solutions require
multiple choices or decisions, and these choices result in various possible routes which
the program can take. These routes signify the outcome of making a choice. Without
selection it would not be possible to contain diverse routes in programs, and the solutions
we come up with would not be realistic. By applying appropriate conditional statement
like selection structures, we can evaluate a condition on a desired output and avoid
errors and invalid result.