Exercise Vectors, I’m stranded

Asked

Viewed 80 times

0

It is intended to elaborate a program that asks the user for integer numbers and stores them in an array, this procedure is repeated until the user type the value zero.

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

It must develop the following sub-programmes: - A function with the prototype, int lerVectorDeInteiros(int tabelaInteiros[], int nElementos), that will handle the collection of integer numbers, store these values in the vector , and consider all previous assumptions. At the end the function should return the amount of elements inserted into the vector. - A procedure with the following prototype, void mostrarTabelaInteiros(int tabelaInteiros[], int nElementos) , which will display only user input values on the screen.

Code done 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;
}
  • What have you done? What is your question?

  • Good afternoon Pablo, nothing, I’m looking at the problem because I don’t even understand where to start

  • #define MAX 15 int lerVectorDeintegers(int *); int main() { int vector[MAX]; int quantityElements = 0; quantityElements =lerVectorDenters(vector); printf("The amount of elements that have been inserted into the vector are %d", quantityElements); Return 0; } int readVectorDeintegers(int vector[MAX]){ int i; int temp = 0; int counter = 0; for(i=0; i<MAX; i++){ scanf("%d", &temp); if(temp > 99 && temp < 501){ vector[i] = temp; counter++; } } counter; }

  • This code is that you have already worked out to try to solve the problem ? If yes add it to the question

  • I’m new here, how do I do it?

  • I voted to close the question because you only put here the statement of the work, and did not even deign to put a word of your text or to show what you have already done. Remember that the Stackoverflow PT community is not a free college work resolution service.

  • Isac I understand and realize, sorry, I’m new here. Thank you! D

  • Dude, you have to add your code and also have to specify to us what your question is, I suggest you edit the question and add your question, to add a code, just copy it from the used IDE, then select it whole and click the {} button, okay ;)

  • Exercicio Vetores, estou encalhado -> you will not be able to solve this with programming.

Show 4 more comments

1 answer

0

First error, you’re reading the variable "temp" and you’re generating random number with it, and that’s not the goal. You have to read the user as long as it is != 0 you will generate numbers.

Second, when generating a number, you must check if it already exists inside the vector, if you already have another 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.

Browser other questions tagged

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