- Course
- Functions B
- Search the greatest value in an array
Search the greatest value in an array
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a program in C# that implements a function called SearchGreatestValue that receives as an parameter an array of 5 real numbers requested from the user. You must obtain and display the largest of those numbers.
Input
5
4
5
6
7
Output
7
Solution
using System;
class FunctionSearchMaxValue
{
static void Main(string[] args)
{
int total = 5;
float[] data = new float[total];
for (int i = 0; i < total; i++)
{
data[i] = Convert.ToSingle(Console.ReadLine());
}
float max = Maximum(data);
Console.WriteLine(max);
}
public static float Maximum(float[] data)
{
float max = -99999999.00f;
for (int i = 0; i < data.Length; i++)
{
if (i == 0)
{
max = data[i];
}
else
{
max = max < data[i] ? data[i] : max;
}
}
return max;
}
}