You are on page 1of 10

Data Structures

Submitted to:

Ms. Anosh Fatima

Submitted by:

Ammara Sehar

Roll no: 1706

Shahida Riaz

Roll no: 1777

M.sc (Morning)

2nd Semester

DEPARTMENT OF COMOUTER SCIENCE GCWUF

1
Program No: 1
 Coding:
#include<iostream.h>
#include<conio.h>
class add
{
private:
int a, b, s;
public:
input()
{
cout<<"Enter first number?";
cin>>a;
cout<<"Enter second number?";
cin>>b;
}
print()
s=a+b;
cout<<"Sum="<<s;
}
};
void main(void)
{
add obj;
obj.input();
obj.print();
getch();
}
 Dry Run

A B
Step1 7 0
Step2 7 0

Step3 0 0

Step4 7 0
Step5 7 32

2
Program No: 2
 Coding:
#include<iostream.h>
#include<conio.h>
class exch
{
private:
int A, B, TEMP;
public:
input()
{
cout<<"Enter first number in A?";
cin>>A;
cout<<"Enter second number in B?";
cin>>B;
}
exchange()
{
TEMP=A;
A=B;
B=TEMP;
cout<<"\nValues after Exchanging\n\n";
cout<<"Values in A="<<A<<endl;
cout<<"Values in B="<<B<<endl;
}
};
void main(void)
{
exch obj;
obj.input();
obj.exchange();
getch();
}
 Dry Run

A B
Step1 25 78

Step2 25 0

Step3 0 78

Step4 - 25
Step5 78 25

3
Program No: 3
 Coding:
#include<iostream.h>
#include<conio.h>
class xy
{
private:
int x, y;
public:
input()
{
cout<<"Enter first number";
cin>>x;
cout<<"Enter second number";
cin>>y;
}
test()
{
if(x>y)
cout<<"First number is greater";
else
cout<<"Second number is greater";
}
};
void main(void)
{
xy obj;
obj.input();
obj.test();
getch();
}
 Dry Run

X y Test
9 12 -
9 0 -
0 12 -
0 0 12

4
Program No: 4
 Coding:

#include<iostream.h>
#include<conio.h>
class xyz
{
public:
display()
{
for(int i=1; i<=10; i++)
{
cout<<i<<endl;
}
}
};
void main(void)
{
xyz obj;
obj.display();
getch();
}
 Dry Run

X y z
12 0 0
0 5 0
0 0 8

5
Program No: 5
 Coding:
#include<iostream.h>
#include<conio.h>
class xyz
{
public:
display()
{
for(int i=1; i<=10; i++)
{
cout<<i<<endl;
}
}
};
void main(void)
{
xyz obj;
obj.display();
getch();
}
 Dry Run

I=1

I=2

I=3

I=4

I=5

I=6

I=7

I=8

I=9

I=10 i<=10

6
Program No: 6
 Coding:

#include<iostream.h>
#include<conio.h>
class xyz
{
public:
print()
{
int n=1;
while(n<=10)
{
cout<<n<<endl;
n=n+1;
}
}
};
void main(void)
{
xyz obj;
obj.print();
getch();
}
 Dry Run

While(n<=10)

n=1

n=n+1

2=1+1

3=2+1

4=3+1

5=4+1

6=5+1

7=6+1

7
8=7+1

9=8+1

10=9+1

N<=10

Program No: 7
 Coding:
#include<iostream.h>
#include<conio.h>
class xyz
{
public:
float mean(float x, float y, float z)
{
return (x+y+z)/3;
}
};
void main(void)
{
xyz obj;
floata, b, c;
cout<<"Enter first number";cin>>a;
cout<<"Enter second number";cin>>b;
cout<<"Enter thitd number";cin>>c;
cout<<"Mean of three numbers:"<<obj.mean(a, b, c);
getch();
}

 Dry Run

mean x y z

3 6 9
=x+y+z/3
=3+9+0/3
=18/3=6

8
Program No: 8
 Coding:
#include<iostream.h>
#include<conio.h>
class xyz
{
public:
exchange(int &x, int &y)
{
int t;
t = x;
x = y;
y = t;
}
};
void main(void)
{
xyz obj;
int a, b;
cout<<"Enter first number in a ?";
cin>>a;
cout<<"Enter second number in b ?";
cin>>b;
obj.exchange(a, b);
cout<<"\nValues after Exchanging\n\n";
cout<<"Values in a ="<<a<<endl;
cout<<"Values in b="<<a<<endl;
getch();
}

 Dry Run
Exchange t x y

- 25 10

25 0 0

0 10 0

0 0 25

9
10

You might also like