Professional Documents
Culture Documents
LAB CONTENTS
Compilation process of C++
Displaying message using cout
Declaring and initializing variables with different data types
Declaring constant identifiers and use in the program
Assigning values to variables
Accepting values from users through cin
Practicing operators and expressions
Practicing control structures (sequential, if, if...else, nested if... else, switch, for loop, while loop, do while loop and
Nested loops)
Arrays and functions
Files
#include<iostream.h>
#include<conio.h>
int age;
char name[30];
char sex;
float height;
void main()
{ clrscr();
cout<<"Enter your name,age,sex and
height"<<endl;
cin>>name>>age>>sex>>height;
cout<<endl;
cout<<"Your name is "<<name<<endl;
cout<<"Your age is "<<age<<endl;
cout<<"Your sex is "<<sex<<endl;
cout<<"Your height is "<<height<<" feet"<<endl;
cout<<"THANK YOU";
getch();
}
#include<iostream.h>
#include<conio.h>
int o,f,s,i,j;
void main()
{ clrscr();
do
{ cout<<endl;
cout<<"CONTROL STRUCTURES\n\n";
cout<<"1.Sequential\n";
cout<<"2.If\n";
cout<<"3.If else\n";
cout<<"4.Nested If else\n";
cout<<"5.Switch\n";
cout<<"6.For loop\n";
cout<<"7.While loop\n";
cout<<"8.Do while loop\n";
cout<<"9.Nested loops\n";
cout<<"10.Exit\n";
cout<<"Enter your option: ";
cin>>o;
switch(o)
{ case 1: cout<<"Example for sequential execution"<<endl;
cout<<"One\n";
cout<<"Two\n";
cout<<"Three\n";
break;
case 2: cout<<"Example for if statement"<<endl;
cout<<"Enter first number ";
cin>>f;
cout<<"Enter second number ";
cin>>s;
if(f>s) cout<<f<<" is big\n";
if(s>f) cout<<s<<" is big\n";
break;
case 3: cout<<"Example for if else statement"<<endl;
cout<<"Enter first number ";
cin>>f;
cout<<"Enter second number ";
cin>>s;
if(f>s) cout<<f<<" is big\n";
else cout<<s<<" is big\n";
break;
case 4: cout<<"Example for nested if else\n";
cout<<"Enter first number:";
cin>>f;
cout<<"Enter second number:";
4
cin>>s;
if(f>s) cout<<f<<" is big";
else if(s>f) cout<<s<<" is big";
else cout<<"Both are same";
break;
case 5: cout<<"Example for switch\n";
cout<<"This program has switch statement within
do loop\n";
cout<<"So, this program itself serves as an
example for switch\n";
break;
case 6: cout<<"Exanple for for loop\n";
for(i=1;i<=10;++i)
cout<<i<<" ";
cout<<endl;
for(i=10;i>=1;--i)
cout<<i<<" ";
break;
case 7: cout<<"Exanple for while loop\n";
i=1;
while(i<=10)
{cout<<i<<" ";++i;}
cout<<endl;
i=10;
while(i>=1)
{cout<<i<<" ";--i;}
break;
case 8: cout<<"Example for do loop\n";
cout<<"This program has switch statement within
do loop\n";
cout<<"So, this program itself serves as an
example for do loop\n";
break;
case 9: cout<<"Example for nested loops\n";
for(i=1;i<=5;++i)
{ cout<<"i="<<i<<" ";
for(j=1;j<=i;++j)
cout<<"j="<<j<<" ";
cout<<endl;
}
break;
case 10: return;
default: cout<<"Please choose between 1 and 10 only";
}
5
}while(1);
}
Arrays and functions
//Matrix Addition
#include<iostream.h>
#include<conio.h>
int a[10][10],b[10][10],aplusb[10][10],r1,r2,r3,c1,c2,c3,i,j;
void readmatrix(int &r,int &c,int a[][10])
{ cout<<"Enter matrix dimension\n";
cin>>r>>c;
cout<<"Enter matrix elements\n";
for(i=1;i<=r;++i)
for(j=1;j<=c;++j)
cin>>a[i][j];
}
void addmatrices()
{
if((r1!=r2)||(c1!=c2))
{cout<<"Matrix addition not possible";getch();return;}
for(i=1;i<=r1;++i)
for(j=1;j<=c1;++j)
aplusb[i][j]=a[i][j]+b[i][j];
r3=r1;c3=c1;
}
void printmatrix(int r,int c,int aplusb[][10])
{ for(i=1;i<=r;++i)
{for(j=1;j<=c;++j)
cout<<aplusb[i][j]<<" ";
cout<<"\n";
}
}
void main()
{ clrscr();
readmatrix(r1,c1,a);
readmatrix(r2,c2,b);
addmatrices();
cout<<"The resultant matrix is \n";
printmatrix(r3,c3,aplusb);
getch();
}
Exercises
6