1. PROGRAM TO ENTER  THE ELEMENTS OF AN ARRAY & PRINT IT


#include<iostream>
using namespace std;
int main()
{
// initializing varibles
int a[10],i,lim;
// there shoud be a limit for the number of varibles
cout<<"enter the limit\n";
cin>>lim;
cout<<"enter the array elements\n";
// LOOP FOR ENTERING THE ARRAY ELEMENTS
for(i=0;i<lim;i++)
{
cout<<"enter the"<<i+1<<"st element";
cin>>a[i];
}
// same as entering . printing the array elements
cout<<" the array elements are\n";
for(i=0;i<lim;i++)
{
cout<<a[i];
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,small,large,i;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
small=Arr[0];
large=Arr[0];
for(i=1;i<n;i++)
{
if(Arr[i]<small)
small=Arr[i];
if(Arr[i]>large)
large=Arr[i];
}
cout<<"\nLargest element is :"<<large;
cout<<"\nSmallest element is :"<<small;
return 0;
}



2.PROGRAM TO FIND THE SUM OF THE ENTERED ELEMENT OF AN ARRAY

#include<iostream>
using namespace std;
int main()
{
// initializing varibles
int a[10],i,lim,sum=0;
// there shoud be a limit for the number of varibles
cout<<"enter the limit\n";
cin>>lim;
cout<<"enter the array elements\n";
for(i=0;i<lim;i++)
{
cout<<"enter the"<<i+1<<"st element";
cin>>a[i];
// here the sum is calculated
sum=sum+a[i];
}
cout<<" the array elements are\n";
for(i=0;i<lim;i++)
{
cout<<a[i];
}
cout<<" the sum of elements are"<<sum;
return 0;
}

similarly from the above programs we can find the(+,-,*,/) of array elements

3. PROGRAM TO SEARCH FOR THE ARRAY ELEMENTS


#include<iostream>
using namespace std;
int main()
{
// HERE num is the number to be searched  and pos is the position of the element
whether it belongs to a[0] or a[1] or a[3]...... up to the limit
int a[20],x,num,y=0,pos;
cout<<" enter the number of elements in the array\n\a";
cin>>x;
cout<<" enter the elements of the string\n\a";
for(int i=0;i<x;i++)
{
cin>>a[i];
}
cout<<" enter the number to be searched\n\a";
cin>>num;
// u an use any loops here i have used for loop
int i=0;
while(i<x) //here x is the limit
{
if(a[i]==num)
{
  y=1;// if the a[element] == number then  y is given the value 1 and the position of the 
                     varible is passed  to  pos
  pos=i;
  break;
}i++;
}
if(y==1)
{
cout<<" the element is found in"<<pos+1<<"position\n\a";
}
else
{
cout<<" the element is not found";

}
}

4.  PROGRAM TO FIND THE GREATEST ELEMENT IN AN ARRAY



#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,small,large,i;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
        // just like finding the greatest & smallest number... same in an array but it comes with loop
        note small=arr[0];large=arr[0]; should be initilized before the loop
small=Arr[0];
large=Arr[0];
for(i=1;i<n;i++)
{ // conditions
if(Arr[i]<small)
small=Arr[i];
if(Arr[i]>large)
large=Arr[i];
}
cout<<"\nLargest element is :"<<large;
cout<<"\nSmallest element is :"<<small;
return 0;
}

5.program to find the sum of the elements of  a matrix


#include<iostream>
using namespace std;
int main()
{
int a[15][15],r,c,i,j,sum=0;
cout<<" enter the number of rows and columns\n";
cin>>r>>c;//reading how many rows &columns are there
for(i=0;i<r;i++)// loop for entering the elements of the row
{
cout<<" enter"<<i+1<<"st row :";
for(j=0;j<c;j++)
{
cout<<j+1<<"st column";
cin>>a[i][j];
sum=sum+a[i][j];//  finding the sum of the elemennts
} }
cout<<"printing array elements\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j];cout<<"\t";
}
cout<<"\n";
}
cout<<"the sum of the elements are"<<sum;
return 0;}






















0 comments:

Post a Comment