-1

the noob, again. todays question is:

int[] forArray = new int[10];
        for (int k = 1; k <= 10; k++)
        {
            forArray[k] = k * 2;
            Console.WriteLine(k); // test
        }
        for (int k = 0; k < 10; k++)
        {
            Console.WriteLine(forArray[k]);
        }

it gives an "out of bounds" error. I would like my program to output natural numbers from 2 to 20. Instead gives off an error. when I change the first for loops condition to k <= 9 it runs but gives me 0 instead of 20. its like it returns the last value as 0 and "re-positions" it to the "front". sorry for the really simple question.

Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159
  • 2
    Look at your first for-loop. You are going from index 1 to index 10. Your array only goes to index 9. you need to traverse 0 to 9. – David Makogon Feb 07 '18 at 01:54
  • 2
    Please learn to use the debugger. It will let you figure out silly errors like this on your own, as well as how to use what is probably the most valuable tool in a coder's toolkit. It's never too early to learn how to use it. And write better titles - this question has nothing to do with what your title says. This is an *index out of bounds* question, and a simple search here would have shown you many existing questions with the same error and cause. – Ken White Feb 07 '18 at 02:06
  • sorry Ken. my problem is whenever I use "search" I always end up in a more advanced discussion. I will be honest, I didnt use it this time as I was in a hurry since ive already been snubbed from my other q&a sources for a half a day. nonetheless, sorry for the spam. – Complete_N00b Feb 07 '18 at 02:54

3 Answers3

1

Arrays are zero-based, when it comes to referencing the elements. So, for 10 items in an array (which is what you allocated), it's forArray[0] through forArray[9]. Your code tries to loop from forArray[1] through forArray[10], and there is no index position 10 (which is when you end up going out of bounds).

Your second for-loop is fine, as it goes from 0 to 9.

Note: Since your loop needs to be zero-based, you'll need to adjust how you calculate the number you stuff into the index positions, if you want it starting with 2.

David Makogon
  • 64,809
  • 21
  • 130
  • 175
  • I think I see it now. so forArray[k] = k * 2; isnt similar to something like {k}; rather, its "this index[1 to 9]", set the value 1 to 9 * 2; so I kind of need to modify it to (k+1)*2; maybe?? thank you very much Dave, you rock! – Complete_N00b Feb 07 '18 at 02:03
  • Yup you'd need something like `forArray[k] = (k+1)*2;` But just remember that you'll want to start `k` at 0, not 1. Otherwise you just have an orphaned item at the front of your array (which you then attempt to print in your second loop). – David Makogon Feb 07 '18 at 02:05
0

Debug your code and step through, as you see below you are trying to assign to index [10], which doesn't exist.

{
    int[] forArray = new int[10];

    for (int k = 1; k < 10; k++)  // k < 10 instead of k <= 10
    {
        forArray[k] = k * 2;
        Console.WriteLine(k); // test

            //forArray[0] = SKIPPED
            //forArray[1] = 2
            //forArray[2] = 4
            //forArray[3] = 6
            //forArray[4] = 8
            //forArray[5] = 10
            //forArray[6] = 12
            //forArray[7] = 14
            //forArray[8] = 16
            //forArray[9] = 18
            //forArray[10] INVALID
    }
    for (int k = 0; k < 10; k++)
    {
        Console.WriteLine(forArray[k]);
    }
}

Tested here

Brien Foss
  • 2,950
  • 3
  • 16
  • 29
  • thanks Brien! I understood forArray[k] = k * 2; as something like new int[10] {k}; . sololearn never really explain/elaborated on it lol – Complete_N00b Feb 07 '18 at 02:14
-1

Change your condition to k

forArray[k - 1] = k * 2;

tonedblue
  • 162
  • 1
  • 6
  • 3
    Or just properly index from 0 to 9. Subtracting an indexer in a for-loop seems like a code-smell to me. – David Makogon Feb 07 '18 at 01:58
  • thanks tonedblue. I went with: forArray[k] = (k+1) * 2; instead. At first I was thinking, maybe I need to use another variable lol thinking like a programmer is difficult – Complete_N00b Feb 07 '18 at 02:10