I want to write a program which takes an array of size N
, loop over it and sum all the numbers before i
(including i
itself). I haven't done a lot so far because I don't know how to access the indexs before i
. When I try for example arr[i - 1]
, it throws me an error that the index was outside the array bounds.
int N = int.Parse(Console.ReadLine());
int[] arr = new int[N];
// Input
for (int i = 0; i < arr.Length; i++)
arr[i] = int.Parse(Console.ReadLine());
// Here we need to sum
for (int i = 0; i < arr.Length; i++)
//
Example (N = 4)
Before = [1, 2, 3, 4]
After = [1, 3, 6, 10]