0

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#.

4
  • "What I'm seeing is that it's putting the first number in the line into the array and moving on." No, it's putting everything into the array - but then you're only printing one number before moving on. How would you expect Console.WriteLine(numbers[i]); to print multiple numbers? Commented Jun 23, 2020 at 9:44
  • because I'm assuming that it's running the loop 9 times.. one for each number in the array? is it adding the numbers to the array in bulk? Commented Jun 23, 2020 at 9:46
  • But it's running the whole of the loop 9 times - so it's reading 9 lines from the console, and printing one number from each of those lines. It sounds like that's not what you wanted. I suggest you step through the code in a debugger, looking carefully at the values at each step. Commented Jun 23, 2020 at 9:48
  • Try to run the program and enter Banana instead of a number. See what's going on there. Commented Jun 23, 2020 at 12:23

2 Answers 2

1

Let's go step by step :

  1. You enter 3 so n = 3 and the code in the for loop will run 3 * 3 = 9 times.
  2. You enter 11 9 1 so string[] numbers = { "11", "9", "1" };.
  3. Then it will print a blank line.
  4. Since 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
  1. You enter 14 90 232, so string numbers = { "14", "90", "232" };.
  2. Blank line
  3. Now it's the second time we're going through the loop and 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
  1. You enter 111 15 111, so string numbers = { "111", "15", "111" };.
  2. Blank line
  3. Now it's the third time we're going through the loop and 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.'

enter image description here

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));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.