Problem with C exercise

Asked

Viewed 148 times

3

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

I’ve tried everything, I can’t do the last part to tell you how many times each number shows up.. changed the code several times and 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]);
                }
            }
        }
    }
}
  • What size of vectors you are using to store the data?

1 answer

9

You are creating your vectors with zero size. So when you do:

vetPrincipal[contador] = digitado;

He’s trying to access a memory region that doesn’t exist. Try giving your vector a positive size (as the problem says the numbers that will appear will come from 1 to 20, a vector of size 20 should be enough; if there was no such restriction, it would be necessary to make dynamic allocation or use another data structure). Just remember that in C the indices start at zero, not at one (i.e. the first element is the 0, the second is the 1 and the twentieth is 19).

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

scanf("%i",digitado);

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

scanf("%i",&digitado);

Some other points that deserve attention:

  • The solution you’re trying to do involves saving all the numbers you’ve already typed - which could be any number, so you can’t create a static vector to keep them all. Try to think of a way not to have to keep all the numbers.

  • I see that you have not started your variables. Unlike Java (for example), uninitiated variables do not receive zero by default. It means that your contador does not begin with 0, but with whatever value happens to be in the memory when your program starts.

  • Always try to keep your code well-indented, it helps a lot to identify errors that otherwise would go unnoticed. I bet you haven’t noticed that you have one for inside each other, right? And both with the same variable i...

Besides, I couldn’t quite understand your logic. As you are learning, it helps a lot to be generous with the comments: saying what you intend to do, along with the code that does the actual thing. Over time, and with experience, you can reduce comments and make them at a higher level, but for now I suggest using and abusing them (for yourself, and for those who read and interpret what you wrote).

  • 2

    +1 Very didactic!

Browser other questions tagged

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