0% found this document useful (0 votes)
125 views41 pages

C++ Slips PDF

The document contains C code to perform operations on binary search trees and sorted arrays. It includes functions to: 1) Create a binary search tree by inserting nodes and print inorder and postorder traversals. 2) Quicksort an array of employee records by age and write the sorted array to a file. 3) Read employee records from a file into an array, sort the array using quicksort, and write it back to a new file.

Uploaded by

Monika Tarwate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views41 pages

C++ Slips PDF

The document contains C code to perform operations on binary search trees and sorted arrays. It includes functions to: 1) Create a binary search tree by inserting nodes and print inorder and postorder traversals. 2) Quicksort an array of employee records by age and write the sorted array to a file. 3) Read employee records from a file into an array, sort the array using quicksort, and write it back to a new file.

Uploaded by

Monika Tarwate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Slip no 9_1:

#include<stdio.h>

#include<string.h>

typedef struct employee

char name[10];

int age;

}record;

record employee[100];

int readfile(record *a)

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);

void writefile(record *a,int n)

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);

merge(record *a,int l,int m,int u)

record c[10]; int i,j,k;

i=l;

j=m+1;

k=0;

while(i<=m && j<=u)

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];

merge_sort(record *a,int i,int 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;

struct node *r;

struct node *l;

};

#define newnode (struct node*)malloc(sizeof(struct node))

struct node *insert(struct node *,int);

struct node *create()

int n,x,i;

struct node *root;

root=NULL;

printf("\n Enter no of nodes");

scanf("%d",&n);

printf("\nEnter tree values");

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

{
scanf("%d",&x);

root=insert(root,x);

return root;

struct node *insert(struct node *rt,int x)

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;

void inorder(struct node *tmp)

{
if(tmp!=NULL)

inorder(tmp->l);

printf("%d->",tmp->data);

inorder(tmp->r);

void postorder(struct node *tmp)

if(tmp!=NULL)

postorder(tmp->l);

postorder(tmp->r);

printf("%d->",tmp->data);

void preorder(struct node *tmp)

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;

struct node *rt;

rt=create();

printf("\nInorder\n");

inorder(rt);

printf("\npostorder\n");

postorder(rt);

printf("\npreorder\n");

preorder(rt);

printf("\nEnter element to insert : ");

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)

printf("\nNode is found in tree \n");

else

printf("\nNode is not found in tree \n");

Ouput:

Enter no of nodes5

Enter tree values6

Inorder

1->2->4->6->8->

postorder

1->2->4->8->6->

preorder

6->4->2->1->8->

Enter element to insert : 3


1->2->3->4->6->8->

Enter the node to be searched

Node is found in tree

Slip 9_C++_1:

#include <iostream>

using namespace std;

class Data

public:

int maximum(int a,int b)

if(a>b)

return a;

else

return b;

int maximum(int *a,int len)

int max=a[0];

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

if(a[i]>max)
max=a[i];

return max;

void maximum(int *a,int len,int n)

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;

cout<<"\n Enter two elements:";

cin>>n1;

cin>>n2;

max=ob.maximum(n1,n2);

cout<<"\n max no="<<max;

cout<<"\n Enter how many elements for array:";

int a[10];

cin>>n;
for(int i=0;i<n;i++)

cout<<"\n Enter element:";

cin>>a[i];

cout<<"\n Max element of given array:"<<ob.maximum(a,n);

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

cin>>num;

cout<<"\n Element greater than"<<num;

ob.maximum(a,n,num);

Ouput:

Enter two elements:3 5

max no=5

Enter how many elements for array: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

Element greater than2

no=8

no=3

no=6

slip 10_DS_1:

#include<stdio.h>

enum bool {false,true};

typedef struct employee

int age;

char name[10];

}record;

record employee[50];

int readfile(record *a)

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);

void writefile(record *a,int n)

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);

//enum bool {false,true};

void quick(record *a,int l,int u)

record temp;int piv,left,right;

enum bool pivot_places=false;

left=l;

right=u;

piv=l;

if(l>=u)

return;
/*

printf("\nsublist:\n");

disp(a,l,u);*/

while(pivot_places==false)

while(a[piv].age<=a[right].age && piv!=right)

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;

while(a[piv].age>=a[left].age && piv!=left)

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;

struct node *r;

struct node *l;

};

#define newnode (struct node*)malloc(sizeof(struct node))

struct node *insert(struct node *,int);

struct node *create()

{
int n,x,i;

struct node *root;

root=NULL;

printf("\n Enter no of nodes");

scanf("%d",&n);

printf("\nEnter tree values");

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

scanf("%d",&x);

root=insert(root,x);

return root;

struct node *insert(struct node *rt,int x)

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;

void inorder(struct node *tmp)

if(tmp!=NULL)

inorder(tmp->l);

printf("%d->",tmp->data);

inorder(tmp->r);

void postorder(struct node *tmp)

if(tmp!=NULL)

postorder(tmp->l);

postorder(tmp->r);

printf("%d->",tmp->data);
}

main()

int no,y;

struct node *rt;

rt=create();

printf("\nInorder\n");

inorder(rt);

printf("\npostorder\n");

postorder(rt);

Output:

Enter no of nodes5

Enter tree values1

Inorder

1->2->3->6->8->

postorder
2->3->8->6->1->

Slip 10_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;

}ob1;

main()

int n;

char *str,*str1;

cout<<"Enter the value of n : ";

cin>>n;

cout<<"\n Inverted No = "<<ob1.invert(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;

Output:

Enter the value of n : 5438


Inverted No = 8345

Enter the string : comp

Inverted String = pmoc

Slip 11_DS_1:

#include<stdio.h>

int linear_search(int a[],int n,int sr)

int i;

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

if(a[i]==sr)

return i;

return -1;

void generate(int a[],int n)

int i;

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

a[i]=rand()%20;

void display(int a[],int n)

{ int i;

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

{
printf("%d\t",a[i]);

main()

int a[20],i,j,n,x,ans;

printf("\n Enter how many elemants:");

scanf("%d",&n);

generate(a,n);

printf("\n Elements are:\n");

display(a,n);

printf("\n Enter searching element : ");

scanf("%d",&x);

ans=linear_search(a,n,x);

if(ans==-1)

printf("\n %d is NOT found.",x);

else

printf("\n %d is found at %d position\n",x,ans+1);

Output:

Enter how many elemants:5

Elements are:

1 7 14 0 9

Enter searching element : 7

7 is found at 2 position
Slip 11_DS_2: from xerox

Slip 11_c++_1:

#include<iostream>

#include<string.h>

using namespace std;

class Student

protected: int rno;

char name[20];

public:

Student(int r, char name1[])

rno=r;

strcpy(name,name1);

void display()

cout<<"\n Rno : "<<rno;

cout<<"\t Name : "<<name;

};

class StudentExam : public Student

protected : int *ptr;

int no;
public :

StudentExam(int r,char nm[],int n):Student(r,nm)

no=n;

ptr = new int[no];

void accept()

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

{ cout<<"\n Enter marks ";

cin>>ptr[i];

void display()

{ cout<<"\n========== Markslist============ \n ";

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

cout<<"subject"<<i<<" : "<<ptr[i]<<"\n";

};

class StudentResult :public StudentExam

float per;char grade;

public :

StudentResult(int r,char nm[],int no):StudentExam(r,nm,no)

{
}

void cal()

int t=0;

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

{ t=t+ptr[i];

per=t/no;

if(per>70)

grade ='A';

else if(per>=60 && per<70)

grade ='B';

else if(per>=50 && per<60)

grade='C';

else if(per>=40 && per<50)

grade ='D';

else grade ='E';

Student::display();

StudentExam::display();

cout<<"======================================================";

cout<<"\n Percentage = "<<per<<"%";

cout<<"\n Grade = "<<grade<<endl;

};
main()

int no,s;char name[20];

cout<<"\n enter roll no ";

cin>>no;

cout<<"\n Enter name :";

cin>>name;

cout<<"\n Enter no of subject :";

cin>>s;

StudentResult ob(no,name,s);

ob.accept();

cout<<"\n MARLKIST IS \n ";

ob.cal();

Output:

enter roll no 30

Enter name :shruti

Enter no of subject :4

Enter marks 84

Enter marks 82

Enter marks 89
Enter marks 90

MARLKIST IS

Rno : 30 Name : shruti

========== Markslist============

subject0 : 84

subject1 : 82

subject2 : 89

subject3 : 90

======================================================

Percentage = 86%

Grade = A

Slip 12_DS_1:

#include<stdio.h>

#include<string.h>

typedef struct city

char name[20];

int code;

}record;

record city[100];

int read_file(record *a)

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);

void l_search(record *a,int n,char x[20])

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)

printf("\ncity not found\n");

main()

{
char x[20];

int n;

n=read_file(city);

printf("\nenter city name\n");

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 no of nodes for 1st link5

enter data3

3 4 2 7 1

enter data to be append : 5

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()

cout<<"\n Enter the length ";

cin>>l;

cout<<"\n Enter the breadth ";

cin>>b;

cout<<"\n Enter the height ";

cin>>h;

cout<<"\n Enter the mass ";

cin>>m;

void display()

{ cout<<"\n length = "<<l<<"\n breadth ="<<b;

cout<<"\n height = "<<h<<"\n Mass = "<<m;

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();

cout<<"\n Volume = "<<C.getVolume();

cout<<"\n Surface Area = "<<C.getsurfaceArea();

cout<<"\n Density = "<<C.getDensity();;

Output:

Enter the length 5

Enter the breadth 3

Enter the height 7

Enter the mass 1


length = 5

breadth =3

height = 7

Mass = 1

Volume = 105

Surface Area = 73

Density = 0.00952381

Slip 13_DS_or _1: Do case study of postfix form ab+cd-*ab/

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;

cout<<"\n Inverted No = "<<ob1.invert(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:

Enter the value of n : 5438

Inverted No = 8345

Enter the string : comp

Inverted String = pmoc


Enter how many elements in array :

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 2:printf("\n Link list is :\n");


display(H);
break;

case 3:
printf("\n Enter data to be search : ");
scanf("%d",&sr);
search(H,sr);
break;

case 4: printf("\n Enter position to be data added :");


scanf("%d",&d);
H=insert(H,d);
break;

case 5:printf("\n Enter position to be data deleted :");


scanf("%d",&d);
H=delete(H,d);
break;

case 6: exit(0);
break;

default :printf("\n Invalid choice ");


}
}while(ch!=6);
}
Refer remaining program in Xerox or mobile
Slip 14_c++_1:
#include<iostream>
#include<string.h>
using namespace std;
class printdata
{
public:
void print(int no)
{
cout<<"\n<"<<no<<">";
}
void print(char *str)
{
cout<<"\""<<str<<"\"";
}
void print(int n,char *str)
{ char *str1=new char[20];int j;
for(int i=0;str[i]!='\0';i++)
{ j=i;
if(i==n)
break;
else {
str1[i]=str[i];

}
}
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];

cout<<"\n Enter string : ";


cin>>str;
ob1.print(str);

cout<<"\n Enter string :";


cin>>str;
cout<<"\n Enter no of charcters to be display : ";
cin>>num;
ob1.print(num,str);
return 0;
}
Output:

Enter num : 10

<10>

Enter string : hi

"hi"

Enter string :object

Enter no of charcters to be display : 3

"obj"

Slip 15_DS_1:

Refer slip 11_DS_1 solution

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 operator+(complex ob1,complex ob2)


{
complex temp;
temp.x=ob1.x+ob2.x;
temp.y=ob1.y+ob2.y;
return temp;
}
complex operator-(complex ob1,complex ob2)
{
complex temp;
temp.x=ob1.x-ob2.x;
temp.y=ob1.y-ob2.y;
return temp;
}
complex operator*(complex ob1,complex ob2)
{
complex temp;
temp.x=ob1.x*ob2.x;
temp.y=ob1.y*ob2.y;
return temp;
}
main()
{
float r,img,r1,img1;
cout<<"Enter 1st real no : ";
cin>>r;
cout<<"Enter 1st imaginary no : ";
cin>>img;
cout<<"Enter 2nd real no : ";
cin>>r1;
cout<<"Enter 2nd imaginary no : ";
cin>>img1;

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();
}

You might also like