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

Computing For Engineers: Lab Activity 2: Switch Case Statement and Function Objectives

This document discusses lab activity objectives for computing for engineers regarding switch case statements and functions in C++. It provides sample code demonstrating a switch case statement calculating values based on user input, a for loop calculating a summation, and functions controlling the size of patterns output. It also provides two questions, one calculating a summation and displaying values based on conditions, and another asking the user for input and outputting different patterns using functions based on that input. The objectives are for students to understand control structures like switch cases and functions in C++ programming.

Uploaded by

yapshenghao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Computing For Engineers: Lab Activity 2: Switch Case Statement and Function Objectives

This document discusses lab activity objectives for computing for engineers regarding switch case statements and functions in C++. It provides sample code demonstrating a switch case statement calculating values based on user input, a for loop calculating a summation, and functions controlling the size of patterns output. It also provides two questions, one calculating a summation and displaying values based on conditions, and another asking the user for input and outputting different patterns using functions based on that input. The objectives are for students to understand control structures like switch cases and functions in C++ programming.

Uploaded by

yapshenghao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

To understand and use the control structures in programming.


To understand the concept of combination between switch case statement with the other
structures of programming.
To understand the concept of function.

Apparatus:

PC with C++ compiler

Sample Code1:
The program code shown below uses to calculate for summation
#include <iostream>
#include <string>

//library for io stream


//library for string

using namespace std;


int main(){
int n,equ,sum=0;
cout<<"n\t"<<"10(n+4)"<<"\t\tsum\n";
for(n=1; n<11; n++){
equ = 10*(n+4);
sum = sum + equ;
cout<< n <<"\t" << equ <<"\t\t"<< sum<<endl;
}
system("pause");
}

Computing For Engineers


Switch Case Statement and Function

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");
}

Computing For Engineers


Switch Case Statement and Function

Sample Code 3:
The program code for controlling the size of pattern by using function.
#include <iostream>
#include <string>

//library for io stream


//library for string

using namespace std;


void pattern1();
//function prototype
void pattern2(int k, int l);
void compare(int x, int y);
int main(){
while(1){
int x,y;
cout<<"please enter value of x and y>> ";
cin >> x >>y;
compare(x,y);
system("pause");
system("cls");

//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();
}
}

Computing For Engineers


Switch Case Statement and Function

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.

Computing For Engineers


Switch Case Statement and Function

You might also like