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