Guess a number, and in attempts, by the percentage show hints

Asked

Viewed 1,071 times

4

I am trying to create a game in which the user must determine an interval and guess a random number that is generated within this range. When the user inserts a number to guess, the program must reply with one of the following messages:

inserir a descrição da imagem aqui

The code so far is like this:

Being "Resp" the generated random number, "Tent" the attempts, "num" the number the user inserts, and "n" the range the user determines in Random().

int random();
void dicas(int num, int resp, int tent);

int main()
{
    int continuar=1,
        resp,
        tent,
        num,
        n;

    do{
        system("cls || clear");
        resp = random();

        printf("Comecou! Tente adivinhar o numero!\n\n");
        tent = 0;

        do{
            tent++;
            printf("Tentativa %d: ", tent);
            scanf("%d", &num);
            dicas(num,resp,tent);
        }while( num != resp);

        printf("Digite 0 para sair, ou qualquer outro numero para continuar: ");
        scanf("%d", &continuar);
    }while(continuar);

}

int random()
{
    int n;
    printf("Insira o valor maximo que pode ser sorteado: ");
    scanf("%d", &n);
    printf("Sorteando numero entre 1 e %d...\n",n);
    srand((unsigned)time(NULL));
    return (1+rand()% n);
}

void dicas(int num, int resp, int tent)
{

    if(num>resp)
        printf("O numero sorteado e menor que %d\n\n", num);

    else if (num<resp)
        printf("O numero sorteado e maior que %d\n\n", num);

    else
        printf("Parabens! Voce acertou o numero em %d tentativas!\n\n", tent);
}

My problem is in the tips, the current ones don’t correspond to what I need to do, but I don’t know which operations to insert so it shows the answers according to the table.

  • Note that the tag [tag:codeblocks] has this at the end of the description: "If the question is not about Codeblocks, do not use this tag, even if you are using Codeblocks in your project."

  • This smells like college work. (=

  • Yes. The first half of the job is in that question :) :) :). Felipe, what exactly are you in doubt about? In calculating the percentage?

  • Yes, it’s in the application. I’ve tried 3 different operations and they didn’t work. And yes, the work is college, it’s a matter completion project. My question is inside the "void tips(int num, int Resp, int Tent)", I need to insert the answers of the table there, with the operations, etc.

  • Don’t put the srand() inside a function that will be called several times: put it inside the function main().

2 answers

1

What you need is to compare the difference between target and chosen number and the range of possible numbers.

That is, imagining that the right number was 1 and that the user shot 4: for a range between 1 and 10 the percentage is approximately (headshot), 40%, for a range between 1 and 1000000000 the percentage is approximately, 0.000000001%.

I mean ... your job dicas() need to receive the range (and do not need to receive the try number)!

double dicas(int tiro, int alvo, int alcance) {
    return (100.0 * abs(tiro - alvo)) / alcance;
}

0

void dicas(int num, int resp, int tent)
{

	float porcento = ((((double) num/resp)-1)*100);
	
	if(porcento < 0){
		porcento *= -1;
	}
	
	if (porcento == 0){
		printf("Parabens! Voce acertou o numero em %d tentativas!\n\n", tent);
	}else if (porcento > 0 && porcento < 10){
		printf("Muito quente\n\n");
	}else if (porcento > 10 && porcento < 20){
		printf("Quente\n\n");
	}else if (porcento > 20 && porcento < 30){
		printf("Frio\n\n");
	}else if (porcento > 30 && porcento < 40){
		printf("Muito frio\n\n");
	}else if (porcento > 40){
		printf("Continue tentando\n\n");
	}else{
		printf("Erro!\n\n");
	}

}

Browser other questions tagged

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