C - Vector (Find the position of the minimum of Vector 1 - Show on screen the even and odd numbers of Vector1)

1

I need

1. Show the odd and even numbers of the Vector1 on the screen

2. Show on screen the minimum number of vector 1, and in what position of the vector is each number. For example: The minimum number is -3 which is in position 3

I'm a beginner in C, I try but I'm dizzy. I need help from you Here is my code:

int main()
{
    int i=0;
    int j=0;
    int largovector=0;
    int vector1[largovector];
    int vector2[largovector];
    int minimo=999999;
    int maximo=-999999;
    int contador=1;

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

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

    for(i=largovector; i>=0; i--,contador++) //Asignar los valores del vector1 al  vector2
    {
        vector2[contador]=vector1[i];
    }
    printf("El vector 1 esta formado por los numeros: ");//Imprimimos el primer vector

    for(j=1; j<=largovector; j++)
    {
        printf(" %d ",vector1[j]);
    }
    printf("\n");

    printf("El vector 2 esta formado por los numeros: "); //Imprimimos el segundo vector

    for (i=1; i<=largovector; i++)
    {
        printf(" %d ",vector2[i]);
    }
    printf("\n");

    printf("\nCantidad de numeros pares en el vector: ");//imprimimos el par

    for(j=1; j<=largovector; j++)
    {
        int pares=j%2;
        if(pares==0)
            printf("%d ",j);
    }
    printf("\n");

    printf("Cantidad de numeros impares en el vector: ");//imprimimos el impar
    for (j=1; j<=largovector; j++)
    {
        int impares=j%2;
        if(impares!=0)
            printf("%d ",j);
    }
    printf("\n");


    for (j=1; j<=largovector; j++)
    {
        if (j<=minimo)
            minimo = j;
    }

    printf("\nEl numero minimo es el %d que esta en la posicion %d",minimo);

    return 0;
}
    
asked by Jefren 28.04.2018 в 05:38
source

1 answer

1

You must remember that the indexes for arrays start at 0 , that is, if you define an array of 5 elements, the positions are [0,1,2,3,4]. Now, when they ask you to show the even and odd numbers, they refer to the elements of vector 1, that is, you must work on the values that you have in the vector. From what I saw in your code, you are working on the index to traverse the vector and not on the element that is indexed by that index:

Your code (with the modification of the dimension):

for(j=0; j<largovector; j++)
    {
        int pares=j%2;
        if(pares==0)
            printf("%d ",j);
    }

Here, you are controlling that the value of the index is even, and it is not what you need, if not the element of the vector to which that index points, that is, vector1[j] . For example:

for(j=0; j<largovector; j++)
    {
        int pares=vector1[j]%2;
        if(pares==0){
            printf("Par: %d ",vector1[j]);
        }else{printf("Impar: %d ",vector1[j]);}
    }

This is traversed by the vector with the index j and controlled if the pointed element is even or odd, and printed in each case.

On the other hand, when you look for the smallest element of the vector, you are making the same mistake of working with the index and not with the value to which that index points:

Your code (with the modification of the dimension):

for (j=0; j<largovector; j++)
    {
        if (j<=minimo)
            minimo = j;
    }

For this problem, you have to go through the vector and look for the smallest element, when you find it you have to save your position as well. For example:

for (j=0; j<largovector; j++)
    {
        if (vector1[j]<=minimo){
            minimo = vector1[j];
            pos = j;}
    }

If you look at minimo the smallest element ( vector1[j] ) is saved and not the value of the index. It also stores the value of the position where that element is, that is, the value of the index ( j ) in the variable pos .

With this revised, the exercise could look like this:

    #include <stdio.h>
int main()
{
    int i=0;
    int j=0;
    int largovector ;

    int minimo=999999;
    int maximo=-999999;
    int contador=1;
    int pos=0;

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

    int vector1[largovector];
    int vector2[largovector];
    printf("%d\n", 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]);
        vector2[j] = vector1[j];
    }


    printf("El vector 1 esta formado por los numeros: ");//Imprimimos el primer vector

    for(j=0; j<largovector; j++)
    {
        printf(" %d ",vector1[j]);
    }
    printf("\n");

    printf("El vector 2 esta formado por los numeros: "); //Imprimimos el segundo vector

    for (i=0; i<largovector; i++)
    {
        printf(" %d ",vector2[i]);
    }
    printf("\n");

    printf("\nNum pares e impares del vector1: ");//imprimimos el par

    for(j=0; j<largovector; j++)
    {
        int pares=vector1[j]%2;
        if(pares==0){
            printf("Par: %d ",vector1[j]);
        }else{printf("Impar: %d ",vector1[j]);}
    }
    printf("\n");

    for (j=0; j<largovector; j++)
    {
        if (vector1[j]<=minimo){
            minimo = vector1[j];
            pos = j+1;}
    }

    printf("\nEl numero minimo es el %d que esta en la posicion %d",minimo,pos);

    return 0;
}

Example:

Ingrese el largo del vector
5
Ingrese el valor de la coordenada 1 del vector: 
10
Ingrese el valor de la coordenada 2 del vector: 
20
Ingrese el valor de la coordenada 3 del vector: 
15
Ingrese el valor de la coordenada 4 del vector: 
30
Ingrese el valor de la coordenada 5 del vector: 
50
El vector 1 esta formado por los numeros:  10  20  15  30  50 
El vector 2 esta formado por los numeros:  50  30  15  20  10 

Num pares e impares del vector1: Par: 10 Par: 20 Impar: 15 Par: 30 Par: 50 

El numero minimo es el 10 que esta en la posicion 1

Finally, to start with c I recommend the book " The programming language C " by Brian Kernighan and Dennis Ritchie.

    
answered by 28.04.2018 / 06:30
source