Problem with matrix boundaries

Asked

Viewed 19 times

0

Good afternoon! I’m doing vector exercises and found a problem where I don’t understand.

The proposed problem was to place values on a vector A and B of size 5, and then merge these values into a vector C of size 10. Desse jeito: A {1,2,3,4,5} B {6,7,8,9,10} C{1,2,3,4,5,6,7,8,9,10}

However it appears that the Dice extrapolates the limits of the matrix, even if I put 800 as read as the code below.

static void Main(string[] args)
{
    int[] vetorA = new int[5];
    int[] vetorB = new int[5];
    int[] vetorC = new int[10];

   //Colocando valores no vetorA
    for (int i = 0; i < 5; i++)
    {
        vetorA[i] = int.Parse(Console.ReadLine());
    }

    //Colocando valores no vetorB
    for(int i = 0; i < 5; i++)
    {
        vetorB[i] = int.Parse(Console.ReadLine());
    }

    //Colocando os valores de vetorA em vetorC
    for(int i = 0; i < 5; i++)
    {
        vetorC[i] = vetorA[i];
    }

    //Colocando os valores de vetorB juntamente com vetorA no vetorC
    for(int j = 0; j < 5; j++)
    {
        vetorC[j + 5] = vetorB[j];
    }

    //lendo e imprimindo os valores do vetorC completo
    for(int x = 0; x < 800; x++)
    {
        Console.WriteLine(vetorC[x]);
    }


    Console.ReadLine();

}
  • 1

    I don’t understand, you speak in matrix and only presents vectors?

  • @Leandroangelo, Visual Studio says that. I’m only working with vectors, but when I run, the sequence appears correctly, but on the line: for(int x = 0; x < 800; x++) the console says that it "extrapolated the boundaries of the matrix", and this is exactly the source of my doubt, because, first, where is the matrix ? and second, because you extrapolated that I put a limit of 800 when I only need 10?

  • I don’t think you understand... "I set an 800 limit when I only needed 10"... What happens when you open the 800th drawer of a closet that only has 10? The same thing that would happen when trying to open the 11th... it doesn’t exist! And it is these instructions that you are giving to your program.

  • @Leandroangelo I redid the program in another visual file, this time putting i < 10, (in the other program i < 10 was also giving error) and it worked. Thank you very much for your attention and explanation!

  • But it’s not just changing a number and working, you need to understand what you’re doing, look at the answer to the question that yours was marked as duplicate

  • @Leandroangelo I am new in programming and stack, thank you very much for having called me attention to reading where it was marked as duplicate, I was able to solve more exercises with understanding, which was weak, with the question of limits, I was confusing 0 with 1 vector and so I was wrong when it came to setting the limit.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.