Pages

Saturday, 19 March 2011

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();
}
 
Copyright (c) 2010 Concepts Of C++. Design by WPThemes Expert

Blogger Templates and RegistryBooster.