Home > Back-end >  Why this program returns unintended output when debugging? [closed]
Why this program returns unintended output when debugging? [closed]

Time:10-09

As you can see my intended use for generating numbers is to store one plus current index yet my output is this weird:

0103050709

What it should be:

12345678910

using System;

namespace Learn
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = GenerateNumbers(10);
            PrintNumbers(numbers);

            Console.ReadKey();

        }

        static int[] GenerateNumbers(int length)
        {
            int[] numbers = new int[length];

            for (int i = 0; i < numbers.Length; i  )
            {
                i  ;
                numbers[i] = i;
            }
            return numbers;
        }

        static void PrintNumbers(int[] numbers)
        {
            foreach (int number in numbers)
            {
                Console.Write($"{number}");
            }
        }
    }
}

The other thing used is:

numbers[i] =   i;

and

i = i   1;
numbers[i] = i;

I've programmed in C before but I am a beginner in C# and I really don't see my fault here.

CodePudding user response:

it is because you have an extra i

in the start

[0,0,0,0,0,0,0,0,0,0]

i=1

[0,1,0,0,0,0,0,0,0,0]

i=3

[0,1,0,3,0,0,0,0,0,0]

i=5

[0,1,0,3,0,5,0,0,0,0]

and so on

  •  Tags:  
  • c#
  • Related