Write a program that inputs values in an array and finds out the largest value from the array using a function.
So let’s Write Code:
Let’s build logic First:
Algorithm for code:
- Step 1: Start
- Step 2: Declare an array and variables i, max.
- Step 3: Initialize i = 0;
- Step 4: Take inputs from user.
- Step 5: Traverse and Compare every element with current max element.
- Step 6: Print result.
- Step 7: Program end.
Code:
Write a program that inputs values in an array and finds out the largest value from the array using a function.
#include<iostream>
using namespace std;
main()
{
long arr[5], i;
long largest(long []), max;
i = 0;
while(i<=4)
{
cout<<"Enter value in element "<<i+1<<" of array ? ";
cin>>arr[i];
i++;
}
max = largest(arr);
cout<<"Largest value entered in the array : "<<max;
}
// definition of largest() function
long largest(long x[])
{
int mx = x[0];
for(int i = 1; i<=4; i++)
if (mx < x[i])
mx = x[i];
return mx;
}
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/