Pages

Showing posts with label ARRAY. Show all posts
Showing posts with label ARRAY. Show all posts

Monday, 21 March 2011

PROGRAM TO CONVERT DECIMAL NUMBER TO BINARY

DESCRIPTION-:This program receive input as decimal number and then calculate its binary equivalent and then display the binary equivalent of that decimal number.
FOR EXAMPLE-:If we have input decimal number------
                                          23
Then it generate output as-----------------
                                       10111
                           
PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=0,j,d,a[10];
cout<<"enter the decimal number";
cin>>d;
while(d>0)
{
a[i]=d%2;
d=d/2;
i++;
}
cout<<"its binary equivalent is";
for(j=i-1;j>=0;j--)
{
cout<<a[j];
}
getch();
}

Saturday, 19 March 2011

PROGRAM TO CALCULATE THE SUM OF EACH ROW AND EACH COLUMN AND TOTAL OF ALL ELEMENTS OF MATRIX

DESCRIPTION-: This program take matrix as input and again generate matrix as output in which sum of each row elements is placed in corrosponding row and also sum of each column elements is placed in corrosponding column and also calculate the total of all elements.
FOR EXAMPLE-:
If we have input matrix as-----

              2   3
              1   5

Then it generate output as--------

              2   3   5
              1   5   6
              3   8   11

PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],m,n,i,j;
cout<<"enter the row and column of matrix";
cin>>m>>n;
cout<<"enter elements of array"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<m;i++)
{
a[i][n]=0;
for(j=0;j<n;j++)
{
a[i][n]=a[i][n]+a[i][j];
}
}
for(j=0;j<n;j++)
{
a[m][j]=0;
for(i=0;i<m;i++)
{
a[m][j]=a[m][j]+a[i][j];
}
}
a[m][n]=0;
for(i=0;i<m;i++)
{
a[m][n]=a[m][n]+a[i][n];
}
cout<<"\n";
for(i=0;i<m+1;i++)
{
cout<<"\n";
for(j=0;j<n+1;j++)
{
cout<<a[i][j]<<"\t";
}
}
getch();
}

PROGRAM TO FIND THE TRANSPOSE OF GIVEN MATRIX

DESCRIPTION-: This program takes matrix as input and then calculate the transpose of input matrix and after calculating transpose of matrix it generate output matrix.
FOR EXAMPLE-: If we have input matrix as--------

                                1   3   6
                                5   9   2
                                4   1   7

Then program generate output as---------

                                1   5   4
                                3   9   1
                                6   2   7


PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,m,n,a[10][10],b[10][10];
cout<<"enter the number of rows and column of matrix"<<"\n";
cin>>m>>n;
cout<<"enter the value of elements of matrix";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"entered matrix is"<<"\n";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
}
cout<<"\n transpose of matrix is"<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
b[i][j]=a[j][i];
}
}
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<m;j++)
{
cout<<b[i][j]<<"\t";
}
}
getch();
}

PROGRAM TO MULTIPLY TWO MATRICES

DESCRIPTION-: This program takes two matrices as input then calculate the multiplication of input  matrices and then generate the output matrix.The output matrix contains the multiplication of input matrices.
FOR EXAMPLE-:

If we have two input matrices as---------

              2   1                                       3   1   2
              1   3                                       1   2   3
              3   1
Then program generate output as--------
                      7   4   7
                      6   7   11
                     10  5   9

PROGRAM-:
#include<iostream.h> 
#include<conio.h>
void main()
{
clrscr();
int i,j,m,n,p,q,a[10][10],b[10][10],c[10][10];
cout<<"enter row and column of matrix A";
cin>>m>>n;
cout<<"\n enter the row and column of matrix B";
cin>>p>>q;
if(n!=p)
{
cout<<"multiplication not possible";
}
else
{
cout<<"enter value of elements of matrix A"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"enter values of elements of matrix B"<<"\n";

for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cin>>b[i][j];
}
}
cout<<"values of elements of matrix A are"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t";
cout<<a[i][j];
}
cout<<"\n";
}
cout<<"values of elements of matrix B are"<<"\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cout<<"\t";
cout<<b[i][j];
}
cout<<"\n";
}
cout<<"multiplication  of two matrix is:";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<q;j++)
{
c[i][j]=0;
for(int k=0;k<p;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
cout<<c[i][j]<<"\t";
}
 }
}
getch();
}

PROGRAM TO SUBTRACT TWO MATRIX

DESCRIPTION- This program takes two matrices as input and then generate the output matrix after subtracting the two input matrices.The order of input matrix must be same.
FOR EXAMPLE-: If we have two input  matrices as---------

             2   3   1                                 2   2   1
             1   5   7                                 1   4   6
             2   8   1                                 1   3   1

Then program generate output as---------
                                  0   1   0
                                  0   1   1
                                  1   5   0

PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,a[10][10],b[10][10],c[10][10];
cout<<"enter the order of matrix A";
cin>>n;
cout<<"enter value of elements of matrix A"<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the order of matrix B";
cin>>n;
cout<<"enter values of elements of matrix B"<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
cout<<"values of elements of matrix A are"<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t";
cout<<a[i][j];
}
cout<<"\n";
}
cout<<"values of elements of matrix B are"<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t";
cout<<b[i][j];
}
cout<<"\n";
}
cout<<"subtraction of matrix A and B is"<<"\n";
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]-b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}

PROGRAM TO ADD TWO MATRIX

DESCRIPTION-:This program takes two matrices as input and then generate the output matrix which contain the addition of two input matrices.The order of two input matrices must be same.
FOR EXAMPLE-:  If we have two input matrices as-------
               2   6   1                           2   1   5
               3   1   3                           7   2   1
               1   0   5                           1   7   0


Then program generate output as-------
                                     4   7   6

                                    10  3   4
                                     2   7   5

PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,a[10][10],b[10][10],c[10][10];
cout<<"enter the order of matrix A";
cin>>n;
cout<<"enter value of elements of matrix A"<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the order of matrix B";
cin>>n;
cout<<"enter values of elements of matrix B"<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
cout<<"values of elements of matrix A are"<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t";
cout<<a[i][j];
}
cout<<"\n";
}
cout<<"values of elements of matrix B are"<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t";
cout<<b[i][j];
}
cout<<"\n";
}
cout<<"addition of matrix A and B is"<<"\n";
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}

PROGRAM TO DISPLAY THE ELEMENT OF 2-D ARRAY

DESCRIPTION-:This program display the elements of two dimensional array.means that it display elements of array in the form of matrix.
In this program we must specifies the number of rows and columns in the array.

PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,m,n,a[100][100];
cout<<"enter number of rows and column in array"<<"\n";
cin>>m>>n;
cout<<"enter value of elements of array"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"values of 2-D array element is"<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t";
cout<<a[i][j];
}
cout<<"\n";
}
getch();
}

PROGRAM TO FIND SUM OF ELEMENTS OF ARRAY USING FUNCTION

DESCRIPTION-: This program calculate the sum of all the elements of an array with the help of function.
In this program firstly we entered the value of elements of an array and after this the program calculate the sum of elements of array and display the sum at last.
FOR EXAMPLE-: If we have elements of array as-----
                                  12   4   7   1   7   3

Then program generate output as-------
                                   sum=34

PROGRAM-:
#include<iostream.h>
#include<conio.h>
void sum(int a[],int n);
void main()
{
clrscr();
int n,a[10];
cout<<"enter the number of elements in array";
cin>>n;
cout<<"enter value of elements of array"<<"\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"entered value of array element is"<<"\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
sum(a,n);
getch();
}
void sum(int a[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
cout<<"sum of elements ="<<sum;
}

PROGRAM TO SORT THE ELEMENTS IN ARRAY IN ASCENDING ORDER USING FUNCTION

DESCRIPTION-: This program arrange the elements of an array in ascending order.In this firstly we enter the elements of an array and after entering the elements of array this program sort the elements of an array in ascending order.
FOR EXAMPLE-:

If we have elements of an array as-------------
                    5    2    12    45    99    9
Then program generate the output as----------
                    2    5    9    12    45    99


PROGRAM-:
#include<iostream.h>
#include<conio.h>
void sort(int a[],int n);
void main()
{
clrscr();
int n,a[10];
cout<<"enter number of elements in array";
cin>>n;
cout<<"\n enter the value of elements of array";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter value of elements of array are"<<"\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
cout<<"elements in ascending order are"<<"\n";
sort(a,n);
getch();
}
void sort(int x[],int y)
{

for(int i=0;i<y;i++)
{
for(int j=0;j<y;j++)
{
if(x[i]<x[j])
{
int temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
for(i=0;i<y;i++)
{
cout<<x[i]<<"\n";
}
}

PROGRAM TO SORT THE ELEMENTS IN ARRAY IN DESACENDING ORDER

DESCRIPTION-: This program arrange the elements of an array in descending order.In this firstly we enter the elements of an array and after entering the elements of array this program sort the elements of an array in descending order.
FOR EXAMPLE-:


If we have elements of an array as-------------

                    5    2    12    45    99    9

Then program generate the output as----------

                    99    45    12    9    5    2



PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a[10],temp;
cout<<"enter number of elements in array";
cin>>n;
cout<<"\n enter the value of elements of array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter value of elements of array are"<<"\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
cout<<"elements in ascending order are"<<"\n";
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
getch();
}

PROGRAM TO SORT THE ELEMENTS IN ARRAY IN ASCENDING ORDER

DESCRIPTION-: This program arrange the elements of an array in ascending order.In this firstly we enter the elements of an array and after entering the elements of array this program sort the elements of an array in ascending order.
FOR EXAMPLE-:


If we have elements of an array as-------------

                    5    2    12    45    99    9

Then program generate the output as----------

                    2    5    9    12    45    99




PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a[10],temp;
cout<<"enter number of elements in array";
cin>>n;
cout<<"\n enter the value of elements of array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter value of elements of array are"<<"\n";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
cout<<"elements in ascending order are"<<"\n";
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
getch();
}

PROGRAM TO FIND GREATEST NUMBER IN ARRAY

DESCRIPTION-: This program calculate the greatest number in the given array.In this we firstly enter the elements of an array and after entering the elements of an array the program finds the greatest element in array and produces output contain the largest number in the array.
FOR EXAMPLE-: If we enter elements of array as--------
                                2    34    1    67    99    45    33             

Then program produces output as---------
                          gretest number in array is  99

PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a[10];
cout<<"enter number of elements in array";
cin>>n;
cout<<"enter the value of array elements"<<"\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n value of elements of array are";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
int max=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
cout<<"largest number is"<<max;
getch();
}

PROGRAM TO CALCULATE AVERAGE OF 'n' ELEMENTS USING ARRAY

DESCRIPTION-: This program calculate the average of all the elements of an input array.this program firstly calculate the sum of the elements of array and after calculating the sum it calculate the average of elements of array by dividing sum to the number of elements in an array.
FOR EXAMPLE-: If we have elements of array as-------
                      2    4    22    54    8    1

Then it generates output as------
               average=15.16
        
PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,sum=0,a[20];
float avg;
cout<<"enter number of elements in array";
cin>>n;
cout<<"enter value of elements of array"<<"\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"sum of elements of array are"<<"\n";
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
cout<<"sum ="<<sum<<"\n";
avg=sum/n;
cout<<"average ="<<avg;
getch();
}

PROGRAM TO PRINT ELEMENTS OF ARRAY

PROGRAM-:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,a[20],i;
cout<<"enter the number of element in array"<<"\n";
cin>>n;
cout<<"enter the elements of array"<<"\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"entered elements of array are";
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
getch();
}
 
Copyright (c) 2010 Concepts Of C++. Design by WPThemes Expert

Blogger Templates and RegistryBooster.