0

Read multiple lines from console to arrays

Hey! The input is - n - number of pairs, and n lines after - with pairs of numbers, the code will be below. F.ex. with input : 3 1 2 3 4 5 6 Output will be: 1 2 0 1 2 1 1 2 2 I can't understand why "Pasrse" takes only the input of the line with (1 2) and saves it to all array elements. Or it's the way how indexes [0] and [1] in "Parse" work? Will be grateful for the explaination. static void Main(string[] args) { int i, n = int.Parse(Console.ReadLine()); string[] tokens = Console.ReadLine().Split(' '); int[] arr1 = new int[n]; int[] arr2 = new int[n]; while (Console.ReadLine() != "") { for (i = 0; i < n; i++) { arr1[i] = int.Parse(tokens[0]); arr2[i] = int.Parse(tokens[1]); } } for (i = 0; i < n; i++) { Console.WriteLine(arr1[i] + " " + arr2[i] + " " + i); }

14th Apr 2019, 8:14 PM
Š„ŠøŠŗŠ¼ŠµŃ‚ ŠØŠ°Ń„ŠøŠµŠ²
Š„ŠøŠŗŠ¼ŠµŃ‚ ŠØŠ°Ń„ŠøŠµŠ² - avatar
1 Answer
0
Fixed, should use Read.Line in the body of the loop to read every line, while loop is not necessary: static void Main(string[] args) { int i, n = int.Parse(Console.ReadLine()); int[] arr1 = new int[n]; int[] arr2 = new int[n]; for (i = 0; i < n; i++) { string[] tokens = Console.ReadLine().Split(' '); arr1[i] = int.Parse(tokens[0]); arr2[i] = int.Parse(tokens[1]); }
14th Apr 2019, 9:25 PM
Š„ŠøŠŗŠ¼ŠµŃ‚ ŠØŠ°Ń„ŠøŠµŠ²
Š„ŠøŠŗŠ¼ŠµŃ‚ ŠØŠ°Ń„ŠøŠµŠ² - avatar