Cs Samy

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

COMPUTER SCIENCE

CLASS-XII

S.NO NAME OF THE PROGRAMME SIGNATURE

1
MULTIPLICATION OF 2-D
ARRAY
2
LINEAR SEARCH
3
Binary search
4
PUSH IN STACK.
5
POP FROM STACK
6
INSERTION
7
DELETION
8
SELECTION SORT
9
BUBBLE SORT
10
INSERTION SORT
11
INSERTION USING
LINKED
LIST
12
STATIC FUNCTION+
13
CONSTRUCTOR AND
DESTRUCTOR
14
COPY CONSTRUCTOR
15
INHERITANCE
16
STRUCTURES
17
FRIEND FUNCTION
18
STATIC FUNCTION
19
DATA FILE HANDALING
20
EQUALITY OF MATRICES
21
ENTERED CHARACTER..

1.MULTIPLICATION OF 2-D ARRAY


#include<iostream.h>

#include<conio.h>
void main()

clrscr();

int a[2][3],b[3][2],c[3][3],k,i,j;

cout<<" MATRIX 1 "<<"\n";

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

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

cout<<"Enter The Elements : ";

cin>>a[i][j];

cout<<"\n";

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

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

cout<<a[i][j]<<"\t";

cout<<"\n";

cout<<"\n\n";

cout<<" MATRIX 2 "<<"\n";

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

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

cout<<"enter the elements : ";

cin>>b[i][j];
}

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

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

cout<<b[i][j]<<"\t";

cout<<"\n";

cout<<"\n";

cout<<"Product of 2 matrices";

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

cout<<"\n";
for(j=0;j<2;j++)

c[i][j]=0;

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

c[i][j]=c[i][j]+a[i][k]*b[k][i];

cout<<c[i][j]<<" ";

cout<<"matrix are not compatible for multiplication";

getch();

OUTPUT
2. LINEAR SEARCH
#include<iostream.h>
#include<conio.h>

void main()

clrscr();

int A[10],size,i,flag=0,num,pos;

cout<<"\n enter the size of array";

cin>>size;

cout<<"\n enter the elements of array(in ascending order):";

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

cin>>A[i];

cout<<" the element to be searchjed for:";

cin>>num;

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

if(A[i]==num)

{flag=1;
pos=i;

break;

if(flag==0)

cout<<"\n element not found";

else

cout<<"\n element found at position"<<" "<<(pos+1);

getch();

OUTPUT
3. Binary search
#include<iostream.h>

#include<conio.h>
int bsearch(int[],int,int);

void main()

clrscr();

int a[10],i,j,size,index;

cout<<"enter size of element:";

cin>>size;

cout<<"enter element of array:";

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

cin>>a[i];

cout<<"enter element to be searched:";

cin>>j;

index=bsearch(a,size,j);
if(index==-1)

cout<<"sorry element is not found:";

else

cout<<"element found at index:"<<index<<",position:"<<index+1<<endl;

getch();

int bsearch(int a[],int n,int y)

int b,l,m;

b=0;

l=n-1;

while(b<=l)

m=(b+l)/2;

if(y==a[m])
return m;

else if(y>a[m]) b=m+1;

else l=m-1;

return -1;

OUTPUT
4. PUSH IN STACK.
#include<iostream.h>

#include<conio.h>

#include<process.h>

int push(int[],int&,int);

void display(int[],int);

const int size=50;

void main()
{

clrscr();

int stack[size],top=-1,res,item;

char ch='y';

while(ch=='y'||ch=='Y')

cout<<"enter element to insert:";

cin>>item;

res=push(stack,top,item);

if(res==-1)

cout<<"overflow"<<endl;

exit(1);

cout<<"stack is now:";
display(stack,top);

cout<<"want to insert more:";

cin>>ch;

getch();

int push(int stack[],int& top,int ele)

if(top==size-1) return -1;

else

top++;

stack[top]=ele;

return 0;
}

void display(int stack[],int top)

cout<<stack[top]<<"<--"<<endl;

for(int i=top-1;i>=0;i--)

cout<<stack[i]<<endl;

OUTPUT
5. POP FROM STACK
#include<iostream.h>

#include<conio.h>

#include<process.h>

#include<stdlib.h>

int push(int[],int &,int);

int pop(int[],int&);

void display(int[],int);

const int size=50;

void main()

clrscr();

int stack[size],item,res,top=-1;

char ch='y';

while(ch=='y'||ch=='Y')

{
cout<<"enter element to insert:"<<endl;

cin>>item;

res=push(stack,top,item);

if(res==-1)

cout<<"overflowed";

exit(1);

cout<<"stack is now:"<<endl;

display(stack,top);

cout<<"want to insert more:";

cin>>ch;

cout<<"now deleition of element is begain";

ch='Y';
while(ch=='y'||ch=='Y')

res=pop(stack,top);

if(res==size-1)

cout<<"underflow";

exit(1);

else

cout<<"element deleted is:"<<res<<endl;

cout<<"the stack is now"<<endl;

display(stack,top);

cout<<"want to delete more";


cin>>ch;

getch();

int push(int stack[],int& top,int ele)

if(top==size-1) return -1;

else

top++;

stack[top]=ele;

return 0;

int pop(int stack[],int & top)


{

int ret;

if(top==size-1) return -1;

else

ret=stack[top];

top--;

return ret;

void display(int stack[],int top)

cout<<stack[top]<<"<--"<<endl;

for(int i=top-1;i>=0;i--)

cout<<stack[i]<<endl;
}

OUTPUT
6. INSERTION
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int i,a[5],n,pos;

cout<<"enter the elements";

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

cin>>a[i];
}

cout<<"\n enter position to insert a new number";

cin>>pos;

if(pos>5)

cout<<"over flow";

else

cout<<"enter new number";

cin>>n;

--pos;

for(i=5; i>=pos; i--)

a[i+1]=a[i];
}

a[pos]=n;

cout<<"new array";

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

cout<<a[i];

getch();

output
7. DELETION

#include<iostream.h>

#include<conio.h>

void main()

{
clrscr();

int i,a[5],n,pos;

cout<<"eter the elements";

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

cin>>a[i];

cout<<"\n enter position to delete a number";

cin>>pos;

if(pos>5)

cout<<"over flow";

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

{
a[i]=a[i+1];

cout<<"new array";

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

cout<<a[i];

getch();

OUTPUT
8. SELECTION SORT

#include<iostream.h>
#include<conio.h>

void selsort(int[],int);

void main()

clrscr();

int a[10],size,item,index;

cout<<"entre size of array:";

cin>>size;

cout<<"enter element:";

for(int i=0;i<size;i++)

cin>>a[i];

selsort(a,size);

cout<<"\nThe shorted array is as shown below..\n";

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

cout<<a[i]<<" ";
cout<<endl;

getch();

void selsort(int a[],int n)

int small,pos,temp;

for(int i=0;i<n-1;i++)

small=a[i];

pos=i;

for(int j=i+1;j<n;j++)

if(a[j]<small)

small=a[j];
pos=j;

temp=a[i];

a[i]=a[pos];

a[pos]=temp;

cout<<"\nArray after pass"<<i+1<<" is";

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

cout<<a[j]<<" ";

OUTPUT
9. BUBBLE SORT
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

void bub_sort(int[],int);

int ar[10];

int size;
cout<<"Enter the size of an array \n";

cin>>size;

cout<<"Enter the lements of an array \n";

for(int i=0;i<size;i++)

cin>>ar[i];

cout<<"The array is \n";

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

cout<<ar[i]<<" ";

bub_sort(ar,size);

cout<<"\nThe sorted array is \n";

for(i=0;i<size;i++)
{

cout<<ar[i]<<" ";

getch();

void bub_sort(int ar[],int sz)

int temp;

for(int i=0;i<sz;i++)

for(int j=0;j<sz-1;j++)

if(ar[j]>ar[j+1])
{

temp=ar[j];

ar[j]=ar[j+1];

ar[j+1]=temp;

OUTPUT
10. INSERTION SORT
#include<iostream.h>

#include<conio.h>

#include<limits.h>

void insort(int[],int);

void main()

clrscr();

int a[10],size,item,index;

cout<<"entre size of array:";

cin>>size;

cout<<"enter element:";

for(int i=1;i<=size;i++)

cin>>a[i];
insort(a,size);

cout<<"The shorted array is as shown below..\n";

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

cout<<a[i]<<" ";

cout<<endl;

getch();

void insort(int a[],int n)

int temp,j;

a[0]=INT_MIN;

for(int i=1;i<=n;i++)

temp=a[i];

j=i-1;
while(temp<a[j])

a[j+1]=a[j];

j--;

a[j+1]=temp;

cout<<"Array after pass"<<i<<" is";

for(int k=1;k<=n;k++)

cout<<a[k]<<" ";

cout<<endl;

OUTPUT
11. INSERTION USING LINKED LIST
#include<iostream.h>

#include<conio.h>

struct node

{ int info;

node*next;
}

*ptr,*newptr,*save,*next,*start;

node*new_node(int);

void ins(node*);

void dis(node*);

void main()

clrscr();

start=NULL;

int inf;

char ch='y';

while(ch=='y'||ch=='Y')
{

cout<<"enter the information\n";

cin>>inf;

newptr=new_node(inf);

ins(newptr);

cout<<"the new list is\n";

dis(start);

cout<<"do you want to enter\n";

cin>>ch;

}
getch();

node* new_node(int n)

ptr=new node;

ptr->info=n;

ptr->next=NULL;

return ptr;

void ins(node* np)

{
if(start==NULL)

start=np;

else

save=start;

start=np;

np->next=save;

void dis(node*st)

while(st!=NULL)

cout<<st->info<<"->";

st=st-> next;
}

OUTPUT
12. STATIC FUNCTION
#include<iostream.h>

#include<conio.h>

#include<process.h>

int insert(int[],int);

void display(int[],int,int);

const int size=20;

int queue[size],front=-1,rear=-1;

void main()

clrscr();

int item,res;

char ch='y';

while(ch=='y'||ch=='Y')

{
cout<<"enter item:";

cin>>item;

res=insert(queue,item);

if(res==-1)

cout<<"overflowed";

exit(1);

cout<<"The queue (front to rear) is"<<endl;

display(queue,front,rear);

cout<<"want to insert more";

cin>>ch;

getch();

}
int insert(int queue[],int ele)

if(rear==size-1) return -1;

else if(rear==-1)

front=rear=0;

queue[rear]=ele;

else

{rear++;

queue[rear]=ele;}

return 0;

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

{
if(front==-1) return;

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

cout<<queue[i]<<"<--\t";

cout<<queue[rear]<<endl;

OUTPUT

13. CONSTRUCTOR AND DESTRUCTOR


#include<iostream.h>

#include<conio.h>
int count=0;

class des

public:

des()

count++;

cout<<"Constructor "<<count<<"\n";

~des()

cout<<"Desructor "<<count<<"\n";

count--;

}
};

void main()

clrscr();

des a,b;

cout<<"Enter block1 \n";

des c;

cout<<"Enter block2 \n";

des d;

}
cout<<"\n"<<count;

getch();

OUTPUT
14. COPY CONSTRUCTOR
#include<iostream.h>

#include<conio.h>

class con

int a;

int b;

public:

con()

cout<<"constructor with no arguments \n";

}
con(int i,int j=100)

a=i;

b=j;

cout<<a<<" "<<b<<"\n";

con(con &i)

a=i.a;

b=i.b;

cout<<"copy constructor "<<a<<" "<<b<<"\n";

}
};

void main()

clrscr();

con c1;

con c2(120);

con c3=c2;

getch();

OUTPUT
15. INHERITANCE
#include<iostream.h>

#include<conio.h>

class A

{ int a;

public:

void read()

{ cout<<"Enter any integer : ";

cin>>a;

cout<<"\n";
}

void disp()

{ cout<<a<<endl;

};

class B:public A

{ char b;

public:

void reading()

{ cout<<"Enter any character : ";

cin>>b;

cout<<"\n";

void display()

{ cout<<b;
}

};

void main()

{ clrscr();

A ob1;

B ob2;

ob1.read();

ob1.disp();

cout<<"THANK YOU ..."<<endl;

ob2.read();
ob2.disp();

ob2.reading();

ob2.display();

getch();

OUTPUT
16. INSERTION SORT
#include<iostream.h>

#include<conio.h>

#include<stdio.h>

void main()

clrscr();

int i,ch;

struct emp

char name[10];

int salary;

int contact;

char address[10];

int code;
};

emp s1[2];

cout<<"enter details of employe"<<"\n";

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

cout<<" Employe "<<i+1<<"\n";

cout<<"enter code";

cin>>s1[i].code;

cout<<"enter the name : ";

gets(s1[i].name);

cout<<"enter salary : ";

cin>>s1[i].salary;

cout<<"enter contact details : ";

cin>>s1[i].contact;

cout<<"enter address : ";


gets(s1[i].address);

cout<<"\n\n";

cout<<" OUTPUT "<<"\n";

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

cout<<"employe"<<i+1<<"\n";

cout<<"name = ";

cout.write(s1[i].name,10)<<"\n";

cout<<"salary="<<s1[i].salary<<"\n";

cout<<"contact number ="<<s1[i].contact<<"\n";

cout<<"address = ";

cout.write(s1[i].address,10)<<"\n";

cout<<"\n";

}
getch();

OUTPUT

17. FRIEND FUNCTION


#include<iostream.h>

#include<conio.h>

class B;
class A

{ int a;

public:

void set(int i)

{ a=i;

friend void max(A,B);

};

class B

{ int b;

public:
void set(int i)

{ b=i;

friend void max(A,B);

};

void max(A x,B y)

if(x.a>y.b)

cout<<x.a<<" is greater";

else

cout<<y.b<< "is greater";

}
void main()

{ clrscr();

A abc;

B xyz;

int a,b;

cout<<"Enter 2 numbers to compare them :: ";

cin>>a>>b;

abc.set(a);

xyz.set(b);

max(abc,xyz);

getch();
}

OUTPUT

18. STATIC FUNCTION


#include<iostream.h>

#include<conio.h>

class stat

static int count;


public:

stat()

++count;

cout<<"hello "<<count<<"\n";

static void counting()

cout<<"no. of objects created "<<count;

};

int stat::count;
void main()

clrscr();

stat s1,s2,s3;

stat::counting();

getch();

OUTPUT
19. DATA FILE HANDALING
#include<iostream.h>

#include<fstream.h>

#include<conio.h>

void main()

{ clrscr();

char n;
int m;

ofstream fo("file.txt",ios::trunc);

cout<<"Enetr your name initial :: ";

cin>>n;

cout<<"Enetr your marks :: ";

cin>>m;

fo<<n<<"\n"<<m;

fo.close();

//

ifstream fin("file.txt",ios::in);

fin>>n>>m;

cout<<n<<endl<<m;
fin.close();

getch();

OUTPUT

20. EQUALITY OF MATRICES


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int m[3][3],n[3][3],i,j,result;

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

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

cout<<"enter elements of matrix 1 ";

cin>>m[i][j];

cout<<"\n";

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

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

cout<<"enter elements of matrix 2 ";

cin>>n[i][j];

cout<<"\n";
}

cout<<" matrix 1 \n";

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

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

cout<<m[i][j]<<" ";

cout<<"\n";

cout<<" matrix 2 \n";

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

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

{
cout<<n[i][j]<<" ";

cout<<"\n";

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

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

if(m[i][j]==n[i][j])

result=1;

else

result =0;

if(result==1)

cout<<"matrices are equal ";


else

cout<<"matrices are not equal ";

getch();

OUTPUT
21. ENTERED CHARACTER..
#include<iostream.h>

#include<conio.h>
#include<ctype.h>

void main()

clrscr();

char ch;

cout<<"enter a character ";

cin>>ch;

if(isdigit(ch))

{int a;

ch=a;

cout<<"it is a digit";

if(a=0-a)

cout<<"it is negative"<<"\n";}

else
{cout<<"it is a positive number"<<"\n"; }

else if(isalpha(ch))

cout<<"it is a alphabet"<<"\n";

switch(ch)

case'a':cout<<"\n"<<"it is a vowel";break;

case'e':cout<<"\n"<<"it is a vowel";break;

case'i':cout<<"\n"<<"it is a vowel";break;

case'o':cout<<"\n"<<"it is a vowel";break;

case'u':cout<<"\n"<<"it is a vowel";break;

case'A':cout<<"\n"<<"it is a vowel";break;

case'E':cout<<"\n"<<"it is a vowel";break;
case'I':cout<<"\n"<<"it is a vowel";break;

case'O':cout<<"\n"<<"it is a vowel";break;

case'U':cout<<"\n"<<"it is a vowel";break;

default: cout<<"it is a consonant"<<"\n";break;

if(islower(ch))

cout<<"lower case letter"<<"\n";

else if(isupper(ch))

cout<<"upper case letter"<<"\n";

getch();
}

OUTPUT

You might also like