Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Fundamentals of Programming

Lab Report 03

Introduction to Switch Structures , Conditional


Operator And Manipulators
SUMMER SEMESTER

Submitted to: Engr. Ali Hassan


Session: ME-14 Section:B
SUBMITTED BY
Name CMS Objective Theory Lab Lab
(1) Work(3) Total
(3) Task (3)
Muhammad Ali 406356
Arshad

School of Mechanical and Manufacturing Engineering


OBJECTIVES
The objectives of today’s lab work include

➢ Introduction and use of switch structures.


➢ Introduction and use of conditional operators.
➢ Use of goto statement for repeated execution of a function or sequence.
➢ Introduction to increment and decrement operators.
➢ Introduction to format flags and manipulators.

THEORY

1. Switch Structures
Switch structures is a type of selection control mechanism used to which can be used to control the
direction of flow of a program similar to the conditional statements (if, if….else etc.).It can be
considered an alternate to if else statement. Value of the expression in the switch also known as
the selector determines the action taken and executed by the program. Multiple statements can
be evaluated using a number of cases in the switch structure.

2. Conditional Operators
Conditional operator also known as ternary operator evaluates the given condition and executes a
block of code based on the result of the condition. If the condition is true than the a certain block
of code is executed , however if it is false another block of code maybe executed as written in the
operator.

3. Goto Statement
The goto statement is used to run the program again from a specific area. The goto is used to take
the program again back to a specific function after the successful execution of the program.The
goto statement can be used to repeatedly execute a program as long as the user wishes .

4. Increment and Decrement Operators


Increment and decrement operators are used to increase and decrease the value of an operand by
1 respectively. Both have two types that is pre and post increment and pre and post decrement
operators . Pre increment operator means the value of the operator is incremented first and then
used in the expression. The post-increment operator means the operand is first used in the
expression and then performs the increment operation to the original value by 1. Same is the case
for pre and post decrement operators.

5. Format Flags and Manipulators


Format flags control the formatting of an output .This means that they can be used to display a
value in particular format . Manipulators are used during coding for the purposes of manipulating
or formulating the output. They do not change the value of output or variable; they just modify the
output. Examples of these manipulators include setw and setfill etc. These two are basically the
functions of a header file iomanip. ‘setw’ is used to create spaces in the output whereas ‘setfill’ fills
the spaces with a certain assigned character in the output.

LAB WORK

Program #1 :
We made a basic program to learn use and application of switch .

Code:
#include<iostream>
using namespace std ;
int main ()
{
int x,y;
cout<<"PLease enter the value of x: ";
cin>>x;
cout<<"PLease enter the value of y: ";
cin>>y;
switch (x+y)
{
case 0:
cout<<"You will die in 2030 ";
cout<<"\n World "
break;

case 1:
cout<<"Saaf chali shafaaf chali";
break;
case 2:
cout<<"I want laptop";
break;

case 3:
cout<<"I want to get A in summers";
break;

case 4:
cout<<"But I wont get A";
break;

default:
cout<<"Your answer exceeded the case limit";
break;
}
return 0;
}

Output:
1) Case 1

2) Case 2

3) Case 3
Program #2 :
We had to make a program which would take entries from the users for his grades in various
subjects in the semester and then would calculate his GPA using switch.

Code:
#include<iostream>
using namespace std ;
int main ()
{
char sub1,sub2,sub3,sub4;
int resl1,resl2,resl3,resl4;
cout<<"Please enter grade of first subject: ";
cin>> sub1;
cout<<"Please enter grade of second subject: ";
cin>> sub2;
cout<<"Please enter grade of third subject: ";
cin>> sub3;
cout<<"Please enter grade of fourth subject: ";
cin>> sub4;

switch (sub1)
{
case 'A':
resl1=4;
break;

case 'B':
resl1=3;
break;

case 'C':
resl1=2;
break;

case 'D':
resl1=1;
break;

default:
resl1=0;
break ;

}
switch (sub2)
{
case 'A':
resl2=4;
break;

case 'B':
resl2=3;
break;

case 'C':
resl2=2;
break;

case 'D':
resl2=1;
break;

default:
resl2=0;
break ;

}
switch (sub3)
{
case 'A':
resl3=4;
break;

case 'B':
resl3=3;
break;

case 'C':
resl3=2;
break;

case 'D':
resl3=1;
break;

default:
resl3=0;
break;
}
switch (sub4)
{
case 'A':
resl4=4;
break;

case 'B':
resl4=3;
break;

case 'C':
resl4=2;
break;

case 'D':
resl4=1;
break;

default:
resl4=0;
break ;

}
float gpa;
gpa =(resl1+resl2+resl3+resl4)/4.0;

cout<<"Your GPA is "<<gpa;


return 0;
}

Output:

Program#3
We had to make a program which would take an entry from the user for his/her age and then
would tell the user whether or not he is eligible for the job and what conditions are applicable for
his eligibility for different ages using switch .

Code:
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"Please enter your age: ";
cin>>age;
switch (age)
{
case 22:
cout<<"You are eligible but you have to appear for job test and interview ";
break ;
case 23:
cout<<"You are eligible but you have to appear for interview";
break ;
case 24:
cout<<"You are eligible if you have 2 years of experience";
break;
case 25:
cout<<"You are eligible if you have 3 years of experience";
break;
default:
cout<<"You are not ineligible ";
break;
}
return 0;
}

Output:
1) For age 22

2) For age 25

Program#4
We had to make a program which would take an entry from a user in the form of an alphabet and
then would tell the user whether or not the alphabet is a vowel using switch.

Code:
#include <iostream>
using namespace std;
int main()
{
char l;
cout<<"Please enter your alphabet: ";
cin>>l;
switch (l)
{
case 'a':
cout<<"The alphabet is a vowel";
break;

case 'e':
cout<<"The alphabet is a vowel";
break;

case 'i':
cout<<"The alphabet is a vowel";
break;

case 'o':
cout<<"The alphabet is a vowel";
break;

case 'u':
cout<<"The alphabet is a vowel";
break;

default:
cout<<"The alphabet is a consonant";

return 0;
}

Output:
1) For vowels

2) For consonants
Program#5
We made a program which would use conditional operator to tell user whether or not the number
entered by user is even or odd.

Code:
#include<iostream>
using namespace std ;
int main ()
{
int a,b;
cout<<"Enter an integer ";
cin>>a;
b=a%2;
cout<<((b == 0)?"The given number is even":"The given number is odd");
return 0;
}

Output:
1) For odd integers

2) For even integers


Program#6
We had to make a program which would compare two integers given by the user with each other
using a conditional operator to tell us which of the two was greater and would allow to use the
program repeatedly as per the choice of the user using goto function.

Code:
#include<iostream>
using namespace std;
int main ()
{
//goto function
akbar:
int a,b;
cout<<"Please enter the first number: ";
cin>>a;
cout<<"Please enter the second number: ";
cin>>b;

int max ;
max=((a<b)?b:a);
cout<<max<<" is maximum";
char op;
cout<<"\nDo you wnat to check another pair?(y/n)";
cin>>op;
if (op=='y')
{
goto akbar;
}
{
cout<<"\nThank you";
}

return 0;
}

Output:

Program#7
We made a basic program using a manipulator for formatting the output.

Code:
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
//manipulators
cout<<setw(15)<<"Hello\tWord";
}

Output:
LAB TASKS

Task#1:
We had to make a program that displayed a menu to a user asking the choice of his/her
operation/conversion which he/she wants to perform and it switched to respective operations
using switch statement.

Code:
#include<iostream>
using namespace std;
int main()
{
ali :
int a;
char u;
float b,c,e;
double d = 1.6093440;
double f = 0.000621371;
cout<<"\nPlease select one of the following options\n ";
cout<<"1) In order to convert miles to kilometers press 1\n";
cout<<"2) In order to convert meters to miles press 2\n";
cout<<"3) In order to convert pounds to kilograms press 3\n";
cout<<"4) In order to convert newtons to pounds press 4\n";
cout<<"5) In order to convert degrees Farhenheit (TF) to degrees Rankin (TR) press 5\n";
cout<<"6) In order to convert degrees Celsius (TC) to degrees Rankin (TR) press 6\n";
cout<<"7) In order to convert degress Kelvin (TK) to degrees Farhenheit (TF) press 7\n";
cin>> a;
switch (a)
{
case 1 :
cout<<"Please enter miles to be converted : ";
cin>>b;
c = b * d;
cout<<b<<" miles are equal to "<<c<<" kilometers. ";
break;
case 2 :
cout<<"Please enter meters to be converted : ";
cin>>b;
c = b/1609.34 ;
cout<<b<<" meters are equal to "<<c<<" miles. ";
break;
case 3 :
cout<<"Please enter pounds to be converted : ";
cin>>b;
c = b/2.205 ;
cout<<b<<" pounds are equal to "<<c<<" kilograms. ";
break;
case 4 :
cout<<"Please enter newtons to be converted : ";
cin>>b;
c = b/4.448 ;
cout<<b<<" newtons are equal to "<<c<<" pounds. ";
break;
case 5 :
cout<<"Please enter degrees Farhenheit (TF) to be converted : ";
cin>>b;
c = b + 459.67;
cout<<b<<" in degrees Farhenheit (TF) is equal to "<<c<<" in degrees Rankin (TR).";
break;
case 6 :
cout<<"Please enter degress Celsius (TC) to be converted : ";
cin>>b;
e = (b*1.8)+32;
c = b + 459.67;
cout<<b<<" in degrees Celsius (TF) is equal to "<<c<<" in degrees Rankin (TR).";
break;
case 7 :
cout<<"Please enter degrees Kelvin (TK) to be converted : ";
cin>>b;
e= b-273.15;
c=(e*1.8)+32;
cout<<b<<" in degrees degrees Kelvin (TK) is equal to "<<c<<" in degrees Farhenheit (TF).";
break;
default :
cout<<"Invalid entry.";
break;
}
cout<<"\nDo you want to use the program again(y/n)\n ";
cin>>u;
if (u == 'y')
{
goto ali;
}
else
{
cout<<"\nThank you for using this program.";
}
return 0;
}

Output:

Task#3:
We had to make a program that computed and calculated the molecular weights of glycine ,
glutamic acid and glutamine and would take entries from the user for the number of atoms for the
five elements of an amino acid and then would compute and print the molecular weight of the
amino acid . We had to use ‘goto’ function atleast once in the program.

Code:
#include<iostream>
#include<string>
using namespace std ;
int main ()
{
char q,r;
int u,v,w,x,z;
float o= 15.9994,c=12.011,n=14.00674,s=32.066,h=1.00794;
float f,g,k,i;
g=(2*o)+(2*c)+(1*n)+(5*h);
k=(3*o)+(5*c)+(2*n)+(10*h);
g=(4*o)+(5*c)+(1*n)+(8*h);
cout<<"Molecular weight of glycine is "<<g<<endl;
cout<<"Molecular weight of glutamic acid is "<<k<<endl;
cout<<"Molecular weight of glutamine is "<<i<<endl;
cout<<"Do you want to use this program to calculate molecular weight of an amino acid using
number of atoms in amino acid (y/n) ?";
cin>>q;
if (q=='y')
{
goto ali;
}
else
{
cout<<"Program end";
}
ali:
string name;
cout<<"Please enter the name of your amino acid: ";
cin>> name;
cout<<"\nPlease enter the number of oxygen atoms in the amino acid : ";
cin>>u;
cout<<"\nPlease enter the number of carbon atoms in the amino acid : ";
cin>>v;
cout<<"\nPlease enter the number of nitrogen atoms in the amino acid : ";
cin>>w;
cout<<"\nPlease enter the number of sulfur atoms in the amino acid : ";
cin>>x;
cout<<"\nPlease enter the number of hydrogen atoms in the amino acid : ";
cin>>z;
f=(u*o)+(v*c)+(w*n)+(x*s)+(z*h);
cout<<"\n The molecular weight of "<<name<<" is: "<<f;
cout<<"Do you want to use this program again ? (y/n)";
cin>>r;
if (r == 'y')
{
goto ali;
}
else
{
cout<<"\n Thank you for using this program .";
}

return 0;
}

Output

Task#3:
We had to make a program that would take an entry from user a positive number and then would
compute its logarithm to either base 2 or base 8 as per the choice of the user making use of
conditional operator.

Code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b;
float c,j,k,d,e;
cout<<"Please enter your positive number ";
cin>>a;
c = log (8);
j= log (2);
k = log (a);
d= (k/j);
e =(k/c);
cout<<"To which base do you want to compute logarithm of your number \n ";
cout<<"For base 2 , press 2\n For base 8 , press 8\n ";
cin>>b;
cout<<((b==2)?d:e);

Output
1) For base 2

2 ) For base 8

You might also like