C++ program to demonstrate the use of constructors and destructors

This program demonstrates the concept of constructors and destructors in c++. A constructor is a funbction with the same name as that of its class. If no explicit constructor is defined the program will invoke the default constructor. Destructors are used to destroy the constructor, when they are no longer used. The program implements the constructor sample() and destructor ~sample().

Program


//program to implement constructors and destructors

#include<iostream.h>

#include<conio.h>

class sample

{

static int count,c1;

public:

sample()

{

count++;

cout<<"\nNumber of objects created="<<count;

}

~sample()

{

c1++;

cout<<"\nNumber of objects destroyed="<<c1;

}

};

int sample::count;

int sample::c1;

void main()

{

{

sample s;

getch();

{

sample s1;

getch();

}

getch();

}

getch();

}

C Program to implement queue

Description


This program implements the queue data structure. A queue is a data structure in which new items are entered to the rear position and items are removed from the front. Therefore a queue is also known as First in first out, which means that the items entered the first will be removed first. This program implements insertion, deletion and display of a queue using a switch case. 

Program


#include<stdio.h>

#include<conio.h>

# define max 3

void main()

{

int i,q[max],front=0,rear=-1,ch,item;

clrscr();

printf("\n1.Insert\n2.Delete\n3.Display\n4Exit\n\nEnter your choice");

scanf("%d",&ch);

while(ch!=4)

{

switch(ch)

{

case 1:

if(rear==(max-1))

{

printf("Queue Full");

break;

}

else

{

printf("Enter the item to be inserted");

scanf("%d",&item);

rear++;

q[rear]=item;

break;

}

case 2:

if(front>rear)

{

printf("Queue is empty!!!");

break;

}

else

{

item=q[front];

printf("Item deleted is %d",item);

front++;

break;

}

case 3:

if(front>rear)

{

printf("Queue is empty!!!");

break;

}

else

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

{

printf("%d ",q[i]);

}

}

printf("\n1.Insert\n2.Delete\n3.Display\n4Exit\n\nEnter your choice");

scanf("%d",&ch);

}

}

C program to demonstrate the use of structures

This program finds the total marks of a student by using structures.


Explanation of the program


An emp structure is created with the variables name[], m1, m2, m3 and total, then a structure array s[3] is declared which has the type emp. The for loop is used to get the name and marks of three students and it will be stored in s[i].name, s[i].m1, s[i].m2, s[i].m3 respectively. The total marks is calculated by the statement s[i].total=s[i].m1+s[i].m2+s[i].m3. After reading the data of three students the name and total marks of each student is printed.

Program


#include<stdio.h>

#include<conio.h>

struct emp

{

char name[10];

int m1,m2,m3,total;

}s[3];

void main()

{

clrscr();

int i;

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

{

printf("\nEnter the name marks 1, 2 & 3 of student %d",i+1);

scanf("%s%d%d%d",&s[i].name,&s[i].m1,&s[i].m2,&s[i].m3);

s[i].total=s[i].m1+s[i].m2+s[i].m3;

}

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

{

printf("\nTotal mark of %s is %d\n",s[i].name, s[i].total);

}

getch();

}


Output





C program using structure to calculate the total marks of a student
C program using structure to calculate total marks

c program to print the pattern

Description


This program prints the pattern shown below. The number of  's' at each side is read as input from the user, then the program uses a nested for loop to print the pattern.

ssssssss
s        s
s        s
s        s
sssssss


Program


// square pattern c program.

//program for pattern printing using for loop. 

#include<stdio.h>

#include<conio.h>

void main()

{

int n;                          /*pattern s */

clrscr();

printf("Enter the value for n");

scanf("%d",&n);

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

{

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

{

if(i>0 && i<(n-1))

if(j==0 | j==(n-1))

printf("s");

else

printf(" ");

else

printf("s");

}

printf("\n");

}

getch();

}


Output


Pattern printing in C
C program to print pattern

c program to calculate the salary of an employee

Explanation of the program



This program calculates the salary of an employee using switch case. The basic pay is read into the variable bp. If bp greater than Rs. 10000 then ta is 5 % of bp, da is 3% of bp and tax is 2% of bp, if bp greater than Rs. 20000 then ta is 7 % of bp, da is 5% of bp and tax is 3% of bp, if bp greater than Rs. 30000 then ta is 9 % of bp, da is 7% of bp and tax is 4% of bp, otherwise  ta is 9 % of bp, da is 7% of bp and tax is 4% of bp. Here bp is divided by 10000 and the result is stored in bp1, it is then given as input to the switch case, to find the appropriate values of ta, da and tax.

After exiting from the switch case,  the total salary is calculated by using the equation bp+(ta+da)-tax and it is stored in the variable tot.

Program



#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int bp1;

float bp,ta,da,tax,tot;

printf("Enter the basic pay");

scanf("%f",&bp);

bp1=bp/10000;

switch(bp1)

{

case 1:ta=bp*5/100;

    da=bp*3/100;

    tax=bp*2/100;

    break;

case 2:ta=bp*7/100;

    da=bp*5/100;

    tax=bp*3/100;

    break;

case 3:ta=bp*9/100;

    da=bp*7/100;

    tax=bp*4/100;

    break;

default:ta=bp*10/100;

    da=bp*9/100;

    tax=bp*5/100;

    break;

}

tot=bp+(ta+da)-tax;

printf("ta=%f \nda=%f \ntax=%f",ta,da,tax);

printf("\n\nTotal is %f",tot);

getch();

}


Output



C program to calculate employee salary using switch case
C program to calculate the salary of an employee


c program to find the roots of a quadratic equation

Description


This program finds the roots of a quadratic equation. The roots are dependent on the discriminant. The program uses variable r to store this value. If the value of the discriminant is greater than 0, then there are two roots. If the value of the discriminant is equal to 0, then there is only one root. Otherwise the roots are imaginary. The program uses if else if statement to check for this conditions and prints the roots.

Program



#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

clrscr();

float a,b,c,r,r1,r2;

printf("enter the value for a,b&c");

scanf("%f%f%f",&a,&b,&c);

r=(b*b)-(4*a*c);

if (r>0)

{

r1=-b+sqrt(r)/(2*a);

r2=-b-sqrt(r)/(2*a);

printf("Roots are%f & %f",r1,r2);

}

else if(r==0)

{

r1=-b/(2*a);

printf("Root is%f",r1);

}

else

{

printf("Roots are imaginary");

}

getch();

}

C program to implement binary search

What is binary search?


As the name implies binary search divides the array into two equal parts and proceeds only with one of these parts, which may contain the element that is searched. Binary search requires that the array contains elements, either in ascending order or in descending order.

Explanation of the program


The program reads the size of the array and stores it in the variable n. Next the elements are read into the array a[] in ascending order. Then the element to be searched is read into the variable i. The variable l and u stores the lower bound and upper bound values for the array. Initially lower bound is 0 and upper bound is n. These values change each time as the array size is reduced. 


(l+u)/2 gives the middle index of the array, it is stored in the variable m. Next the value stored in this location is compared with the element to be searched. If it equals, the element is found and we closes the loop. If i is less than  a[m], the elements after a[m] is dropped from the array otherwise elements before a[m] is dropped from the array. The search is proceeded with the remaining part of the array until the element is found or all elements are searched. 


Example


Suppose the array size is 5 and contains the following elements.

10   14   17   20   23

We need to search for the element 14

Here,

l=0 (Lower bound) & u=5 (Upper bound)
m=(l+u)/2=2 (value after decimal is omitted)

a[m]= a[2]= 17
14 is less than 17
so we now consider only the elements before 17, ie 10 & 14.


Now, u=0 and l=2
m=(l+u)/2=1
a[m]= a[1]= 14
14 equals 14, so we have found the element.

Program


#include<stdio.h>

#include<conio.h>

void main()

{

int a[25],n,m,l,u,f=0,i,j;

printf("Enter the size of the array: ");

scanf("%d",&n);

printf("Enter the elements in sorted order: ");

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

{

scanf("%d",&a[j]);

}

printf("Enter the item to be searched for: ");

scanf("%d",&i);

l=0;

u=n;

while(l<=u)

{

m=(l+u)/2;

if(a[m]==i)

{

printf("\n\nItem found at position %d",m+1);

f=1;

break;

}

else if(a[m]>i)

{

u=m-1;

}

else

{

l=m+1;

}}

if(f==0)

printf("\n\nItem not found in the array");

getch();

}


Output


C program to search using binary search
C program for binary search


Friend function C++ program

Description


This program demonstrates friend function in c++. A friend function is a non member function of a class, which can access all the private and protected members of the class. A friend function is preceded by the keyword friend. The program implements a friend function add(), which is defined outside the class second, but still access the private variables a, b and c.

Program


//Friend function c++

#include<iostream.h>

#include<conio.h>

class second

{

int a,b,c;

public:

void getdata(int p,int q)

{

a=p;b=q;

}

friend int add(second s);

};

int add(second s)

{

s.c=s.a+s.b;

return s.c;

}

void main()

{

int d;

second s;

s.getdata(10,5);

d=add(s);

cout<<"Sum= "<<d;

getch();

}


C++ program to implement function overloading

Description


This program implements function overloading in c++. Function overloading can be used for implementing polymorphism. Function overloading refers to the use of multiple functions with the same name but with different parameters in the same class. This program two functions add(), one without parameters and other with two parameters. The function call s.add(); calls the first function and the function call s.add(p,q); calls the second function.

Program


//function overloading

#include<iostream.h>

#include<conio.h>

class sample

{

int a,b,c;

public:

void add()

{

a=10;

b=5;

c=a+b;

cout<<"\nc= "<<c;

}

void add(int x,int y)

{

a=x;

b=y;

c=a+b;

cout<<"\nC= "<<c;

}

};

void main()

{

int ch,p,q;

sample s;

cout<<"\n1.Add without parameter\n2.Add with parameter\n3.Exit";

cout<<"\nEnter your choice: ";

cin>>ch;

while(ch!=3)

{

if(ch==1)

s.add();

else

{

cout<<"\nEnter the 2 values";

cin>>p>>q;

s.add(p,q);

}

cout<<"\n1.Add without parameter\n2.Add with parameter\n3.Exit";

cout<<"\nEnter your choice: ";

cin>>ch;

}

}

C program for polynomial addition using structure

Description


This program implements polynomial addition. A structure array [], b[] and c[] is declared. The number of coefficients of both polynomials are read to the variables n1 and n2 respectively.  Then the coefficients and exponents of both the polynomials are read one by one to the arrays a[] and b[] using a for loop. Then the program uses while loop to find the sum of these polynomials and stores it in the array c[]. Then the result of the addition is displayed to the output screen. 

Program


// polynomial addition using structure c program

#include<stdio.h>
#include<conio.h>
struct poly
{
int exp;
int coef;
}a[10],b[10],c[10];
void main()
{
int n1,n2,n3,i,j,k;
printf("\nEnter the number of coefficients of polynomial A");
scanf("%d",&n1);
printf("\nEnter the coefficient and exponents\n\n");
for(i=0;i<n1;i++)
scanf("%d%d",&a[i].coef,&a[i].exp);
printf("\nEnter the number of coefficients of polynomial B");
scanf("%d",&n2);
printf("\nEnter the coefficientds and exponents\n\n");
for(i=0;i<n2;i++)
scanf("%d%d",&b[i].coef,&b[i].exp);
i=0;j=0;k=0;
while((i<n1)&(j<n2))
{
if(a[i].exp==b[j].exp)
{
c[k].exp=a[i].exp+b[j].exp;
c[k].coef =a[i].coef;
i++;
j++;
k++;
}
else if(a[i].exp>b[j].exp)
{
c[k].exp=a[i].exp;
c[k].coef=a[i].coef;
i++;
k++;
}
else
{
c[k].exp=b[j].exp;
c[k].coef=b[j].coef;
k++;
j++;
}
}
while(i<n1)
{
c[k].exp=a[i].exp;
c[k].coef=a[i].coef;
i++;
k++;
}
while(j<n2)
{
c[k].exp=b[j].exp;
c[k].coef=b[j].coef;
k++;
j++;
}
i=0;
printf("\nSum Poly...\n");
while(i<k)
{
printf("%dX^%d",c[i].coef,c[i].exp);
i++;
}
getch();
}

C program for sparse matrix representation

Description


This program implements sparse matrix representation. A sparse matrix is a matrix in which the majority of its elements are 0. In terms of computer storage, storing a sparse matrix as itself is a wastage of space. The representation of a sparse matrix discards all 0 elements and only represents the non zero elements with their position parameters.


Program


#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,i,j,a[10][10],s[10][10],k;
printf("Enter the row and colum value of the matrix");
scanf("%d%d",&r,&c);
printf("Enter the elements to the matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
k=1;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]!=0)
{
s[k][0]=i;
s[k][1]=j;
s[k][2]=a[i][j];
k++;
}}}
s[0][0]=r;
s[0][1]=c;
s[0][2]=k-1;
for(i=0;i<k;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf(" %d",s[i][j]);
}}
getch();
}