Exercise Vectors, I'm stranded

0

It is intended to elaborate a program that asks integers to the user and saves them in a vector, this procedure is repeated until the user enters the value zero.

At the end the program should show all the values that the user entered. You should also consider the following assumptions: - Keep in the vector, only the values in the interval [100, 500]; - The vector has a maximum capacity of 15 elements; - When the capacity reaches the limit, you must inform the user.

You should develop the following subprograms: - A function with the prototype, int lerVectorDeInteiros(int tabelaInteiros[], int nElementos) , which will handle the collection of integers, store those values in the vector, and consider all previous assumptions. At the end the function should return the amount of elements inserted in the vector. - A procedure with the following prototype, void mostrarTabelaInteiros(int tabelaInteiros[], int nElementos) , which will display only the values entered by the user on the screen.

Code made so far:

#include <stdio.h>
#define MAX 15

int lerVectorDeInteiros(int *);

int main ()
{
    int vetor[MAX];
    int quantidadeElementos = 0;

    quantidadeElementos =lerVectorDeInteiros(vetor);
    printf("A quantidade de elementos que foram inseridos no vetor sao %d", quantidadeElementos);

    return 0;
}

int lerVectorDeInteiros ( int vetor[MAX] )
{
    int i;
    int temp = 0;
    int contador = 0;

    for ( i = 0; i < MAX; i++ )
    {
        scanf("%d", &temp);
        if(temp > 99 && temp < 501)
        {
            vetor[i] = temp;
            contador++;
        }
    }

    return contador;
}
    
asked by anonymous 22.10.2017 / 14:47

1 answer

0

First mistake, you are reading the variable "temp" and you are generating random numbers with it, and that is not the goal. You have to read the user, while for! = 0 you will generate numbers.

Second, when you generate a number, you should check if it already exists inside the vector, if you already have one, otherwise you enter the next available position.

Tip: Use one variable to read the user and another to count the numbers. I think with this help you can solve your problem.

    
24.10.2017 / 14:52