Limit, or capacity of a Vector

Asked

Viewed 447 times

3

Imagine you ask "when the capacity reaches the limit you should warn the user" I have an integer vector with 15 positions, and the user will enter values inside, the goal is to warn when to reach the end, as?

#include <stdio.h>
#include <stdlib.h>

#define MAX 15

int lerVectorDeInteiros(int *);
void mostrarVetor(int *);
void inicializarVetor(int *);

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

    inicializarVetor(vetor);

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

    mostrarVetor(vetor);

    return 0;
}

void inicializarVetor(int vetor[MAX]){
    int i;

    for(i=0; i<MAX; i++){
        vetor[i] = 0;
    }

}

int lerVectorDeInteiros(int vetor[MAX]){
    int i;
    int temp = 0;
    int contador = 0;
    for(i=0; i<MAX; i++){
        printf("Insire o valor da posicao %d do vetor: ", i+1);
        scanf("%d", &temp);
        if(temp > 99 && temp < 501){
            vetor[i] = temp;
            contador++;
        }
    }
    return contador;
}

void mostrarVetor(int vetor[MAX]){
    int i;

    for(i=0; i<MAX; i++){
        printf("O valor da posicao %d e: %d\n", i+1, vetor[i]);
    }
}
  • "when the capacity reaches the limit you should warn the user" - If you are reading the amount corresponding to the size then you will never read more than the amount. So warning you when it’s over doesn’t make sense, unless you put a printf after the for reading to indicate that you have read everything. For me it is not very clear what you want to do.

2 answers

0

Create a loop with while(with counter of course) and at the end of the loop add an if condition(if it is the final position)...and give the warning with printf();

  • Good afternoon Mauricio, as I see, or "check" whether or not it is the final position?

  • Yes young. Limit= last position... do with while... add a counter and put one if the counter is equal to the final post

0

Just put a printf() warning that it has reached the limit after the for that this in its function int lerVectorDeInteiros(int vetor[MAX]), for always when he comes to the end of that for, or better always when it comes out of that loop for, it will be at the end of the vector, ie at its limit.

But how to check if you are in fact or not at the end, well just think of the following logic, "if you are on the edge, print the warning", It is to do this just make a if, in this case you have to think of the following, which variable represents the vector boundary? , if you think a little you will notice that is the variable MAX, because it represents the maximum value of data that the vector supports, but not just that, MAX - 1 represents the last position of the vector, that is, to check whether or not it is in the limit if where you compare the current position of your vector with the last, if it is in the last one means that your vector has reached the limit, by that time of the championship you should already know that the variable that represents the current position of your vector in the case of that for is the i, then just compare it.

For comparisons in if there are several ways, to check if X is greater than Y, just do the following if ( X > Y ), to check if it is smaller, if ( X < Y ), greater or equal, if ( X >= Y ), equal to or equal to, if ( X <= Y ), check if it’s equal if ( X == Y ), here you must take care, always remember that two equals is an equality comparison, and one is value assignment.

Good are basically these comparison operators, if I haven’t forgotten any, anyway then your if would be so if ( i == MAX - 1 ), and if this is true then you will print your warning.

I hope I’ve helped, anything at all just ask, good luck with your C language studies.

Browser other questions tagged

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