Computing For Engineers: Lab Activity 2: Switch Case Statement and Function Objectives
Computing For Engineers: Lab Activity 2: Switch Case Statement and Function Objectives
To be familiar with the control structures type: switch case statement and function.
Learning Outcomes: At the end of the session, the students are able:
Apparatus:
Sample Code1:
The program code shown below uses to calculate for summation
#include <iostream>
#include <string>
Sample Code 2:
The program code for switch case statement.
#include <iostream>
#include <string>
using namespace std;
int main(){
int number;
int number2;
here:
cout<<"please enter your value ";
cin>> number;
if (number >0 && number<5){
number2 = 1;
}
else if (number>4 && number<11){
number2 = 2;
}
else{
number2 = 3;
}
switch(number2){
case 1:
cout<<"abc";
//break;
case 2:
cout<<"cde";
//break;
default:
cout<<"wrong value";
}
cout<<endl;
goto here;
system("PAUSE");
}
Sample Code 3:
The program code for controlling the size of pattern by using function.
#include <iostream>
#include <string>
//clear screen
}
system("pause");
}
void pattern1(){
int i;
for(i=1; i<6; i++){
for(int j=1; j<6; j++){
cout <<"* ";
}
cout<<endl;
}
}
void pattern2(int k, int l){
int i;
for(i=1; i<k; i++){
for(int j=1; j<l; j++){
cout <<"* ";
}
cout<<endl;
}
}
void compare(int x, int y){
if (x<=y){
int k,l;
cout<<"plese enter your size (k n l)>> ";
cin>>k >>l;
pattern2(k,l);
}
else{
pattern1();
}
}
Question 1
Design a complete C++ program to solve the following equation.
100
n5
n4
n 1
If the value of summation between 10.10 to 40.78 , calculate and display p = 5summation and if the
value of summation are 41 or 160, calculate q summation 10 . When the value of summation
is greater or equal than 1000, user will be notified either to continue or end the process. Press
<c> to continue or <e> to end the process. Use switch case statement to calculate and
display value of p and q.
Question 2
Design a complete C++ program based on the following statement.
i- User must enter character for x.
ii- If user enter vowel letter, please display the below pattern.
* 1 * 2 *
3 * 4 *
* 5 *
6 *
*
Pattern A
iii- If user enter odd number, please ask the user to enter the size of j and k and display
pattern B.
a * b * c
* d * e
size of j
f * g
* h
i
size of k
Pattern B
Use void function in your program.