Given a positive integers array find the average of the numbers in that array using for-each loop
This exercise contains a class named NumberAverage with the following methods:
+FindAverage(int[]) : String
- Should take positive integer array as input and return a String with the average calculated
- The average of the array should be calculated by using a for-each loop
- Should send error message if all value in array are not positive
- Should send error message if array is empty
class NumberAverage
{
public static string FindAverage(int[] massive)
{
int SumOfNumbersInArray = 0;
if(massive.Length == 0)
{
return "Error. Massive length must have at least 1 number";
}
foreach(int i in massive)
{
if (i < 0)
{
return "Error. Massive must have only positive numbers";
}
SumOfNumbersInArray += i;
}
return $"{SumOfNumbersInArray / massive.Length}";
}
}
Comments
Leave a comment