Error traversing vector

0

The user pastes the values that will be saved into the vector and then displays those values. However the following error is occurring:

  An unhandled exception of type 'System.IndexOutOfRangeException' occurred in vetoor.exe ".

My code looks like this:

namespace vetoor 
{
class Program
{
    static void Main(string[] args)
    {
        int[] vet = new int[3];
        int i,a;


        for (i = 0; i < vet.Length;  i++)
        {
            Console.WriteLine("Digite o numero");
            vet[i] = int.Parse(Console.ReadLine());

        }
        for(a=0; a<4; a++)
        {
            Console.WriteLine("{0}", vet[i]);
        }

        Console.ReadKey();
    }
}

}

    
asked by anonymous 04.01.2017 / 22:01

3 answers

3

You have created a vector with 3 elements and even started well on the first loop going until reaching its size, although in this very specific case does not have to do this. But then it was up to 3. The vector goes from 0 to 2. Changing the second condition to 3 will solve. Or always use Length to avoid this problem. Maybe the ideal would be to use foreach instead of for , but I think you have not learned it yet.

But this code can be more organized and modernized:

using static System.Console;

namespace vetor {
    public class Program {
        public static void Main(string[] args) {
            int[] vet = new int[3];
            for (int i = 0; i < vet.Length; i++) {
                WriteLine("Digite o numero: ");
                vet[i] = int.Parse(ReadLine());
            }
            for (int a = 0; a < vet.Length; a++){
                WriteLine("{0}", vet[a]);
            }

        }
    }
}

See running on dotNetFiddle and on CodingGround .

Note that there is still a potential problem. When you ask someone to enter a value and have no control over it, you can not use Parse() because typing can not be converted will generate an error. you'll have to use TryParse() .

    
04.01.2017 / 22:11
2

The second for iterates over its vector up to index 3.

When you access the vet index [a] when a is equal to 3, this exception is thrown. This means that the vector vector does not have index 3.

This may be confusing at first, but although you have declared the vector vet with 3 positions, the index starts from 0. That is, the vet vector has only the indexes 0, 1, and 2.

    
04.01.2017 / 22:10
0

My answer is based on error only .. I do not know if you had other doubts or not .. you also have another alternative to a for .. foreach that goes through a list / array

       namespace vetoor 
     {
    class Program
      {
    static void Main(string[] args)
{
    int[] vet = new int[3];
    int i,a;


    for (i = 0; i <= vet.Length;  i++)
    {
        Console.WriteLine("Digite o numero");
        vet[i] = int.Parse(Console.ReadLine());

    }
   // for(a=0; a<4; a++) -> a = 3 e a = 4 iria estar fora do vector
   // {
   //     Console.WriteLine("{0}", vet[i]);
   // }
   foreach(int num in vet){
    Console.WriteLine(num);
   }

    Console.ReadKey();
   }
 }
    
09.01.2017 / 11:23