C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 12th, 2005, 06:07 AM
bigdaddy bigdaddy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 4 bigdaddy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 52 sec
Reputation Power: 0
I am overwhealmed, pls help!!!

hi..was wondering if any of the experts here will be able to help me with the code of this program...its very urgent cos i need to submit it 2 hrs after i post this msg...thanx for the help...


Background
Nemo and Dory are at it again. You are a born-again shark who has gone vegetarian. You are swimming nearby and you eavesdrop on their conversation:
Dory: “Let’s play a number guessing game.”
Nemo: “Err...”
Dory: “Come on, it’ll be lots of fun!”
Nemo: “Ok ok... what is it?”
Dory: “I’ll think of a secret random number between 1 too 100 inclusive, and you have to guess it. After each guess, I’ll tell you whether it is too high, too low, or correct. We’ll see how many guesses you’ll take.”
Nemo: “Ok... but no cheating ok?”
Dory: “Ok, promise. I’ve thought of a random number. Shoot!”
Nemo: “50.”
Dory: “Too low.”
Nemo: “75.”
Dory: “Too high.”
Nemo: “63.”
Dory: “Too low.”
Nemo: “69.”
Dory: “Too high.”
Nemo: “66.”
Dory: “Too high.”
Nemo: “65.”
Dory: “Correct! You took 6 guesses. This is so much fun. Shall we play again?”


Intelligent Guessing
If you examine Nemo’s pattern of guessing more closely, you realize that he is not as dumb as he looks. His first guess is:
(Min + Max) / 2 = (1 + 100) / 2 = 50. (after adjustment; rounding down or rounding up (a guess of 51) are both acceptable).
Since this was too low, his second guess is:

(50 + 100) / 2 = 75.
Since this was too high, his third guess is:

(50 + 75) / 2 = 62. (after adjustment; rounding down or rounding up (a guess of 63) are both acceptable).
And so on. Do you see a pattern? Identify it and think about how you would write the algorithm for intelligently guessing a random integer.

Implementation
You will be provided with an input file consisting of different experiment scenarios. Each line in the input file gives the parameters for a single experiment. Each experiment typically contains many simulations (100, 1,000, or possibly more). A typical line from the input file consist of four integers separated by blank spaces as shown below:
122 1 100 1000

The explanation for each number is given as follows:
122 Random number seed
1 Minimum value for secret number
100 Maximum value for secret number
1000 Number of simulations


This says that for this particular experiment, the random number seed is 122, the minimum value for the secret number is 1, the maximum value for the secret number is 100, and we wish to carry out 1,000 simulations.


An Experiment
We illustrate how the experiment is performed for the above. First, we initialize the random number seed to 122. The random number seed should be initialized only at the beginning of each experiment; not in between simulations. For the above experiment, we need to conduct 1,000 simulations.

A Simulation
We illustrate how a single simulation is performed using the parameters given above. First, we choose a secret number which is a random integer between 1 to 100 inclusive. Then, we keep simulating Nemo’s guesses by intelligently guessing the secret number. We also simulate Dory’s truthful responses (too high / too low / correct) to Nemo. We take note of the number of guesses Nemo takes to guess each secret number.
Experiment Data
After running 1,000 simulations, we calculate the average number of guesses taken by Nemo to guess each random secret number correctly, and display it rounded off to three decimal places.
Input File
Assume that the text input file is always called number1.txt. This sample input file contains data for seven experiments:

121 1 100 100
122 1 100 1000
123 1 100 10000
123 2 101 10000
123 -49 50 10000
124 1 1000 10000
125 1 10000 10000


Sample Run
The following shows a sample run for the input file numbers1.txt. Actual values for the average number of guesses have been replaced with x’s.

Seed: 121, min: 1, max: 100, average number of guesses: x.xxx.
Seed: 122, min: 1, max: 100, average number of guesses: x.xxx.
Seed: 123, min: 1, max: 100, average number of guesses: x.xxx.
Seed: 123, min: 2, max: 101, average number of guesses: x.xxx.
Seed: 123, min: -49, max: 50, average number of guesses: x.xxx.
Seed: 124, min: 1, max: 1000, average number of guesses: x.xxx.
Seed: 125, min: 1, max: 10000, average number of guesses: xx.xxx.

Assume that the minimum value of the secret number is never greater than the maximum value of the secret number.

Assume that all the numbers in the input file will never be greater than 1,000,000 or smaller than -1,000,000.

Ensure that your intelligent guesser works for all possible values of the secret number, including both the minimum and maximum values.

Reply With Quote
  #2  
Old October 12th, 2005, 06:09 AM
bigdaddy bigdaddy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 4 bigdaddy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 52 sec
Reputation Power: 0
this is my code, but i dunno wats wrong cos its not working

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define FILENAME "numbers1.txt"

//define function prototype
int guess (int a);
int rand_int(int a, int b);

int main(void)
{

//declare variables

unsigned int seed;
int a,b,simulations,k,number,no_of_guess;
double average_guess,total;

FILE *fp;
fp = fopen(FILENAME, "r");
if (fp == NULL)
printf("Error opening file \n");
while ((fscanf(fp, "%i %i %i %i", &seed, &a, &b, &simulations))==4)
{
srand(seed);
printf("%i %i %i %i",seed,a,b,simulations);
for (k=1, total=0 ; k<=simulations ; k++)
{
number = rand_int(a,b);
no_of_guess = guess(number);
total += no_of_guess;
}

average_guess = (total / simulations);
printf("Seed: %i min: %i, max: %i, average number of guesses : %.3f \n",seed,a,b,average_guess);
}

fclose(fp);
return 0;

//function to generate random number between a and b

int rand_int (int a, int b)
{

//declare variables
int number;

number = rand()%(b-a+1)+a;

return number;

}

//function to compute guess function

int guess(int number)
{

//declare variables

int a,b,i,max,min;
double guess_number;

guess_number = (a+b)/2;

for(i=1,min=a,max=b ; guess_number!=number ; i++) //loop until condition is false and return i which is no of loops gone through
{
if(guess_number < number)
{
min = guess_number+1;
guess_number = ceil((max+min)/2);
}
if(guess_number > number)
{
max = guess_number-1;
guess_number = ceil((max+min)/2);
}
return i;
}

the data file to read contains the following data
121 1 100 100
122 1 100 1000
123 1 100 10000
123 2 101 10000
123 -49 50 10000
124 1 1000 10000
125 1 10000 10000
where 121 is seed no, 1 is min value, 100 is max value and 100 is no of simulations....

Reply With Quote
  #3  
Old October 12th, 2005, 08:16 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2005
Posts: 632 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 4 h 45 m 4 sec
Reputation Power: 3
After a quick look I'd say add parameters a and b to your function guess(int number), otherwise a and b will be 0 or something. Move the return i statement out of the for-loop..

Reply With Quote
  #4  
Old October 12th, 2005, 08:19 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2005
Posts: 632 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 4 h 45 m 4 sec
Reputation Power: 3
Don't know if the ansers are correct (haven't checked the algorithm thoroughly) but I get this if I do above corrections:
121 1 100 100Seed: 121 min: 1, max: 100, average number of guesses : 4.830
122 1 100 1000Seed: 122 min: 1, max: 100, average number of guesses : 4.941
123 1 100 10000Seed: 123 min: 1, max: 100, average number of guesses : 4.918
123 2 101 10000Seed: 123 min: 2, max: 101, average number of guesses : 4.918
123 -49 50 10000Seed: 123 min: -49, max: 50, average number of guesses : 4.845
124 1 1000 10000Seed: 124 min: 1, max: 1000, average number of guesses : 7.228
125 1 10000 10000Seed: 125 min: 1, max: 10000, average number of guesses : 9.850


Hope it's on time for you

EDIT: I guess not ...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > I am overwhealmed, pls help!!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway