Problem with exercise of C

3

The exercise asks for this: 5. Make a program that reads a sequence of numbers between 1 and 20 and shows how many times each number has appeared. The sequence of numbers ends with a number equal to zero.

I have tried everything, I can not do the last part to inform how many times each number appears. I changed the code several times and it continues to crash when I type the numbers, at the moment it is like this:

#include <stdio.h>

int main(){

    int digitado,contador,i,vetPrincipal[0],vetContador[0],repeticoes;

    printf("Digite um numero de 1 a 20, o ultimo sendo 0");
    scanf("%i",digitado);

    while(digitado != 0){
        if(digitado > 0 && digitado<20){
            printf("Digite um numero: \n");
            scanf("%i",digitado);
            vetPrincipal[contador] = digitado;
            //percorre o vetor e insere na posição em que foi adicionado o resultado de quantas vezes apareceu o numero
            repeticoes =1;
            for(i=0;i<contador;i++){
                if (vetPrincipal[i]==digitado){
                    repeticoes++;
                    vetContador[contador] = repeticoes;
                    contador++;
                }
                else        {
                    break;
                }

                //mostra o resultado
                for (i=0;i<20;i++){
                    printf("O numero %d foi digitado %d vezes",vetPrincipal[i],vetContador[i]);
                }
            }
        }
    }
}
    
asked by anonymous 13.08.2014 / 05:34

1 answer

9

You are creating your zero-sized vectors. So when you do:

vetPrincipal[contador] = digitado;

It attempts to access a region of memory that does not exist. Try making a positive size to your vector (like the problem is that the numbers that will appear will come from 1 a 20 , a vector of size 20 should be enough if there were no such restriction, it would be necessary to allocate dynamically or use another data structure). Remember that only C indices start at zero, not one (i.e. the first element is 0 %% is the second 1 and twenty% is 19 ).

Also, if I remember correctly, your way of calling scanf is incorrect:

scanf("%i",digitado);

The parameter for this function should be a pointer (or address) for an integer, not a simple variable. If you want to read something and save to digitado , pass the digitado address as a parameter:

scanf("%i",&digitado);

Some other points that deserve attention:

  • The solution you are trying to do involves saving all the numbers you typed in - which can be any number, so you can not create a static vector to save them all. Try to think of a way to not have to save all the numbers.

  • I see that you have not started your variables. Unlike Java (for example), uninitialized variables do not get zero by default. This means that your contador does not start with 0 , but with any value you happen to be in memory when your program starts.

  • Always try to keep your code well-matched, this helps a lot to identify errors that would otherwise go undetected. I bet you did not notice that you have for inside the other, right? And both with the same variable i ...

Moreover, I could not quite understand its logic. As you are learning, it helps a lot to be generous with the comments: say what you intend to do, along with the code that actually does the thing. Over time and with experience, you can reduce the reviews and do them at the highest level, but for now I suggest using and abusing them (to yourself, and to whoever read and interpret what you wrote).

    
13.08.2014 / 06:02