I need to iterate through an Array and find the sum of the odd, and the even positioned values.
Here is what I have, but I'm getting the error:
Cannot apply indexing with [] to an expression of type 'Array'
static void SumOfOddsAndEvens(Array arr)
{
int i = 0;
bool isEven = true;
int evenSum = 0;
int oddSum = 0;
while (i < arr.Length)
{
Console.WriteLine(arr[i]);
if (isEven) evenSum += arr[i]; // Cannot apply indexing with [] to an expression of type 'Array'
if (!isEven) oddSum += arr[i];
isEven = !isEven;
i++;
}
Console.WriteLine("Sum of even slots: " + evenSum);
Console.WriteLine("Sum of odd slots: " + oddSum);
}
Function is being called here:
static void Main(string[] args)
{
SumOfOddsAndEvens(new int[] { 1, 2, 3, 4, 6, 7, 8 });
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…