Reputation: 5
I have the following very basic code:
static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < n*3; i++) { string[] numbers = Console.ReadLine().Split(); Console.WriteLine(); Console.WriteLine(numbers[i]); } }
It's supposed to take the following data:
3
11 9 1
14 90 232
111 15 111
It is taking the first number to determine the number of lines of data their are (there is a reason for this but outside of the scope of this question.
The loop should take line 2, 3 and 4 and populate the numbers array, splitting the data up so numbers[0] = 11, numbers[1] = 9, numbers[2] = 1... and so on.
What I'm seeing is that it's putting the first number in the line into the array and moving on. Here is a preview of what it's doing currently:
3
11 9 1
11
14 90 232
90
111 15 111
I was hoping the output would be:
3
11 9 1
11 9 1
14 90 232
14 90 232
111 15 111
111 15 111
I'm probably doing something completely stupid and blatantly obvious but I'm still trying to learn C#.
Upvotes: 0
Views: 4007
Reputation:
Let's go step by step :
n = 3
and the code in the for loop will run 3 * 3 = 9 times.11 9 1
so string[] numbers = { "11", "9", "1" };
.i = 0
now, it will print numbers[0]
which is "11"
(It will print 11
).The output is the following at this time:
3
11 9 1
11
14 90 232
, so string numbers = { "14", "90", "232" };
.i = 1
. So it will print numbers[1]
which is "90"
.The output is the following at this time:
3
11 9 1
11
14 90 232
90
111 15 111
, so string numbers = { "111", "15", "111" };
.i = 2
. So it will print numbers[2]
which is "111"
.The output is the following at this time:
3
11 9 1
11
14 90 232
90
111 15 111
111
You will encounter an error if you enter something like 1 2 3
which contains three numbers, because it will be the forth time we will be going through the loop and i = 3
and since numbers
contains three elements you will see the following:
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
I recommend string.Join() to get your expected result.
Here's the solutions:
static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < n*3; i++) { string[] numbers = Console.ReadLine().Split(); Console.WriteLine(); Console.WriteLine(string.Join(" ", numbers)); } }
Upvotes: 1
Reputation: 44
As i see, your numbers array is inside the loop, so every time the loop goes for an iteration, the array is reinitialized, so yes - your program does put all the numbers in the array but only prints the ith element, and even if it runs 9 times, it'll initialize the array 9 times (quite a big thing though). Below is a simpler and more easier for of your code.
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
string numbers[3];
// read n lines, one by one
for (int i = 0; i < n; i++)
{
numbers = Console.ReadLine().Split();
Console.WriteLine();
// print the 3 numbers, either using a smaller loop or simply indexing
// I use a loop here to make it more dynamic
for (int j = 0; j < 3; j++) {
Console.WriteLine(numbers[j] + " ");
}
}
}
though i do not exactly understand your expected output. Do you want to display the last printed line again? If yes, you can simple print the numbers array before reading the new numbers into it (make sure it is not empty, using a boolean flag or something).
Upvotes: 0