Create a vector similar to another to create inverse

0

I have the following exercise:

  

Only using For - While - If - Switch Case - Vectors - Printf - Scanf - Int - Char

     

It will work with two vectors, where the length of the vector must be given by the user by keyboard (int longVector). Once the user defined the length of the vectors, the first vector is loaded (vector1), the user must load by keyboard each whole number that forms the vector. When we have the vector1 loaded with the numbers of the user, for example vector1 = {1,4,5,3,6}, the vector vector2 must be created, with the numbers all inverted, that is, for example, vector2 = { 6, -3.5.4.1}.

     

B- Show on screen both vectors in two different lines with this format per console: Vector 1 is formed by the numbers: 1 4 5 -3 6 And vector 2 is formed by the numbers: 6 -3 5 4 1.

I'm a beginner, and I got to do it up here:

int main()
{
    int i=0;
    int j=0;
    int largovector=0;
    int vector1[largovector];
    int vector2[largovector];

    printf("Ingrese el largo del vector\n"); //Definimos el largo del vector
    scanf("%d",&largovector);

    for(j=0; j<largovector; j++) //Definimos los numeros del vector
    {
        printf ("Ingrese el valor de la coordenada %d del vector: \n",j);
        scanf ("%d",&vector1[j]);
    }


    printf("El vector 1 esta formado por los numeros: ");//Imprimimos el vector
    for(j=0; j<largovector; j++)
    {
        printf("%d ",vector1[j]);
    }
    printf("\n");

    printf("El vector 2 esta formado por los numeros: ");
    for (i=largovector-1; i>=0; i--)
    {
        printf("%d ",&vector2[j]);
    }

    return 0;
}

My problem is that I do not know how to create a Vector2[] that has the same coordinates as the Vector1 to create the inverse. How could I do it?

    
asked by Jefren 28.04.2018 в 01:45
source

1 answer

1

In this case, what I understand is that you want to obtain a vector and then generate another one with the contents of the first one in reverse. In this case it does not work for you since you are not assigning the respective values of the first vector to the second one. Here is the solution and I hope it helps you.

int main() { int i=0; int j=0; int largovector=0;  

printf("Ingrese el largo del vector\n"); //Definimos el largo del vector
scanf("%d",&largovector);

int vector1[largovector];int vector2[largovector];

for(j=0; j<largovector; j++) //Definimos los numeros del vector
{
printf ("Ingrese el valor de la coordenada %d del vector: \n",j);
scanf ("%d",&vector1[j]);
}
//Asignar los valores del vector1 al  vector2
 for(int contador=0,i=largovector-1;i>=0;i--,contador++){
 vector2[contador]=vector1[i];
 }

printf("El vector 1 esta formado por los numeros: ");//Imprimimos el vector
for(j=0; j<largovector; j++)
{
printf("%d ",vector1[j]);
}
printf("\n");

printf("El vector 2 esta formado por los numeros: ");
for (i=0; i<largovector; i++)
{
printf("%d ",vector2[i]);
}


return 0;
}
    
answered by 28.04.2018 / 02:36
source