Doubt in vector in c

Asked

Viewed 78 times

-1

Create a counter function that works as follows:

  • the initial value of the counter is 5.
  • every time the function is called its counter shall be decreased.
  • the counter shall reset automatically: when the counter reaches 0, on the next function call the counter shall be reset with 5 again.
#include<stdio.h>
static int contador = 5;

void cont();

int main(){
        cont();
        return 0;
}

void cont(){
        int i,j=0;
        int vet[contador];
        do{
                for(i = 1;i <= contador;i++){
                        vet[i]++;
                        printf("%d\n",vet[i]);
                }
                contador -=1;
        }while(contador != 0);

}

  • 1

    Eae man, you could explain your doubt?

  • I am doubtful on the part of implementing an array and when the counter should reset automatically when the counter reaches 0.

  • 1

    Your question says nothing about vector, where a vector enters what you want?

  • then and bury the vector and consider that my doubt and the counter should reset automatically: when the counter reaches 0, on the next call of the function the counter should be reset with 5 again.

2 answers

3


There is a problem in its function, it accesses an invalid memory position. In C the indexes of a vector go from 0 to N-1. In your case the vector goes from v[0] to v[4], need to fix this in the loop.

To make the counter restart to 5 just put one if at the end of the loop. Note that with this your program will go into infinite loop, since counter will always be different from 0 when it is evaluated on do{}while(). Follows the amendment below:

void cont(){
        int i;
        int vet[contador];
        do{
                for(i = 0; i < contador; i++)
                {
                        vet[i]++;
                        printf("%d\n",vet[i]);
                }
                contador -=1;
                if (contador == 0)
                    contador = 5;
        }while(contador != 0);
}
  • 2

    The if should go outside the do-while, otherwise the loop will be infinite, because whenever it reaches the end, the value of contador will again be 5 and the condition contador != 0 will never be satisfied.

1

From what I can understand, you want to make a countdown count of how many times this function was called, arriving at the value 0 (zero) it returns to the initial value.

#include <stdio.h>

#define MAX 3

int GLOBAL_COUNT = MAX;

void contador();

int main() {
    for(int i = 0; i <= 7; i++) {
        contador();
    }
    return 0;
}

void contador() {
    if (GLOBAL_COUNT <= 0) {
        GLOBAL_COUNT = MAX;
    }
    printf("chamada: %d/%d\n", GLOBAL_COUNT, MAX);
    GLOBAL_COUNT--;
}

See the example working on IDEONE.

In this my example I took the liberty of changing the value to 3 just to simplify the visualization and just that, in it I created a constant value MAX and assigns to a variable that is the global replay counter, this will be decremented in the function call.

In the main I made a loop of 7 calls to show the quoter working.

In accountant() it is comparing whether the current value of GLOBAL_COUNT is less than or equal to 0 (zero), if so, then it restarts the value, after which it will print the current value and then it will decrement stating that it passed the function.

Browser other questions tagged

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