Write a program that inputs 10 numbers into an array and finds out the second largest value from that array.
So let’s Write Code:
Let’s build logic First:
Algorithm for code:
- Step 1: Start
- Step 2: Declare an array and variables largest, sec_largest, and i.
- Step 3: Take Inputs form user.
- Step 4: Set largest and sec_largest = arr[0].
- Step 5: Compare all array elements one by one and store second largest number in sec_largest.
- Step 6: Print sec_largest
- Step 7: End Program
Code:
Write a program that inputs 10 numbers into an array and finds out the second largest value from that array.
#include<iostream>
using namespace std;
main()
{
int ar[10], largest, sec_largest, i;
for(i=0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>ar[i];
}
sec_largest = largest = ar[0];
for(i = 1; i<=9; i++)
{
if(largest < ar[i])
largest = ar[i];
if(sec_largest < ar[i] && largest > ar[i])
sec_largest = ar[i];
}
cout<<"Second Largest value in array is : "<<sec_largest<<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/