C++ Slips PDF
C++ Slips PDF
#include<stdio.h>
#include<string.h>
char name[10];
int age;
}record;
record employee[100];
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
while(!feof(fp))
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
return(i-1);
int i=0;
FILE *fp;
if((fp=fopen("emp12.txt","w"))!=NULL)
for(i=0;i<n;i++)
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
i=l;
j=m+1;
k=0;
if(a[i].age<a[j].age)
c[k]=a[i];
k++;i++;
else
c[k]=a[j];
k++;j++;
}
while(i<=m)
c[k]=a[i];
i++;k++;
while(j<=u)
c[k]=a[j];
k++;j++;
for(i=l,j=0;i<=u;i++,j++)
a[i]=c[j];
int k=0;
if(i<j)
k=(i+j)/2;
merge_sort(a,i,k);
merge_sort(a,k+1,j);
merge(a,i,k,j);
main()
{
int n;
n=readfile(employee);
merge_sort(employee,0,n-1);
writefile(employee,n);
Slip 9_2:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
int n,x,i;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
return root;
if(rt==NULL)
rt=newnode;
rt->data=x;
rt->l=NULL;
rt->r=NULL;
return rt;
else if(x>rt->data)
rt->r=insert(rt->r,x);
return rt;
else
rt->l=insert(rt->l,x);
return rt;
{
if(tmp!=NULL)
inorder(tmp->l);
printf("%d->",tmp->data);
inorder(tmp->r);
if(tmp!=NULL)
postorder(tmp->l);
postorder(tmp->r);
printf("%d->",tmp->data);
if(tmp!=NULL)
printf("%d->",tmp->data);
preorder(tmp->l);
preorder(tmp->r);
}
struct node *search(struct node *tmp,int x)
if(tmp==NULL)
return (NULL);
if(tmp->data==x)
return (tmp);
if(x>tmp->data)
return(search((tmp->r),x));
return(search((tmp->l),x));
main()
int no,y;
rt=create();
printf("\nInorder\n");
inorder(rt);
printf("\npostorder\n");
postorder(rt);
printf("\npreorder\n");
preorder(rt);
scanf("%d",&no);
rt=insert(rt,no);
inorder(rt);
printf("\nEnter the node to be searched \n");
scanf("%d",&y);
rt=search(rt,y);
if(rt!=NULL)
else
Ouput:
Enter no of nodes5
Inorder
1->2->4->6->8->
postorder
1->2->4->8->6->
preorder
6->4->2->1->8->
Slip 9_C++_1:
#include <iostream>
class Data
public:
if(a>b)
return a;
else
return b;
int max=a[0];
for(int i=1;i<len;i++)
if(a[i]>max)
max=a[i];
return max;
for(int i=0;i<len;i++)
if(a[i]>n)
cout<<"\n no="<<a[i];
};
main()
Data ob;
int n1,n2,num,n,max,min;
cin>>n1;
cin>>n2;
max=ob.maximum(n1,n2);
int a[10];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
cin>>num;
ob.maximum(a,n,num);
Ouput:
max no=5
Enter element:1
Enter element:8
Enter element:3
Enter element:6
Enter element:2
Max element of given array:8
Enter a no:2
no=8
no=3
no=6
slip 10_DS_1:
#include<stdio.h>
int age;
char name[10];
}record;
record employee[50];
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
while(!feof(fp))
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}return (i-1);
int i;
FILE *fp;
if((fp=fopen("emp14.txt","w"))!=NULL)
for(i=0;i<n;i++)
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
left=l;
right=u;
piv=l;
if(l>=u)
return;
/*
printf("\nsublist:\n");
disp(a,l,u);*/
while(pivot_places==false)
right--;
if(piv==right)
pivot_places=true;
if(a[piv].age>a[right].age)
temp=a[piv];
a[piv]=a[right];
a[right]=temp;
piv=right;
left++;
if(piv==left)
pivot_places=true;
if(a[piv].age<a[left].age)
temp=a[piv];
a[piv]=a[left];
a[left]=temp;
piv=left;
}
}
//disp(a,l,u);
quick(a,l,piv-1);
quick(a,piv+1,u);
main()
int n;
n=readfile(employee);
quick(employee,0,n-1);
writefile(employee,n);
Slip 10_DS_2:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
{
int n,x,i;
root=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x);
root=insert(root,x);
return root;
if(rt==NULL)
rt=newnode;
rt->data=x;
rt->l=NULL;
rt->r=NULL;
return rt;
else if(x>rt->data)
rt->r=insert(rt->r,x);
return rt;
else
rt->l=insert(rt->l,x);
return rt;
if(tmp!=NULL)
inorder(tmp->l);
printf("%d->",tmp->data);
inorder(tmp->r);
if(tmp!=NULL)
postorder(tmp->l);
postorder(tmp->r);
printf("%d->",tmp->data);
}
main()
int no,y;
rt=create();
printf("\nInorder\n");
inorder(rt);
printf("\npostorder\n");
postorder(rt);
Output:
Enter no of nodes5
Inorder
1->2->3->6->8->
postorder
2->3->8->6->1->
Slip 10_c++_1:
#include<iostream>
#include<string.h>
int cnt=0;
class invertdata
public:
int invert(int n)
int rev=0,r;
while(n!=0)
r=n%10;
rev=rev*10+r;
n=n/10;
return rev;
int i,j,len;
len=strlen(s1);
for(i=(len-1),j=0;i>=0;i--,j++)
{
s2[j]=s1[i];
s2[j]='\0';
return s2;
}ob1;
main()
int n;
char *str,*str1;
cin>>n;
str=new char[20];
str1=new char[20];
cin>>str;
str1=ob1.invert(str);
Output:
Slip 11_DS_1:
#include<stdio.h>
int i;
for(i=0;i<n;i++)
if(a[i]==sr)
return i;
return -1;
int i;
for(i=0;i<n;i++)
a[i]=rand()%20;
{ int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
main()
int a[20],i,j,n,x,ans;
scanf("%d",&n);
generate(a,n);
display(a,n);
scanf("%d",&x);
ans=linear_search(a,n,x);
if(ans==-1)
else
Output:
Elements are:
1 7 14 0 9
7 is found at 2 position
Slip 11_DS_2: from xerox
Slip 11_c++_1:
#include<iostream>
#include<string.h>
class Student
char name[20];
public:
rno=r;
strcpy(name,name1);
void display()
};
int no;
public :
no=n;
void accept()
for(int i=0;i<no;i++)
cin>>ptr[i];
void display()
for(int i=0;i<no;i++)
cout<<"subject"<<i<<" : "<<ptr[i]<<"\n";
};
public :
{
}
void cal()
int t=0;
for(int i=0;i<no;i++)
{ t=t+ptr[i];
per=t/no;
if(per>70)
grade ='A';
grade ='B';
grade='C';
grade ='D';
Student::display();
StudentExam::display();
cout<<"======================================================";
};
main()
cin>>no;
cin>>name;
cin>>s;
StudentResult ob(no,name,s);
ob.accept();
ob.cal();
Output:
enter roll no 30
Enter no of subject :4
Enter marks 84
Enter marks 82
Enter marks 89
Enter marks 90
MARLKIST IS
========== Markslist============
subject0 : 84
subject1 : 82
subject2 : 89
subject3 : 90
======================================================
Percentage = 86%
Grade = A
Slip 12_DS_1:
#include<stdio.h>
#include<string.h>
char name[20];
int code;
}record;
record city[100];
int i=0;
FILE *fp;
if((fp=fopen("cities.txt","r"))!=NULL)
while(!feof(fp))
fscanf(fp,"%s%d",a[i].name,&a[i].code);
i++;
}}
return (i-1);
int i;
for(i=0;i<n;i++)
if(strcmp(a[i].name,x)==0)
printf("\n%s=%d\n",a[i].name,a[i].code);
break;
if(i==n)
main()
{
char x[20];
int n;
n=read_file(city);
gets(x);
l_search(city,n,x);
Slip 12_DS_2:
#include<stdio.h>
#include<stdlib.h>
#define newnode (struct snode*)malloc(sizeof(struct snode))
struct snode
{
int data;
struct snode *next;
};
struct snode *create(int no)
{
struct snode *f,*s;
int i;
f=newnode;
printf("enter data");
scanf("%d",&f->data);
f->next=NULL;
s=f;
for(i=2;i<=no;i++)
{
s->next=newnode;
s=s->next;
scanf("%d",&s->data);
s->next=NULL;
}
s->next=f;
return f;
}
void display(struct snode *f)
{
struct snode *s;
s=f;
do
{printf("\t%d",s->data);
s=s->next;
}while(s!=f);
}
struct node * append(struct snode *f,int n)
{ struct snode *t,*s;
s=newnode;
s->data=n;
t=f;
int i,count=0;
do
{ count++;
t=t->next;
} while(t->next!=f);
t->next=s;
t=t->next;
t->next=f;
return f;
}
main()
{ int no,num;
struct snode *l1,*l2;
printf("enter no of nodes for 1st link");
scanf("%d",&no);
l1=create(no);
display(l1);
printf("\nenter data to be append : ");
scanf("%d",&num);
l1=append(l1,num);
display(l1);
}
Ouput:
enter data3
3 4 2 7 1
3 4 2 7 1 5
Slip 12_C++_1:
#include<iostream>
using namespace std;
class cuboidsolid
float l,b,h,m;
public:
void accept()
cin>>l;
cin>>b;
cin>>h;
cin>>m;
void display()
float getVolume()
{ return l*b*h;
float getsurfaceArea()
{
return (2+(l*b)+(b*h)+(h*l));
float getDensity()
{ float den;
den=m/getVolume();
return den;
};
main()
{ cuboidsolid C;
C.accept();
C.display();
Output:
breadth =3
height = 7
Mass = 1
Volume = 105
Surface Area = 73
Density = 0.00952381
Slip 13_c++_1:
#include<iostream>
#include<string.h>
using namespace std;
int cnt=0;
class invertdata
{
public:
int invert(int n)
{
int rev=0,r;
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
return rev;
}
char *invert(char *s1)
{
int i,j,len;
len=strlen(s1);
char *s2=new char[len+1];
for(i=(len-1),j=0;i>=0;i--,j++)
{
s2[j]=s1[i];
}
s2[j]='\0';
return s2;
}
void invert(int *p)
{ int i;
for(i=cnt-1;i>=0;i--)
{
cout<<p[i]<<"\t";
}
}
}ob1;
main()
{
int n;
char *str,*str1;
cout<<"Enter the value of n : ";
cin>>n;
str=new char[20];
str1=new char[20];
cout<<"\nEnter the string : ";
cin>>str;
str1=ob1.invert(str);
cout<<"\n Inverted String = "<<str1<<endl;
int a[20];
cout<<"Enter how many elements in array :\n";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\n Enter element : ";
cin>>a[i];
cnt++;
}
cout<<"\n Inverted Array is ";
ob1.invert(a);
}
Ouput:
Inverted No = 8345
Enter element : 5
Enter element : 7
Enter element : 12
Enter element : 4
Inverted Array is 4 12 7 5
Slip 14_DS_1:
#include<stdio.h>
#include"stack.h"
main()
{
int n,i=0,ch;
init();
do
{
printf("\nchoices are:\n1:push into stack\n2:pop from stack\n3:check whether stack is
empty or not\n4:check whether stack is full or not\n5:peek(top) element of stack\n6:display
elements of stack\n7:exit\n");
printf("enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nenter element:");
scanf("%d",&n);
push(n);
break;
case 2:
printf("\ndeleted elements is:%d",pop());
break;
case 3: i=isempty();
if(i==1)
printf("\nstack is empty");
else
printf("\nstack is not empty");
break;
case 4: i=isfull();
if(i==1)
printf("\nstack is full");
else
printf("\nstack is not full");
break;
case 5: printf("\ntop element of stack is:%d",peek());
break;
case 6: display();
break;
case 7:exit(0);
}
}while(ch!=7);
}
Refer remaining program in xerox
Slip_14_DS_2:
#include<stdio.h>
#include<stdlib.h>
#include "doublylist.h"
void main()
{
int n,ch,sr,d;
struct dnode *H;
do
{
printf("\nMENU\n");
printf("\n1.Create");
printf("\n2.Display");
printf("\n3.Search");
printf("\n4.Insert");
printf("\n5.Delete");
printf("\n6.Exit\n");
printf("\nEnter Ur Choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nHow many nodes you want to \n");
scanf("%d",&n);
H=create(n);
break;
case 3:
printf("\n Enter data to be search : ");
scanf("%d",&sr);
search(H,sr);
break;
case 6: exit(0);
break;
}
}
str1[j]='\0';
cout<<"\n\""<<str1<<"\"";
}
};
int main()
{
printdata ob1;
int num,n1,n2;
char *str,ch;
cout<<"\n Enter num : ";
cin>>num;
ob1.print(num);
str=new char[30];
Enter num : 10
<10>
Enter string : hi
"hi"
"obj"
Slip 15_DS_1:
Slip 15_DS_2:
#include<stdio.h>
#include"dystack.h"
main()
{
int n,i=0,ch;
init();
do
{
printf("\nchoices are:\n1:push into stack\n2:pop from stack\n3:check whether stack is
empty or not\n4:check whether stack is full or not\n5:peek(top) element of stack\n6:display
elements of stack\n7:exit\n");
printf("enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nenter element:");
scanf("%d",&n);
push(n);
break;
case 2:
printf("\ndeleted elements is:%d",pop());
break;
case 3: i=isempty();
if(i==1)
printf("\nstack is empty");
else
printf("\nstack is not empty");
break;
case 4: i=isfull();
if(i==1)
printf("\nstack is full");
else
printf("\nstack is not full");
break;
case 5: printf("\ntop element of stack is:%d",peek());
break;
case 6: display();
break;
case 7:exit(0);
}
}while(ch!=7);
}
Refer remaining program from Xerox
Slip_15_c++_2:
#include<iostream>
using namespace std;
class complex
{
float x, y;
public:
complex()
{
}
complex(float r,float img)
{
x=r;
y=img;
}
friend complex operator +(complex,complex);
friend complex operator -(complex,complex);
friend complex operator *(complex,complex);
void display()
{
cout<<"\t x= "<< x<<"\t "<<"y= "<<y<<"i"<<endl;
}
};
complex c1,c2,c3,c4,c5;
c1=complex(r,img);
c2=complex(r1,img1);
c3=c1+c2;
c4=c1-c2;
c5=c1*c2;
cout<<"c1 = ";
c1.display();
cout<<"c2 = ";
c2.display();
cout<<"c1+c2 = ";
c3.display();
cout<<"c1-c2 = ";
c4.display();
cout<<"c1*c2 = ";
c5.display();
}