Hello, I’m working on this task:
Use a method namedGetNumbersFromConsole which will read from the console a count of int values and return all those values in an int array. Then, output to the console, the average of the array values. (Average is a sum of all array elements divided by their length value).
Example:
>4
>1
>2
>3
>4
2.5
Here is my code:
namespace PrimitiveTypes
{
class ArrayAverage
{
public static void Main(string[] args)
{
int[] myArray = GetNumbersFromConsole();
var sum=0;
for(int i=0; i<=myArray.Length; i++)
{
sum=sum+i;
double sumDouble=(double)sum;
if (i>=myArray.Length)
Console.WriteLine(sumDouble/myArray.Length);
}
}
static int[] GetNumbersFromConsole()
{
int count = int.Parse(Console.ReadLine());
int[] result = new int[count];
for(int i = 0; i < count; ++i)
{
result[i] = int.Parse(Console.ReadLine());
}
return result;
}
}
}
Can anyone please help me to solve it?