-3

I am learning C#. I know how to do this in C++ but i get an exception to this line of code :

v[i] = int.Parse(Console.ReadLine()); 

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

 int[] v = new int[n];

 for (int i = 1; i <= n; i++)
 {
     v[i] = int.Parse(Console.ReadLine());
 }

I want to translate this from C++ to C#:

int v[1001], n;
cin>>n;
for (int i = 1; i <= n; i++)
{
    cin>>v[i];
}

There is more code but i think it's irelevant for this problem.

  • 4
    The array has `n` items. The first is at index `0`. The second is at index `1`. The nth is at index `n-1`. You need to loop from `0` to `n - 1`. That C++ code looks a bit sketchy as well, though I'm not very current in that language. – 15ee8f99-57ff-4f92-890c-b56153 Jun 03 '19 at 20:16
  • `int[] v = Enumerable.Range(0, n).Select(x => int.Parse(Console.ReadLine())).ToArray();` – Rufus L Jun 03 '19 at 20:18
  • 1
    C++ really has one based array indexes? Should be zero based there too, shouldn't it? – sticky bit Jun 03 '19 at 20:19
  • 1
    Just to make it clear: your C++ code is wrong in the sense that it has undefined behavior: https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior –  Jun 03 '19 at 20:22
  • Do not ever use `Parse` for user input! Use `TryParse`, and handle the error condition. – Eric Lippert Jun 03 '19 at 20:37
  • More generally, your approach is flawed. Don't do a line-for-line translation. Understand the meaning of the code, and then write code in the new language that has that meaning. Then you'll write idiomatic code, instead of replicating the flaws of code written in one language into another language. – Eric Lippert Jun 05 '19 at 00:18

1 Answers1

-2

Just to add the code which will work, alongside Christos' explanation.

Solution: Now you're also starting the loop at 0 which will not result in an exception and you won't skip the first element.
Note that here you have < not <=. That's because the last element in the array has an index of n-1.

int[] v = new int[n];

for (int i = 0; i < n; i++)
{
    v[i] = int.Parse(Console.ReadLine());
}

As already mentionend, for a detailed explanation/comparison(between c++ and c#) on this see Christos' answer.

Joelius
  • 3,839
  • 1
  • 16
  • 36
  • Maybe someone didn't like your Solution 1 I guess. Consider removing that approach in case you want. – Sandy Jun 03 '19 at 21:18
  • I even added "(better)" to the second approach since the beginning so people wouldn't complain.. – Joelius Jun 03 '19 at 21:31
  • I see that...but if they saw that then I am not sure why they down voted. But basically, the first approach should go away....that is insanely bad and should not be even part of proposed solutions. – Sandy Jun 03 '19 at 23:00
  • Okay I've removed the first solution. I just want to justify why I put it in in the first place. OP said: "There is more code.." because of that I assumed that maybe the for loop is much bigger and i is used in several otherplaces where it has to be from 1. Now if you have 50 uses for i from 1 and a single use for i from 0, in my opinion it's not "insanely bad" to make the for-loop go from one. But I totally agree that this situation is rare and 99.9% of the time you want to use the one I have now – Joelius Jun 04 '19 at 07:25