+ 1
Can someone explain/help me to undestand why the output is Element[0] = 100 Element[1] = 101 Element[2] = 102 Etc..
using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; for ( int i = 0; i < 10; i++ ) { n[i] = i + 100; } foreach (int j in n ) { int i = j-100; Console.WriteLine("Element[{0}] = {1}", i, j); } Console.ReadKey(); } } }
1 Answer
+ 1
well in the loop you initialize every array to be 100 + their index. then, in the loop, you simply display each element.
example for 0:
int i = 100 - 100;
Console.WriteLine("Element[{0}] = {1}", i //i is 0, j //j is 100);