Computer Practicals-File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 53

PROGRAMS

1.
Binary Search:

#include<conio.h>

#include<iostream.h>

int Binary_Search(int a[], int N, int data)

int first,last,mid;

int found=0;

first=0;

last=N-1;

while(first<=last && found==0)

mid=int((first+last)/2);

if(a[mid]==data)

found=1;

if(a[mid]<data)

first=mid+1;

if(a[mid]>data)

last=mid-1;

return(found);

int main()
{

clrscr();

int x[10],data, i;

cout<<"\n Enter 10 numbers in ascending order:";

for(i=0;i<10;i++)

cin>>x[i];

cout<<"\n Enter No. to be searched:";

cin>>data;

int res=Binary_Search(x,10,data);

if(res==1)

cout<<"\n found";

else

cout<<"\n not found";

getch();

return 0;

}
/*

Enter 10 numbers in ascending order:1

11

14

16

19

20

23

Enter No. to be searched:11

found

*/

2.
Linear Search

#include<conio.h>

#include<iostream.h>

int Linear_Search (int x[],int N,int data)

int i, found=0;

for(i=0;i<N;i++)

if(x[i]==data)

found=1;

return(found);

int main()

clrscr();

int x[10],data, i;

cout<<"\n Enter 10 numbers:";

for(i=0;i<10;i++)

cin>>x[i];

cout<<"\n Enter No. to be searched:";

cin>>data;
int res=Linear_Search(x,10,data);

if(res==1)

cout<<"\n found";

else

cout<<"\n not found";

getch();

return 0;

/* Enter 10 numbers:1

10

Enter No. to be searched:5

found */

3.
Bubble Sort

#include<conio.h>

#include<iostream.h>

#include<iomanip.h>

int main()

int x[10],i,j,temp;

//input phase

clrscr();

for(i=0;i<10;i++)

cout<<"\n Enter "<<i+1<<" value";

cin>>x[i];

// Processing

for(i=0;i<9;i++)

for(j=0;j<=(9-i);j++)

if(x[j]>x[j+1])

temp=x[j];

x[j]=x[j+1];

x[j+1]=temp;
}

// Output Phase

cout<<"\n Sorted Array";

for(i=0;i<10;i++)

cout<<setw(6)<<x[i];

getch();

return 0;

/*

Enter 1 value 3

Enter 2 value 45

Enter 3 value 67

Enter 4 value 88

Enter 5 value 12

Enter 6 value 90

Enter 7 value 34

Enter 8 value 76

Enter 9 value 29

Enter 10 value 12

Sorted Array 3 12 12 29 34 45 67 76 88 90
*/

4. Selection Sort

#include<conio.h>

#include<iostream.h>

#include<iomanip.h>

void main()

clrscr();

int x[10],i,j,temp,low,pos;

// input

cout<<"\n Enter 10 numbers to be sort:";

for(i=0;i<10;i++)

cin>>x[i];

// Processing

for(i=0;i<9;i++)

low=x[i];

pos=i;

for(j=i+1;j<10;j++)

if(low>x[j])

low=x[j];
pos=j;

temp=x[i];

x[i]=x[pos];

x[pos]=temp;

} // end of i for loop

// output phase

cout<<"\n Sorted Array:";

for(i=0;i<10;i++)

cout<<setw(4)<<x[i];

getch();

return ;

/*

Enter 10 numbers to be sort:23

74

88

92

34

54

60

71

22
29

Sorted Array: 22 23 29 34 54 60 71 74 88 92

*/

5. Insertion Sort

#include<conio.h>

#include<iostream.h>

#include<iomanip.h>

void main()

clrscr();

int x[10],i,j,temp;

// input

cout<<"Enter 10 numbers to be sort:";

for(i=0;i<10;i++)

cin>>x[i];

// Processing

for(i=1;i<10;i++)

temp=x[i];

j=i-1;

while(temp<x[j] && j>=0)

{
x[j+1]=x[j];

j=j-1;

x[j+1]=temp;

// output

cout<<"\n sorted Array:";

for(i=0;i<10;i++)

cout<<setw(4)<<x[i];

getch();

return;

/*

Enter 10 numbers to be sort:

23

65

87

66

12

16

62

94

74
90

sorted Array: 12 16 23 62 65 66 74 87 90 94

*/

6. Copy Constructor

#include<conio.h>

#include<iostream.h>

#include<string.h>

class student

int roll;

char name[30];

public:

student()

cout<<"\n Constructor:";

roll=10;

strcpy(name,"Rahul");

student(student &s)

cout<<"\n Copy constructor:";

roll=s.roll;
strcpy(name,s.name);

void input_void()

cout<<"\n Enter roll no:";

cin>>roll;

cout<<"\n Enter name:";

cin>>name;

void show_data()

cout<<"\n Roll no:";

cout<<roll;

cout<<"\n Name:";

cout<<name;

}};

int main()

student s;

s.show_data();

cout<<"\n";

student A(s);

A.show_data();

getch();

return 0;
}

/*

Constructor:

Roll no:10

Name:Rahul

Copy constructor:

Roll no:10

Name:Rahul */
7. Parametrised Constructor

#include<conio.h>

#include<iostream.h>

class read_constructor

int x;

public:

read_constructor(int a)

x=a;

void read_data()

cin>>x;

void show_data()

cout<<"Value of x:"<<x;

};

int main()

read_constructor obj(10);
obj.show_data();

getch();

return 0;

/*

Value of x:10

*/
8. Lower Matrix

#include<conio.h>

#include<iostream.h>

#include<iomanip.h>

int main()

clrscr();

int x[4][4]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

int i,j;

// output phase

cout<<"Lower matrix:";

for(i=0;i<4;i++)

cout<<endl;

for(j=0;j<4;j++)

if(i>=j)

cout<<setw(6)<<x[i][j];

else

cout<<setw(6)<<" ";

getch();

return 0;
}

/*

Lower matrix:

5 6

9 10 11

13 14 15 16

*/
9. Sum of Matrices

#include<conio.h>

#include<iostream.h>

int main()

clrscr();

int x[2][3]={10,9,8,8,3,4};

int i,j,sum;

// Processing

sum=0;

for(i=0;i<2;i++)

for(j=0;j<3;j++)

sum=sum +x[i][j];

// output

cout<<"\n Sum of elements of matrix :"<<sum;

cout<<"\n The matrix is:";

for(i=0;i<2;i++)

cout<<endl;

for(j=0;j<3;j++)
cout<<" \t"<<x[i][j];

getch();

return 0;

/*

Sum of elements of matrix :42

The matrix is:

10 9 8

8 3 4

*/
10. Nesting

#include<conio.h>

#include<iostream.h>

class area

int b, h;

double ar;

public:

void input_data()

cout<<"\n Enter base:";

cin>>b;

cout<<"\n Enter height:";

cin>>h;

void calculate()

input_data();

ar=0.5*b*h;

}
void output()

calculate();

cout<<"\n Area of triangle:"<<ar;

};

int main()

clrscr();

area A;

A.output();

getch();

return 0;

/*

Enter base:10

Enter height:5

Area of triangle:25

*/
11. Pointers

#include<conio.h>

#include<iostream.h>

int* read()

int x;

x=45;

return(&x);

int main()

clrscr();

int *res;

res=read();

cout<<"\n Value at res:"<<res;

getch();

return 0;

}
/*

Value at res:0x8fc7ffee

*/

12. Linear Queue

#include<conio.h>

#include<iostream.h>

#include<iomanip.h>

//function prototype

void add(int &front, int &rear, int x[], int N, int value);

void deletion(int &front, int &rear);

void display(int front, int rear, int x[]);

void main()

clrscr();

int x[100],front,rear,choice,value;

front=-1;

rear=-1;

do

cout<<"\n Queue Menu";

cout<<"\n 1. Addition";

cout<<"\n 2. Deletion";
cout<<"\n 3. Display";

cout<<"\n 4. Exit";

cout<<"\n Enter your choice";

cin>>choice;

switch(choice)

case 1:

cout<<"\n Enter value";

cin>>value;

add(front,rear,x,100,value);

break;

case 2:

deletion(front,rear);

break;

case 3:

display(front,rear,x);

getch();

break;

case 4:

break;

default:

cout<<" wrong choice";

getch();

}while(choice!=4);
getch();

return ;

void add(int &front, int &rear, int x[], int N, int value)

if(rear==-1)

front=0;

rear=0;

x[rear]=value;

else

if(rear>=(N-1))

cout<<"full";

else

rear=rear+1;

x[rear]=value;

void deletion (int &front, int &rear)


{

if(front==-1)

cout<<"Empty";

else

if(front==rear)

front=-1;

rear=-1;

else

front=front+1;

return;

void display (int front, int rear, int x[])

int i;

if(front==-1)

cout<<"Empty";

else

for(i=front;i<=rear;i++)

cout<<setw(4)<<x[i];
}

return;

/*

3. Display

4. Exit

Enter your choice 1

Enter value 8

Queue Menu

1. Addition

2. Deletion

3. Display

4. Exit

Enter your choice 3

25 8

Queue Menu

1. Addition

2. Deletion

3. Display

4. Exit

Enter your choice 2

Queue Menu
1. Addition

2. Deletion

3. Display

4. Exit

Enter your choice 3

5 8

Queue Menu

1. Addition

2. Deletion

3. Display

4. Exit

Enter your choice 1

Enter value 9

Queue Menu

1. Addition

2. Deletion

3. Display

4. Exit

Enter your choice 3

58 9

Queue Menu

1. Addition

2. Deletion
3. Display

4. Exit

Enter your choice 4

*/

13.Circular Queue

#include<conio.h>

#include<iostream.h>

#include<iomanip.h>

//Function prototype

void push(int x[], int N, int &M, int data);

void pop(int &m);

void display(int x[], int &m);

void main()

clrscr();

int x[100],m,data,choice;

m=0;

do

cout<<"\n STACK MENU";

cout<<"\n 1. Push";

cout<<"\n 2. Pop";

cout<<"\n 3. Display";
cout<<"\n 4. Exit";

cout<<"\n Enter choice:";

cin>>choice;

switch(choice)

case 1:

cout<<" Enter value:";

cin>>data;

push(x,100,m,data);

break;

case 2:

pop(m);

break;

case 3:

display(x,m);

getch();

break;

case 4:

break;

default:

cout<<"\n Wrong choice";

getch();

}while(choice!=4);

}
void push(int x[], int N, int &M, int data)

if(M>=N)

cout<<"STACK full";

else

x[M]=data;

M=M+1;

return;

void pop(int &m)

if(m<1)

cout<<"\n STACK Empty";

else

m=m-1;

return;

void display(int x[],int &m)

int i;
if(m<1)

cout<<"\n STACK Empty";

else

for(i=0;i<m;i++)

cout<<setw(6)<<x[i];

return;

/*

2. Pop

3. Display

4. Exit

Enter choice:1

Enter value:4

STACK MENU

1. Push

2. Pop

3. Display

4. Exit

Enter choice:1

Enter value:6

STACK MENU

1. Push
2. Pop

3. Display

4. Exit

Enter choice:1

Enter value:8

STACK MENU

1. Push

2. Pop

3. Display

4. Exit

Enter choice:3

2 4 6 8

STACK MENU

1. Push

2. Pop

3. Display

4. Exit

Enter choice:2

STACK MENU

1. Push

2. Pop

3. Display

4. Exit
Enter choice:3

2 4 6

STACK MENU

1. Push

2. Pop

3. Display

4. Exit

Enter choice:4

*/
14. Read Binary File

#include<fstream.h>

#include<conio.h>

#include<iostream.h>

#include<string.h>

struct student{

int roll;

char name[30];

char address[80];

};

int main()

clrscr();

ifstream obj("student.dat");

student s;

cout<<"\n Reading student.dat file";

while(obj.read((char*)&s, sizeof(student)))

cout<<"\nRoll no:"<<s.roll;
cout<<"\n Name:"<<s.name;

cout<<"\n Address:"<<s.address;

obj.close();

getch();

return 0;

/*

Reading student.dat file

Roll no:1

Name:ashish

Address:delhi

*/
15. Write in Binary File

#include<fstream.h>

#include<conio.h>

#include<iostream.h>

struct student

int roll;

char name[30];

char address[80];

};

int main()

clrscr();

ifstream fin("student.dat");

ofstream fout("temp.dat");

int troll;

student s;

cout<<"\n Enter roll no to modify";

cin>>troll;
while(fin.read((char*)&s, sizeof(student)))

if(troll==s.roll)

cout<<"\n Enter New name:";

cin>>s.name;

cout<<"\n Enter Address:";

cin>>s.address;

fout.write((char*)&s, sizeof(student));

fin.close();

fout.close();

//remove("student.dat");

//rename("temp.dat","student.dat");

getch();

return 0;

/*

Enter roll no to modify 1

Enter New name:AG


Enter Address:anand vihar

*/

SQL COMMANDS
Command 1:

Show databases;

Command 2:

Create database;
Command 3 & 4:

Create and desc table


Command 5 & 6:

Insert & select

Command 7:

Alter table- add


Command 8:

Alter table drop

Command 9:

Select-Like
Command 10:

Select- order by
Command 11:

Select-distinct
Command 12:

Grouping functions – max, min, avg

Command 13:

Select – grouping functions- sum, count


Command 14:

Select – group by

Command 15:
Update

Command 16:

Create view
Command 17:

Drop view
INDEX
● PROGRAMS:

□ Binary Search

□ Linear Search

□ Bubble Sorting

□ Selection Sorting

□ Insertion Sorting

□ Copy Constructor

□ Paramterised Constructor

□ Lower Triangular Matrix

□ Sum of two Matrices

□ To calculate area of a triangle using class

□ Pointer

□ Linear Queue

□ Circular Queue

□ To read content from a Binary File

□ To write content in the Binary File


● SQL COMMANDS:

□ Show database

□ Create database

□ To insert data and use of SELECT command

□ Creating and printing the table in descending order

□ Alter the table with ADD command

□ Alter the table with DROP command

□ LIKE command

□ SELECT-ORDER BY command

□ SELECT-DISTINCT command

□ Grouping function – MAX,MIN,AVG

□ SELECT-Grouping function- SUM,COUNT

□ SELECT-GROUP BY command

□ UPDATE command

□ CREATE VIEW command

□ DROP VIEW command

You might also like