Δημοσιεύτηκε: 31 Ιούλ 2011, 23:21
από stamatiou
Να και η λύση της άσκησης:
Spoiler: show
Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int check(int guess,int right,int lim) {
if(guess <= 0 || guess > lim) {
return -1;
}else if(guess < right) {
return 0;
}else if(guess > right) {
return 1;
}else {
return 2;
}
return 3;
}

int main(void) {
char inbuf[256];
int guess,right,lim = 150,counter=0;
srand(time(NULL));
right = rand() % 151;
printf("Enter the number: ");
while((check(guess,right,lim)) != 2) {
fgets(inbuf,256,stdin);
guess = atoi(inbuf);
if((check(guess,right,lim)) == 0) {
printf("No it is higher, try again: ");
}else if ((check(guess,right,lim)) == 1) {
printf("No it is less, try again: ");
}else if ((check(guess,right,lim)) == -1) {
printf("The limit is from 0 to 150! Try again: ");
}
counter++;
}
printf("Congratulations! You found it with only %d tries!\n",counter);
return 0;
}

Αυτό με τον πίνακα δεν το κατάλαβα, γιατί να το βάλουμε;