Intercalation of two arrays (vectors) in C#

Asked

Viewed 255 times

0

My code has two arrays of 5 elements, a array has odd numbers while the other has even numbers and is needed intersperse, I created a third vector to do this, but the result is not expected.

The entrance:

Digite o 0° número do vetor 1: 1
Digite o 1° número do vetor 1: 3
Digite o 2° número do vetor 1: 5
Digite o 3° número do vetor 1: 7
Digite o 4° número do vetor 1: 9

Digite o 0° número do vetor 2: 2
Digite o 1° número do vetor 2: 4
Digite o 2° número do vetor 2: 6
Digite o 3° número do vetor 2: 8
Digite o 4° número do vetor 2: 10

The expected output

1
2
3
4
5
6
7
8
9
10

The output generated

0
0
0
0
0
0
0
0
0
0
using static System.Console;

namespace TesteVetor
{
    class Program4
    {
        static int Main(string[] args)
        {
            var vetor1 = new int[5];
            var vetor2 = new int[5];
            var vetor3 = new int[10];
            int i = 0, j = 0;

            for (i = 0; i < vetor1.GetLength(0); i++)
            {
                Write($"Digite o {i}° número do vetor 1: ");
                vetor1[i] = int.Parse(ReadLine());
            }

            WriteLine();

            for (i = 0; i < vetor2.GetLength(0); i++)
            {
                Write($"Digite o {i}° número do vetor 2: ");
                vetor1[i] = int.Parse(ReadLine());
            }

            for (i = 0; i < 5; i++)
            {
                vetor3[i] = vetor1[i];
                j++;
                vetor3[i] = vetor2[i];
                j++;
            }

            for (i = 0; i < vetor3.GetLength(0); i++)
            {
                WriteLine($"{vetor3[i]}");
            }
            ReadKey();
            return 0;
        }
    }
}

2 answers

1


I won’t try to solve the other problems of the code, because I’ve taught the right one, but apparently it doesn’t matter, but there is the tip to anyone who reads this answer that this code should not be used this way (there are several other codes here on the site that show a better way)then you have two problems to solve:

  • use the variable j that was created, augmented, but not used in `array.
  • in the second loop save the data in vetor2 and not in vetor1 that goes over what was in vetor1 and vetor2runs out of data.

Thus:

using static System.Console;

namespace TesteVetor {
    class Program4 {
        static void Main() {
            var vetor1 = new int[5];
            var vetor2 = new int[5];
            var vetor3 = new int[10];
            for (int i = 0; i < vetor1.GetLength(0); i++) {
                Write($"Digite o {i}° número do vetor 1: ");
                vetor1[i] = int.Parse(ReadLine());
            }
            WriteLine();
            for (int i = 0; i < vetor2.GetLength(0); i++) {
                Write($"Digite o {i}° número do vetor 2: ");
                vetor2[i] = int.Parse(ReadLine());
            }
            for (int i = 0, j = 0; i < 5; i++) {
                vetor3[j++] = vetor1[i];
                vetor3[j++] = vetor2[i];
            }
            for (int i = 0; i < vetor3.GetLength(0); i++) WriteLine($"{vetor3[i]}");
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • I didn’t know you could use two boot variables at once on for.

-1

Good afternoon, friend putting his code in VS, I checked that there are some inconsistencies, such as when asking the user to enter the data of vector 2, is being written in vector 1, as shown below:

inserir a descrição da imagem aqui

about the incorrect result is due to the is that fills the vector 3, because vc is incrementing the variable "j", but is not using.

inserir a descrição da imagem aqui

An example of how the correction should be applied.

inserir a descrição da imagem aqui

Remembering that there are more elegant ways to solve, but following your reasoning, follows image below how your code should be:

inserir a descrição da imagem aqui

  • 5

    Please do not put pictures of the codes but paste them here and format correctly.

Browser other questions tagged

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