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

4

I'm 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:

Thecodesofarisasfollows:

Giventhe"random" number generated, try "attempts", "num" the number that the user enters, and "n" the interval 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, however I do not know which operations to insert so that it shows the answers according to the table.

    
asked by anonymous 30.11.2015 / 21:02

2 answers

1

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

That is, assuming that the number to hit was 1 and that the user shot 4 : for a range between 1 and 10 the percentage is approximately % by%, for a range between 40% and 1 the percentage is approximately 1000000000 .

In other words, your 0.000000001% function needs to receive the range (and you do not need to receive the try number)!

double dicas(int tiro, int alvo, int alcance) {
    return (100.0 * abs(tiro - alvo)) / alcance;
}
    
16.12.2015 / 16:57
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");
	}

}
    
01.12.2015 / 00:21