Write a program that inputs different values into an array and finds out the smallest value and largest value from it.
So let’s Write Code:
Let’s build logic First:
Algorithm for code:
- Step 1: Start
- Step 2: Declare an array and variables smallest, largest and i.
- Step 3: Take inputs from user.
- Step 4: Set Smallest and largest equal to arr[0].
- Step 5: Set condition and compare array values one by one.
- Step 6: Print values.
- Step 7: End Program
Code:
Write a program that inputs different values into an array and finds out the smallest value and largest value from it.
#include<iostream>
using namespace std;
main()
{
int ar[10], smallest, largest, i;
for(i=0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>ar[i];
}
smallest = largest = ar[0];
for(i=1; i<=9; i++)
{
if(smallest > ar[i])
smallest = ar[i];
if(largest < ar[i])
largest = ar[i];
}
cout<<"Largest value in array is : "<<largest<<endl;
cout<<"Smallest value in array is : "<<smallest<<endl;
}
Hope You will learn Something. Happy Coding.
Visit my YouTube Channel as Well๐ https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4
Click on the below Links for more Programming Exercises:
Conditional Structure Exercises: ๐https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/
Array Exercises: ๐https://myustaadg.com/category/programming-exercises/arrays-exercises/