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

I am trying to create a game where the user must determine a range and guess a random number that is generated within this range. When the user enters a number to try to guess, the program must respond with one of the following messages:

insert the description of the image here

The code so far looks like this:

Being "resp" the random number generated, "attempt" the attempts, " num "the number that the user enters, and" n " the range that 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 do not correspond to what I need to do, but I do not know which operations to insert so that it shows the answers as the table.

 4
Author: Comunidade, 2015-11-30

2 answers

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

That is, imagining that the number to hit was 1 and that the user shot 4: for a range between 1 and 10 the percentage is approximately (top-up count) 40%, for a range between 1 and 1000000000 the percentage is approximately 0.000000001%.

That is ... your function dicas() needs receive range (and don't need to receive try number)!

double dicas(int tiro, int alvo, int alcance) {
    return (100.0 * abs(tiro - alvo)) / alcance;
}
 1
Author: pmg, 2015-12-16 16:33:49

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");
	}

}
 0
Author: Micael Ferreira, 2016-12-07 18:25:12