0% found this document useful (0 votes)
515 views

Computer Practical File

The document contains 21 questions related to C++ programming. Each question provides a full code sample to solve a specific programming problem, such as sorting arrays, searching arrays, stack and queue implementations, inheritance concepts, and file handling programs to count characters, words, and vowels in a text file. The questions cover a wide range of fundamental C++ concepts like arrays, functions, classes, inheritance, file I/O, and data structures.

Uploaded by

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

Computer Practical File

The document contains 21 questions related to C++ programming. Each question provides a full code sample to solve a specific programming problem, such as sorting arrays, searching arrays, stack and queue implementations, inheritance concepts, and file handling programs to count characters, words, and vowels in a text file. The questions cover a wide range of fundamental C++ concepts like arrays, functions, classes, inheritance, file I/O, and data structures.

Uploaded by

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

Q.1.

Write a program in C++ to print the given numbers


in ascending order using Bubble sort.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,temp;
cout<<"enter the element of array";
cin>>n;
for (int i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for (int j=0;j<n-i-1;j++)
{
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"value as ascending order\n";

for (i=0;i<n;i++)
{
cout<<a[i]<<"

}
getch();
}

";

Q.2. Write a program in C++ to print the given numbers


in descending order using Bubble sort.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,temp;
cout<<"enter the element of array";
cin>>n;
for (int i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for (int j=0;j<n-i-1;j++)
{
if (a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"value as decending order\n";

for (i=0;i<n;i++)
{
cout<<a[i]<<"

}
getch();
}

";

Q.3. Write a program in C++ to print the given numbers


in ascending order using Insertion sort.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,j,temp;
cout<<"enter the element of array";
cin>>n;
for (int i=0;i<10;i++)
{
cin>>a[i];
}
for(i=1;i<10;i++)
{
temp=a[i];
j=i-1;
while (temp<a[j]&&j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout<<"insertion sort\n";
for (i=0;i<10;i++)

{
cout<<a[i]<<" ";
}
getch();
}

Q.4. Write a program in C++ to print the given numbers


in ascending order using Selection sort.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,j,temp;
cout<<"enter the element of array";
cin>>n;
for (int i=0;i<10;i++)
{
cin>>a[i];
}
for(i=1;i<10;i++)
{
temp=a[i];
j=i-1;
while (temp<=a[j]&&j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout<<"sorted array\n";

for (i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
getch();
}

Q.5. Write a program in C++ to merge the two arrays


elements and print the same in ascending order using
Merge sort. (Hint: first array is in ascending and second
array is in descending order)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],b[5], c[10];
int i=0,j=4,k=0,x;
cout<<"enter first array";
for (x=0;x<5;x++)
{
cin>>a[x];
}
cout<<"enter second array";
for (x=0;x<5;x++)
{
cin>>b[x];
}
while (i<5&&j>=0)
{
if (a[i]<b[j])
{
c[k]=a[i];

k++;i++;
}
else if (b[j]<a[i])
{
c[k]=b[j];
k++;j--;
}
else
{
c[k]=a[i];
k++;i++;j--;
}
}
while (i<=4)
{
c[k]=a[i];
k++;i++;
}
while (j>=0)
{
c[k]=b[j];
k++;j--;
}
cout<<"merged array is";
for(i=0;i<10;i++)
{

cout<<c[i]<<" ";
}
getch();
}

Q.6. Write a program in C++ to merge the two arrays


elements and print the same in descending order using
Merge sort. (Hint: first array is in ascending and second
array is in descending order)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],b[5], c[10];
int i=4,j=0,k=0,x;
cout<<"enter first array";
for (x=0;x<5;x++)
{
cin>>a[x];
}
cout<<"enter second array";
for (x=0;x<5;x++)
{
cin>>b[x];
}
while (i>=0&&j<5)
{
if (a[i]>b[j])
{

c[k]=a[i];

k++;i--;
}
else if (b[j]>a[i])
{
c[k]=b[j];
k++;j++;
}
else
{
c[k]=a[i];
k++;i--;j++;
}
}
while (i>=0)
{
c[k]=a[i];
k++;i--;
}
while (j<5)
{
c[k]=b[j];
k++;j++;
}
cout<<"merged array is";
for(i=0;i<10;i++)

{
cout<<c[i]<<" ";
}
getch();
}

Q.8. Write a program in C++ to implement Linear Search


in 1-D array.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i,n,a[5],ctr=0;
cout<<"enter 5 numbers";
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<"\n enter elements to search";
cin>>n;
for(i=0;i<5;i++)
{
if(a[i]==n)
{
cout<<"item found at"<<i+1;
ctr=1;
break;
}
}
if(ctr==0)

{
cout<<"element is not found";
}
getch();
}

Q.9 Write a program in C++ to implement Binary Search


in 1-D array.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int n, i,a[5],l=0,u=4,mid,flag=0;
for(i=0;i<5;i++)
{
cout<<"enter any number";
cin>>a[i];
}
cout<<"enter a number to be searched";
cin>>n;
while(l<=u)
{
mid=((l+u)/2);
if(n>a[mid])
l=mid+1;
else if (n<a[mid])
u=mid-1;
else
{
flag=1;

break;
}
}
if(flag==1)
cout<<"element found at"<<mid+1;
else
cout<<"not found";
getch();

Q.10. Write a program in C++ to implement Sum of


matrices in 2-D arrays.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[3][3],b[3][3],c[3][3]; cout<<"enter
the element of first matrix"; for
(i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"enter second matrix";
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cin>>b[i][j];
}
}
for (i=0;i<3;i++)

{
for (j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"addition is \n";
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
getch();
}

Q.11. Write a program in C++ to implement transpose of


a matrix.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[3][3];
cout<<"enter the element of first matrix";
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cin>>a[i][j];

}
}
cout<<"transpose of a matrix is=\n";
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cout<<a[j][i];
}
cout<<"\n";
}
getch();
}

Q.12. Write a program in C++ to implement a dynamically


allocated stack containing names of countries.
#include<conio.h>
#include<iostream.h>
#include<string.h>
struct node
{
char country[20];
struct node *next;
} *strt,*p,*q; void
push()
{
p=new node;

cout<<"\nEnter the country";


char a[20];
cin>>a; strcpy(p>country,a); p>next=NULL;
if(strt==NULL)
{
strt=p;
}
else
{
p->next=strt;
strt=p;
}
}
void disp()
{
if(strt==NULL)
{
cout<<"\nNo Stack created" ;
}
else
{
q=strt; while(q>next!=NULL)
{
cout<<"\n "<<q->country;

q=q->next;
}
cout<<"\n "<<q->country;
}
}
void pop()
{
if(strt==NULL)
{
cout<<"NO Stack created" ;
}
else
{
cout<<"\nValue Poped is "<<strt->country;
q=strt;
strt=strt->next;
delete q;
}
}
void main()
{
clrscr();
strt=NULL; int
i;
for(i=0;i<5;i++)
push(); pop();

pop();
pop();
disp();
getch();
}

Q.13. Write a program in C++ to implement a


dynamically allocated queue containing names of cities.
#include<conio.h>
#include<iostream.h>
#include<string.h>
struct node
{
char city[20]; struct
node *next; }
*front,*p,*q,*rear;
void insert()
{
p=new node;
cout<<"\nEnter the city";
char a[20];
cin>>a; strcpy(p>city,a); p>next=NULL;
if(front==NULL&&rear==NULL)
{
front=p;
rear=p;
}

else
{
rear->next=p;
rear=p;
}
}
void disp()
{
if(front==NULL)
{
cout<<"\nNo Queue created" ;
}
else
{
q=front; while(q>next!=NULL)
{
cout<<" \n"<<q->city;
q=q->next;
}
cout<<" \n"<<q->city;
}
}
void deleteq()
{
if(front==NULL&&rear ==NULL)
{

cout<<"NO Queue created" ;


}
else if(rear==front)
{
rear=NULL;
}
else
{
cout<<"\nValue Poped is "<<front->city;
q=front;
front=front->next;
delete q;
}
}
void main()
{
clrscr();
front=NULL;
rear=NULL; int
i;
for(i=0;i<5;i++)
insert();
for(i=0;i<3;i++)
deleteq();
cout<<endl;
disp(); getch();

Q.14. Write a program in C++ to implement the concept


of multilevel inheritance.
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
class A
{
int a;
public:
void getdata()
{
cout<<"enter the value of a";
cin>>a;
}
void display()
{
cout<<"a="<<a;
}
};
class B:public A
{
int b;
public:
void getdata1()
{
getdata();

cout<<"enter the value of b";


cin>>b;
}
void display1()
{
display();
cout<<"b="<<b;
}
};
class C:public B
{
int c;
public:
void getdata2()
{
getdata1();
cout<<"enter the value of c";
cin>>c;
}
void display2()
{
display1();
cout<<"c="<<c;
}
};
void main()
{

clrscr();

C obj;
obj.getdata2();
obj.display2();
getch();
}

Q.15. Write a program in C++ to implement the concept


of multiple inheritance.
#include <iostream.h>
#include <conio.h>
class Triangle
{
public:
int b,h;
float area1;
void getdata1()
{
cout<<"enter the value of b,h\n";
cin>>b>>h;
area1=0.5*b*h;
}
};
class circle
{
public:
int r;
float area2;
void getdata2()
{
cout<<"\nenter the value of r\n";
cin>>r;
area2=3.14*r*r;

}};
class C: public Triangle,public circle
{
public:
void display1()
{
cout<<"\narea of triangle= "<<area1<<endl;
}
void display2()
{
cout<<"\narea of circle= "<<area2<<endl;
}};
void main()
{
clrscr();
C obj1,obj;
obj1.getdata1();
obj1.display1();
obj.getdata2();
obj.display2();
getch();
}

Q.16. Write a program in C++ to implement the concept


of single level inheritance.
#include<iostream.h>
#include<conio.h>
class A
{
int a;
public:
void getdata()
{
cout<<"enter a no";
cin>>a;
}
void display()
{
cout<<"a=";
}
};
class B:public A
{
int b;
public:
void getdata1()
{
getdata();
cout<<"enter value for b";

cin>>b;
}
void display1()
{
display();
cout<<"b="<<b;
}
};
void main()
{
clrscr();
B obj;
obj.getdata1();
obj.display1();
getch();
}

Q.17. Write a program in C++ to count the total numbers


of characters present in a text file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
int count=0;
char ch[20];
fstream f1("D:\\ABC.txt",ios::in|ios::out);
f1<<"Hi, Welcome to file handling program";
f1.seekg(0,ios::beg);
f1>>ch;
while(!f1.eof())
{
count++;
f1>>ch;
}
f1.close();
cout<<"total characters"<<"\n"<<count;
getch();
}

Q.18. Write a program in C++ to count the total numbers


of alphabet A present in a text file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
fstream f1("ABC.txt",ios::in|ios::out); f1>>"HI,WELCOME
TO DATA FILE HANDLING PROGRAM"; int count=0;

char ch;
while(!f1.eof())
{
f1>>ch;
if(ch=='A')
{
count++;
}
}
cout<<"Total number of alphabet A is"<<\t<<count;
getch();

Q.20. Write a program in C++ to count the total number


of blank spaces present in a text file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
fstream f1("ABC.txt",ios::in|ios::out); //f1>>"HI,WELCOME
TO DATA FILE HANDLING PROGRAM "; int count=0;

char ch[20];
while(!f1.eof())
{
f1>>ch;
count++;
}
cout<<"Total number of blank spaces is "<<count-1;
f1.close();
getch();
}

Q.21. Write a program in C++ to count the total number


of vowels present in a text file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
int count=0;
char ch;
fstream f1("D:\\ABC.txt",ios::in|ios::out);
f1<<"Do you know file handling program?";
f1.seekg(0,ios::beg);
f1>>ch;
while(!f1.eof())
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
count++;
f1>>ch;
}
f1.close();
cout<<"total number of vowels"<<"\n"<<count;
getch();
}

Q.22. Write a program in C++ to count the total numbers


of question mark present in a text.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
int count=0;
char ch;
fstream f1("D:\\ABC.txt",ios::in|ios::out);
f1<<"Do you know file handling program?";
f1.seekg(0,ios::beg);
f1>>ch;
while(!f1.eof())
{
if(ch=='?')
count++;
f1>>ch;
}
f1.close();
cout<<"total question marks"<<"\n"<<count;
getch();
}

Q.23. Write a program in C++ to implement function


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

long add(long x,long y);


float add(float x,float y);
void main()
{
clrscr();
long n1,n2;
float f1,f2;
cout<<"Enter two numbers integers :\n";
cin>>n1>>n2;
cout<<"\nEnter two floating numbers :\n";
cin>>f1>>f2;
cout<<"\nSum of integers="<<add(n1,n2)<<endl;
cout<<"\nSum of floating numbers="<<add(f1,f2)<<endl;
getch();
}
long add(long x,long y)
{
long sum=0;
sum=x+y;
return sum;
}
float add(float x,float y)
{
float sum=0;
sum=x+y;
return sum;
}

Q.24. Write a program in C++ to swap value of two


variables by passing pointers.
#include<iostream.h>
#include<conio.h>
void swap(int *y, int *x);
void main()
{
clrscr();
int a,b;
cout<<"enter two numbers";
cin>>a>>b;
swap(&a,&b);

cout<<"\n value after swap"<<"\t"<<a<<"\t"<<b;


getch();
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

Q.25. Write a program to implement


constructor overloading.
#include<iostream.h>
#include<conio.h>
class ABC
{
int a,b;
public:
ABC()
{
a=50;
b=100;
}
ABC(int x, int y)
{
a=x;
b=y;
}
void display()
{
cout<<"\n values:"<<a<<"\t"<<b;
}
};
void main()
{
clrscr();

ABC obj (10,20);


ABC obj2;
obj.display();
obj2.display();
getch(); }

You might also like