Limit, or capacity of a Vector

Question:

Imagine that you are asked "when the capacity reaches the limit, you should notify the user" I have a vector of integers with 15 positions, and the user will insert values ​​inside, the objective is to notify when I reach the end, how?

#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]);
    }
}

Answer:

Create a loop with while(with a count 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();

Scroll to Top