Guessing Game

hmm
Last edited on
first idea that came up to my mind:

1) read a number of data to be evaluated from file
2) use for statement to count
a) read two integers (one of them will be negative due to dash)
b) read a string that will hold letters
c) do the average of absolute values of those integers
d) go through the string and modify the average value according to letters

Last edited on
If I do the average thing, then I'd be doing this?
1) 1+5 = 6/2 = 3
2) 5+20 = 25/2 = 12 (having type int)
3) 40+44 = 84/2 = 42

I could get the right outputs except for #2 because I would be getting 16 (12+4), instead of the right output which is 19 (according to the sample, that is).

And, how do I do it with pointers...
closed account (j3Rz8vqX)

The strategy provided sounds like binary search (Google "Binary Search" for more detail).

Your first process of calculating the average is correct.

1) 1+5 = 6/2 = 3
2) 5+20 = 25/2 = 12 (having type int)
3) 40+44 = 84/2 = 42

The reason why it doesn't seem to make sense for #2 is because your missing a step.

Your instruction quote: "The process is repeated until the number is found."

You need to repeat the process, not average once then increment/decrement by a unit.

Instead you should determine your minimum and maximum, then redetermine these every time the loop is repeated.

Example:(rounding down)

First Cycle: Min=1, Max=5, Average=3; // L, max=average;
Second Cycle: Min=1, Max=3, Average=2; // L, max=average;
Third Cycle: Min=1, Max=2, Average=1; // Y

First Cycle: Min=5, Max=20, Average=12; // H, min=average;
Second Cycle: Min=12, Max=20, Average=16; // H, min=average;
Third Cycle: Min=16, Max=20, Average=18; // H, min=average;
Fourth Cycle: Min=18, Max=20, Average=19; // Y

First Cycle: Min=40, Max=44, Average=42; // Y


Not sure why you would get "HHHHY" on #2 though.
If you're accidentally counting loops, you might want to consider counting High's and Low's instead.

And if the count is correctly embedded, then possibly your output may be counting from 0 when producing the "H"s in the string output. (Too many factors)
--------------------------

As for Pointers, I do not recommend using pointers unless you know what your doing.

Pointers are mainly used to share direct memory access of address to objects/functions that are out of scope. And even then, it's more simple to pass by reference; especially for primitives.

But if you must:
http://www.cplusplus.com/doc/tutorial/pointers/

Good Luck.
Last edited on
Keep track of the min and range (difference between max and min). Once you've read in the initial minimum value and calculated the range, traverse the line string with a pointer to (const) char and update min and range as appropriate until you reach a 'Y'. At that point, you'll be able to pick the correct number.
Thanks for the ideas. I'll let you know if I have progress. Thanks!

Edit: Got it working, had to fit in some pointers because we are required to use it :P Thanks!!
Last edited on
Topic archived. No new replies allowed.