Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Im trying to fill an array of size n(user input) with positive integers in numerical order e.g n = 10, 1,2,3,4,5,6,7,8,9,10. However ive been looking at whats wrong with this for - loop and i cant for the life of me figure out what needs to be put in there to stop the index outside of the array exception being thrown. ( I am fairly new to C#) Here is my code, any help would be greatly appreciated.

C#
 "main"
            int b = 0;
            int[] a = new int[b];
            Console.WriteLine("Please enter a positive integer:");
            b = Convert.ToInt32(Console.ReadLine());

            numArray(a, b);

public static void numArray(int[] arr, int c)
        {

            
            int[] a = arr;

            for (int i = 1; i < c; i++)
            {
                a[i] = a[c];
                Console.WriteLine(a[i]);
            }
        }
Posted

C#
int b = 0;
int[] a = new int[b];

- b is 0
- a is initialized to be sized 0
- thus when you try to access it with an indext starting from 1, you are underindexing the empty array...

Btw: what's the use of this: int[] a = arr;? Nothing...

It is not clear what numArr would do, but you don't need to pass size, as the array is an object, which defines it's length. So if you want simply to list the elements, this would do it:
C#
public static void numArray(int[] arr)
{
  for (int i = 0; i < arr.Length; i++)
  {
      Console.WriteLine(arr[i]);
  }
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 15-Feb-15 16:17pm    
5ed.
—SA
Keep in mind that C# arrays are zero-based (that is, the first index number is 0). So if you enter 10 as array size, the last index number of the array is 9. Now c is 10, so a[c] gives that error. Also, because the array is zero-based, your loop should start with 0. That being said, if you want to fill the array with numbers from 1 to 10, you shouldn't set a[i] to a[c]; set it to i + 1 instead.
C#
public static void numArray(int[] arr, int c)
{
    int[] a = arr;
 
    for (int i = 0; i < c; i++)
    {
        a[i] = i + 1;
        Console.WriteLine(a[i]);
    }
}
 
Share this answer
 
v2
Comments
Bawwla 15-Feb-15 15:36pm    
Having tried so many things there was some random "useless" bits of code left in places. However thank you alot for this reply ans its fixed my code, and i learnt in the process.
Thanks :)
Thomas Daniels 15-Feb-15 15:40pm    
You're welcome!
Sergey Alexandrovich Kryukov 15-Feb-15 16:16pm    
Programming is not done by "trying". You need to understand every line you write and exclude any guesswork.
—SA
Bawwla 16-Feb-15 13:56pm    
Well you never learn how things work without trying first?
Sergey Alexandrovich Kryukov 16-Feb-15 14:06pm    
I did not mean trying during learning. Always learn on a separate study project, prototype, etc.

And even during learning, trying, being fine, is not the best approach. Figuring out what should be written theoretically, or based on documentation, before writing, is generally more efficient, even if at first it takes more time. Ultimately, you need to understand each line you write. Of course, at first you can put it wrong, that would be fine. I'm talking about finding a solution by trial-and-error and, once it works, leaving it as is, without complete understanding. This is a usual mistake though, but one of the worst.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900