Showing posts with label Cpp. Show all posts
Showing posts with label Cpp. Show all posts

C++ program to find the sum of elements in a matrix

This C++ program finds the sum of all elements in a matrix using pointer. The row and column values of the matrix are read into the variables m and n. Then the program uses for loop to read the elements to the matrix and at the same time the address of the first element of each row is read into the pointer array p[]. Then the program uses nested for loop to calculate the sum of elements row by row and stores it in the variable s. Then the sum is printed to the output screen.


Program


#include<iostream.h>
#include<conio.h>
void main()
{
          int a[10][10],*p[10],m,n,i,j,s=0;
          cout<<"Enter the order of matrics: ";
          cin>>m>>n;
          cout<<"\nEnter the elements\n\n";
          for(i=0;i<m;i++)
          {
                   p[i]=&a[i][0];
                   for(j=0;j<n;j++)
                   {
                             cin>>a[i][j];
                   }
          }
          for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                             s=s+*(p[i]+j);
                   }
          }
          cout<<"\nSum of elements: ";
          cout<<s;   

          getch();
}

C++ program to sort n strings

 This C++ program sorts n strings using pointer. The value for n is read as input and the strings are read into the 2D character array a[][]. A pointer array p[] is declared to store the address of each element in the array a[][]. Then the program uses nested for loop to sort the strings. Here strcmp() function is used to compare each strings and strcpy() function is used to copy the strings from one location to another.After sorting is completed, the program displays the sorted strings to the output screen.

Program


#include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

{

          char a[10][20],*p[10],temp[20];

          int n,i,j;

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

          cin>>n;

          cout<<"\nEnter the strings:\n\n";

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

          {

                   cin>>a[i];

                   p[i]=a[i];

          }

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

                   {

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



                             {

                                      if(strcmp(p[j],p[j+1])>0)

                                                {

                                                          strcpy(temp,p[j]);

                                                          strcpy(p[j],p[j+1]);

                                                          strcpy(p[j+1],temp);

                                                }

                             }

                    }

                   cout<<"Strings after sorting: \n";

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

                   {

                             cout<<"\n"<<p[i];

                   }

          getch();

}

C++ program to sort an array using bubble sort

This program implements bubble sort in C++.  Bubble sort iterates through the array by considering each pair of elements and swapping them if it is not in the sorted order. This procedure repeats until the entire array is sorted. The program reads the limit of the array into the variable n. The elements are read one by one into the array a[]. Then the program uses a nested for loop to perform bubble sort. After sorting is completed the new array is displayed to the output screen.


Program


// bubble sorting in c
#include<iostream.h>
#include<conio.h>
void main()
{
          int a[10],i,n,j,temp,*p;
          clrscr();
          cout<<"Enter the limit: ";
          cin>>n;
          cout<<"\nEnter the "<<n<<" array elements\n\n";
          for(i=0;i<n;i++)
          {
                   cin>>a[i];
          }
          p=a;
          for(i=0;i<n;i++)
          {
                   for(j=0;j<n-1-i;j++)
                   {
                             if(*(p+j)>*(p+j+1))
                             {
                                      temp=*(p+j);
                                      *(p+j)=*(p+j+1);
                                      *(p+j+1)=temp;
                             }
                   }
          }
          cout<<"\nsorted array is as follows\n\n";
          for(i=0;i<n;i++)
          {
                   cout<<a[i]<<" ";
          }
          getch();
}



bubble sort in c++
C++ program for bubble sort












Related post: Bubble sort in c

C++ program to demonstrate binary search

This program implements binary search in C++.  Binary search requires that the array be sorted in either ascending or descending order. The algorithm then compares the middle element in the array with the item to be searched, if a match is found its position is returned. Otherwise if the item to be searched is less than the middle element, the algorithm proceeds with the elements which are on the left side of the middle element, or if the item to be searched is greater than the middle element, the algorithm proceeds with the elements which comes after the middle element.  



Program



#include<iostream.h>
#include<conio.h>
void main()
{
          clrscr();
          int a[50],*p,n,ele,i,beg=0,end,mid;
          cout<<"Enter the no.of elements:";
          cin>>n;
          cout<<"\nEnter the elements in ascending order:";
          for(i=0;i<n;i++)
          {
                   cin>>a[i];
          }
          cout<<"\nEnter the element to be searched:";
          cin>>ele;
          beg=0,end=n-1;
          while(beg<=end)
          {
                   mid=(beg+end)/2;
                   p=&a[mid];
                   if(*p==ele)break;
                   else if(ele>*p) beg=mid+1;
                   else end=mid-1;
          }
          if(beg<=end)
          cout<<"\nThe element "<<ele<<" found at position "<<mid+1;
          else
          cout<<"\nnot found";
          getch();
}



Binary search program in C++
Binary Search in C++

C++ program to find the even and odd elements from a matrix

 This C++ program finds the even and odd elements in a matrix. The program uses two arrays even[] and odd[] to store the even and odd elements respectively.  The row and column values of the matrix are read into the variables m and n.  Then the program uses a nested for loop to read the elements one by one into the array a[][], at the same time the address of the first element of each row is stored in the pointer array p[]. Next the program uses modulo operator in the for loop to check each element and stores the even elements in the array even[] and odd elements in the array odd[].


Program


// Matrix even and odd elements c++ program
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],*p[10],m,n,i,j,k=0,l=0,even[20],odd[20];
cout<<"Enter the order of matrics: ";
cin>>m>>n;
cout<<"\n\nEnter the elements\n\n";
for(i=0;i<m;i++)
{
p[i]=&a[i][0];
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(*(p[i]+j)%2==0)
{
even[k]=*(p[i]+j);
k++;
}
else
{
odd[l]=*(p[i]+j);
l++;
}
}
}      
cout<<"\nEven elements\n\n";
for(i=0;i<k;i++)
cout<<even[i]<<" ";
cout<<"\n\nOdd elements\n\n";
for(i=0;i<l;i++)
cout<<odd[i]<<" ";getch();
}


C++ program to find the transpose of a matrix and check whether it is symmetric or not

This C++ program finds the transpose of a matrix and checks whether it is symmetric or not. A matrix is symmetric if  both the matrix and its transpose are equal. The program reads the row and column values of the matrix into the variable m and n. Then the elements are read one by one into the 2D array a[][] using a nested for loop and at the same time the address of the first element of each row is stored to the pointer array p[]. Then the transpose of the matrix a[][] is found and stored in the array b[][]. Next the elements of the arrays a[][] and b[][] are compared to find any dissimilarity. If there is any dissimilarity, the matrix is not symmetric, otherwise the matrix is symmetric.  

Program


#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[10][10],b[10][10],f=0,*p1[10],*p2[10],m,n,r,c,i,j;
cout<<"Enter the order of matrics: ";
cin>>m>>n;
cout<<"\n\nEnter the elements\n\n";
for(i=0;i<m;i++)
{
p1[i]=&a[i][0];
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
r=m;r
c=n;
for(i=0;i<c;i++)
{
p2[i]=&b[i][0];
for(j=0;j<r;j++)
{
b[i][j]=*(p1[j]+i);
}
}
cout<<"\n\nTranspose Matrics\n\n";
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
cout<<b[i][j]<<"\t";
}
cout<<"\n";        
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if((*(*(a+i)+j))!=(*(*(b+i)+j)))
{
f=1;
break;
}
}
}
if(f==0)
{
cout<<"\n\nThe matrix is symmetric";
}
else
cout<<"\n\nThe matrix is not symmetric";
getch();
}


Transpose of a sparse matrix and check whether it is symmetric in c++
Transpose of a matrix in c++















C++ program to find the sum of diagonal elements of a matrix

This C++ program finds the sum of the diagonal elements of a matrix. The row and column values of the matrix are read into the variables m and n. Then the elements are read one by one into the 2D array a[][] using a nested for loop, at the same time the address of the first element in each row is stored to the pointer array p[]. Then the program uses for loop to find the sum of the first and second diagonal elements and the result is displayed to the output screen. 

Program


//matrix diagonal elements sum c++
#include<iostream.h>
#include<conio.h>
void main()
{
          int a[10][10],*p[10];
          int m,n,i,j,s1=0,s2=0,t;
          cout<<"Enter the order of matrics: ";
          cin>>m>>n;
          cout<<"Enter the elements\n\n";
          for(i=0;i<m;i++)
          {
                   p[i]=&a[i][0];
                   for(j=0;j<n;j++)
                   {
                             cin>>a[i][j];
                   }
          }
          cout<<"\n\nFirst diagonal elements are\n\n";
          for(i=0;i<n;i++)
          {
                   cout<<*(p[i]+i)<<"\t";
                   s1=s1+*(p[i]+i);
          }
          cout<<"\n\nSecond diagonal elements are\n\n";
          t=n;
          for(i=0;i<n;i++)
          {
                   cout<<*(p[t-1]+i)<<"\t";
                   s2=s2+*(p[t-1]+i);
                   t--;
          }
          cout<<"\n\nSum of first diagonal elements is: ";
          cout<<s1;
          cout<<"\n\nSum of second diagonal elements is: ";
          cout<<s2;
          getch();

}


 

C++ program to find the largest and smallest element in a matrix

This C++ program finds the largest and smallest element in a matrix using pointers. The row and column values of the matrix are read into the variables m and n. Then the program uses nested for loop to read the values to the 2D array a[][] and also the address of the first element of each row is stored in the pointer array p[]. The variables l and s is initially set with the value at a[0][0], these variables are used to store the largest and smallest element. Then the program uses another nested for loop to check in each row for the largest and smallest element and stores it in the variables l and s. Then the value of l and s is displayed to the output screen.


Program


#include<iostream.h>
#include<conio.h>
void main()
{
          int a[10][10],*p[10],m,n,i,j,s,l;
          cout<<"Enter the order of matrics: ";
          cin>>m>>n;
          cout<<"Enter the elements\n\n";
          for(i=0;i<m;i++)
          {
                   p[i]=&a[i][0];
                   for(j=0;j<n;j++)
                   {
                             cin>>a[i][j];
                   }
          }
          l=a[0][0];
          s=a[0][0];
          for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                             if(*(p[i]+j)>l)
                             l=*(p[i]+j);
                             if(*(p[i]+j)<s)
                             s=*(p[i]+j);
                   }
          }
          cout<<"\nLargest element is: ";
          cout<<l;
          cout<<"\nSmallest element is: ";
          cout<<s;
          getch();
}

C++ program to sort elements in a matrix

This C++ program sorts the elements of a 2D matrix row by row using pointer. The row and column values of the matrix are read into the variables m and n. Then the program uses nested for loop to read the elements to the matrix a[][] using pointers, also each elements are stored into the pointer array p[]. The elements in the pointer array is sorted and the new array is written to the original 2D array a[][] overwriting the existing elements. Then the sorted 2D array is displayed to the output screen.


Program


#include<iostream.h>
#include<conio.h>
void main()
{
          int a[20][20],i,j,m,n,*p[10],temp,b[20],s,*p1;
          p1=b;
          clrscr();
          cout<<"\n\nEnter the order of the matrix: ";
          cin>>m>>n;
          s=0;
          cout<<"\nEnter the elements:";
          for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                             cin>>*(*(a+i)+j);
                             p[i]=(*(a+i)+j);
                             p1[s]=*(*(a+i)+j);
                             s++;
                   }
          }
          for(i=0;i<s;i++)
          {
                   for(j=i+1;j<s;j++)
                   {
                             if(p1[i]>p1[j])
                             {
                                      temp=p1[i];
                                      p1[i]=p1[j];
                                      p1[j]=temp;
                             }
                   }
          }
          s=0;
          for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                             *(*(a+i)+j)=p1[s];
                             s++;
                   }
          }
          cout<<"\nMatrics after sorting:";
          for(i=0;i<m;i++)
          {
                   cout<<"\n\n";
                   for(j=0;j<n;j++)
                   {
                             cout<<*(*(a+i)+j)<<"\t";
                   }
          }
          getch();
}

C++ program to implement linear search

This C++ program implements linear search. In this method all the elements in the list will be checked in a sequential order, to check for a match with the element to be searched. If a match is found the iteration will be stopped and the element will be returned with its position. The program reads the size of the array in the variable n, then the elements are read into the array a[] by using a for loop. Next the element to be searched is read into the variable elem. Then the program uses a for loop to check all the elements in the array with the value of elem to find a match. 

 

Program


#include<iostream.h>
#include<conio.h>
void main()
{
          int a[50],f=0,elem,*p,i,n;
          cout<<"Enter the size of the array: ";
          cin>>n;
          p=a;
          cout<<"Enter the elements: ";
          for(i=0;i<n;i++)
          {
                   cin>>a[i];
          }
          cout<<"Enter the element to be searched: ";
          cin>>elem;
          for(i=0;i<n;i++)
          {
                   if(*p==elem)
                             {
                             cout<<"\nItem Found at position "<<i+1;
                             f=1;
                             break;
                             }
                   p++;
          }
          if(f==0)
          cout<<"\nItem not found";
          getch();
}


C++ program for linear search
Linear search in C++

C++ program to delete an element from an array

This C++ program deletes an element from an array. The size of the array is read as input into the variable n. Then the elements are read into the array a[] using a for loop.  Then the position of the element to be deleted is read into the variable pos. The elements which lies after the element to be deleted is shifted one position backward, overwriting the element to be deleted. Then the new array is displayed to the output screen.


Program


//Delete element from array c++ program

#include<iostream.h>
#include<conio.h>
void main()
{
          int a[50],n,i,*p,pos;
          cout<<"Enter the value for n: ";
          cin>>n;
          cout<<"\nEnter the elements\n";
          for(i=0;i<n;i++)
          {
                   cin>>a[i];
          }
          cout<<"\nEnter the position of element to delete: ";
          cin>>pos;
          p=&a[pos];

          for(i=pos;i<n;i++)
          {
                   *(p)=*(p+1);
                   p++;
          }
          p=a;
          cout<<"\nArray after deletion\n\n";
          for(i=0;i<n-1;i++)
          {
                   cout<<*(p+i)<<"\t";
          }
          getch();
}



Delete an element from an array C++ progarm
C++ - delete an element from an array

C++ program to insert an element to an array

This c++ program inserts an element into an array in a given position. The variable n reads the limit of the array. Then the numbers are read one by one into the array a[]. Next the element to be inserted and the position of insertion is read to the variables elem and pos. Then the program uses a for loop to make space of the new element in the array and the new element is inserted into the given position and the new array is displayed to the output screen.   


Program


#include<iostream.h>
#include<conio.h>
void main()
{
          int a[50],n,i,*p,pos,elem;
          cout<<"Enter the value for n: ";
          cin>>n;
          cout<<"\nEnter the elements\n";
          for(i=0;i<n;i++)
          {
                   cin>>a[i];
          }
          cout<<"\nEnter the element and position to insert: ";
          cin>>elem>>pos;
          p=&a[n];
          for(i=n;i>pos;i--)
          {
                   *(p)=*(p-1);
                   p--;
          }
          a[pos]=elem;
          p=a;
          cout<<"\nArray after insertion\n\n";
          for(i=0;i<=n;i++)
          {
                   cout<<*(p+i)<<"\t";
          }
          getch();
}


Insert an element to an array in c++
C++: Insert an element into an array

C++ program to swap 2 arrays

This C++ program swaps two integer arrays using pointers. Two arrays a[] and b[] are declared which stores the two input arrays. The pointers p1 and p2 is initially set to point to the first index of both the arrays a[] and b[]. Then the program uses a for loop to swap values in the two arrays. After swapping is complected the new arrays are displayed to the output screen.


Program



//program to swap array's using c++

#include<iostream.h>
#include<conio.h>
void main()
{
int *p1,*p2,a[50],b[50],n1,n2,i=0,t;
cout<<"Enter the size of first array: ";
cin>>n1;
p1=a;
cout<<"Enter the elements:\n";
for(i=0;i<n1;i++)
{

cin>>a[i];
}
cout<<"Enter the size of second array: ";
cin>>n2;
p2=b;
cout<<"Enter the elements:\n";
for(i=0;i<n2;i++)
{
cin>>b[i];
}
for(i=0;i<n1|i<n2;i++)
{
t=*p1;
*p1=*p2;
*p2=t;
p1++;
p2++;
}
p1=a;
p2=b;
cout<<"Arrays after swapping\n";
cout<<"Array A\n";
for(i=0;i<n2;i++)
{
cout<<*p1<<" ";
p1++;
}
cout<<"Array B\n";
for(i=0;i<n1;i++)
{
cout<<*p2<<" ";
p2++;
}
getch();
}

Output



program in c++ to swap arrays
C++ program to swap two arrays

C++ Program to find the cube of a number using void pointer

This program finds the cube of a number in C++. The number is read as input and is stored is the variable n. The program uses a void pointer which points to n.  The statement int *pint= (int *)p; stores the value at p to the pointer variable pint. Then the program repeatedly multiples pint 3 times which gives the cube of the given number.



Program



#include<iostream.h>



#include<conio.h>



void main()



{



          int n;



          void *p;



          p=&n;



          cout<<"Enter the number";



          cin>>n;



          int *pint= (int *)p;



          n=(*pint)*(*pint)*(*pint);



          cout<<"Cube of the number is\n";



          cout<<n;



          getch();



}


C++ program to swap 2d character array

This program swaps two 2d character arrays.  The limit of the two arrays are read to the variables n1 and n2. Then the program reads the strings one by one to both arrays. After reading the strings the program swaps the two arrays using a for loop and a while loop. The program uses strcpy() function to copy the string from one location to other.

Program



// Program to swap two dimensional character arrays
#include<iostream.h> #include<conio.h> #include<string.h> void main() { char a[5][10],b[5][10],t[10]; int i,j,n1,n2; cout<<"Enter the value for n1: "; cin>>n1; cout<<"Enter the value for n2: " ; cin>>n2; cout<<"Enter the strings to first array"; for(i=0;i<n1;i++) cin>>a[i]; cout<<"Enter the strings to second array"; for(i=0;i<n2;i++) cin>>b[i]; for(i=0;i<n1&&n2;i++) { strcpy(t,a[i]); strcpy(a[i],b[i]); strcpy(b[i],t); } if(i==n1) { while(i<n2) { strcpy(a[i],b[i]); i++; } } else { while(i<n1) { strcpy(b[i],a[i]); i++; } } cout<<"\nArrays after swapping\n"; cout<<"\nArray 1\n"; for(i=0;i<n2;i++) cout<<"\n"<<a[i]; cout<<"\nArray 2\n"; for(i=0;i<n1;i++) cout<<"\n"<<b[i]; getch(); }

C++ Program to demonstrate virtual function working

This program demonstrates virtual function in c++. When there exist two functions with same function name in both base and derived class the compiler identifies which function to use at run time based on the type of object pointed to by base pointer. The run time polymorphism in c++ is achieved through virtual functions.

Program


 //Run time polymorphism

#include<iostream.h>

#include<conio.h>

class base

{

public:

void display()

{

cout<<"\nDisplay Base";

}

virtual void show()

{

cout<<"\nShow Base";

}

};

class derived:public base

{

public:

void display()

{

cout<<"\nDisplay Derived";

}

void show()

{

cout<<"\nShow Derived";

}

};

void main()

{

base *bptr;

base b;

bptr=&b;

bptr->display();

bptr->show();

derived d;

bptr=&d;

bptr->display();

bptr->show();

getch();

}


Output



Display Base
Show Base
Display Base
Show Derived

C++ program to demonstrate unary operator overloading

This program demonstrate unary minus overloading in c. The value of x is read as input in the function getdata() and the putdata() function is used to display the value of x. The function void operator -() performs the overloading, which changes the sign of x to negative. In the main function, the statement a.getdata(); calls the function getdata() and the statement a.putdata(); calls the function putdata(). The statement -a; calls the function void operator -(), which overloads the unary minus operator. The statement a.putdata(); after the statement cout<<"\nAfter Overloading value of x="; prints the negative value of x.

Program

//program to overload unary operator -

#include<iostream.h>

#include<conio.h>

class overld

{

int x;

public:

void getdata()

{

cout<<"Enter the value for x";

cin>>x;

}

void putdata()

{

cout<<x;

}

void operator -()

{

x=-x;

}

};

void main()

{

overld a;

a.getdata();

cout<<"\nBefore Overloading value of x=";

a.putdata();

-a;

cout<<"\nAfter Overloading value of x=";

a.putdata();

getch();

}

C++ program to demonstrate file operations

This program demonstrates file operations in c++. The program reads as input five names and stores it in a file named sample.txt using the out statement, then the file is closed. Next the program reads a new name to be searched in the file and stores it in the variable name. Then the program program compares each name in the file with the value stored in the variable name, if there is a match the string is found in the file. Otherwise the string is not present in the file.


Program


//c++ program to check whether a particular word is present in the file

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<string.h>

#include<process.h>

void main()

{

int i;

char name[20],t[20];

ofstream out;

out.open("sample1.txt");

cout<<"Enter five names\n\n";

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

{

cin>>name;

out<<name<<"\n";

}

out.close();

cout<<"Enter the name to be searched for";

cin>>name;

ifstream in;

in.open("sample1.txt");

while(in)

{

in.seekg(2,ios::beg);

in>>t;

cout<<"\n"<<t;

}

in.close();

in.open("sample1.txt");

while(in)

{

in>>t;

if(strcmp(name,t)==0)

{

cout<<"\nString found!!!";

getch();

in.close();

exit(0);

}

}

cout<<"\nString not found!!!";

in.close();

getch();

}





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

}

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

}