Limit, or ability of a Vector

3

Imagine asking yourself "when the capacity reaches the limit you should warn the user" I have a vector of integers with 15 positions, and the user will enter values in there, the goal is to warn when it reaches 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]);
    }
}
    
asked by anonymous 22.10.2017 / 15:41

2 answers

0

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

    
22.10.2017 / 15:45
0

Just put a printf() warning that it reached the limit after the for that is in its int lerVectorDeInteiros(int vetor[MAX]) function, since whenever it reaches the end of that for , or better always when it exits the loop of that for , it will be at the end of the vector, that is, at its limit.

But how do you check to see if you are actually or not at the end, you just have to think of the following logic, "if you are at the limit, print the warning", you can do that by just doing a if , in this case you you have to think of the following, which variable represents the limit of the vector ?, if you think a little you will notice that it is the variable MAX , because it represents the maximum data value that the vector supports, but not only that, MAX - 1 represents the last position of the vector, that is, to check whether or not in the limit make a if where you compare the current position of your vector with the last one, if it is in the last one means that your vector has reached the limit, At this point in the championship you should know that the variable that represents the current position of your vector in the case of that for is i , so just compares it.

To make 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 ) , lower or equal, if ( X <= Y ) , check if it is equal if ( X == Y ) , here you should be careful, always remember that two equals is an equality comparison, and one is value assignment.

Well, these are the comparison operators, if I have not forgotten any of them, then anyway your if would be so if ( i == MAX - 1 ) , and if this is true then you will print your warning.

I hope to have helped, anything is just asking, good luck with studies in the C language.

    
22.10.2017 / 16:41