An array can store more than one element in it. In programming you may need find out the largest and the smallest integer or float element in it. We are going to do that using C++ programming language.
Let’s write the program
The output of above program
Let’s write the program
#include<iostream>
using namespace std;
main()
{
int a[10],i,n,big,small;
cout<<"how many element you want to enter : ";
cin>>n;
cout<<"Enter Your Value in Array\n";
for(i=0;i<n;i++)
{
cout<<"Enter Your Value ";
cin>>a[i];
}
big=a[0];
for(i=0;i<n;i++)
{
if(a[i]>=big)
{
big=a[i];
}
}
small=a[0];
for(i=0;i<n;i++)
{
if(a[i]<=small)
{
small=a[i];
}
}
cout<<"Largest One is "<<big<<"\n";
cout<<"Smallest One is "<<small;
}
In above program first we set a value that considered as first biggest or first smallest integer value, then we check if there any other element that is bigger or smaller than stored value and if found then just replace it and continue the test. When control check that all element in array is compared then it shows the final result.using namespace std;
main()
{
int a[10],i,n,big,small;
cout<<"how many element you want to enter : ";
cin>>n;
cout<<"Enter Your Value in Array\n";
for(i=0;i<n;i++)
{
cout<<"Enter Your Value ";
cin>>a[i];
}
big=a[0];
for(i=0;i<n;i++)
{
if(a[i]>=big)
{
big=a[i];
}
}
small=a[0];
for(i=0;i<n;i++)
{
if(a[i]<=small)
{
small=a[i];
}
}
cout<<"Largest One is "<<big<<"\n";
cout<<"Smallest One is "<<small;
}
The output of above program
how many element you want to enter : 5
Enter Your Value in Array
Enter Your Value 22
Enter Your Value 54
Enter Your Value 31
Enter Your Value 91
Enter Your Value 77
Largest One is 91
Smallest One is 22
Enter Your Value in Array
Enter Your Value 22
Enter Your Value 54
Enter Your Value 31
Enter Your Value 91
Enter Your Value 77
Largest One is 91
Smallest One is 22

0 comments:
Post a Comment