944,132 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 427
  • C RSS
Oct 19th, 2009
0

Loops in C...help needed!!

Expand Post »
If a person has a number between 1 and 100 in mind, you can always figure out what this number is by asking no more than 7 questions “Is your number the same, smaller than or equal to x?” where x is to be determined. The trick is to a binary search so that at least half of the all possibilities are eliminated after each question For example, if the secret number is 59, the following could take place (you may want to use –1 for less than, 0 for equal to, and 1 for larger than)

Is your number equal to, less than, or larger than 50: 1
Is your number equal to, less than, or larger than 75: -1
Is your number equal to, less than, or larger than 62: -1
Is your number equal to, less than, or larger than 56: 1
Is your number equal to, less than, or larger than 59: 0

Write a C program that plays this game. Make sure that you use this binary search:

The indices for the objects to be searched are low, low+1, low+2, ..., high (in our case, they are initially 1, 2, ..., 100). The middle is at m = (low+high)/2 (note that this is an integer division. If m is not the answer, then if the number is less than m, then we need to search between low and m-1. Otherwise, m+1 and high.


I am having major difficulties making this computer program for school. I was hoping someone would be able to guide me in the right direction with this post.

Thanks
Ryan
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hoey33 is offline Offline
2 posts
since Oct 2009
Oct 19th, 2009
0
Re: Loops in C...help needed!!
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 19th, 2009
0
Re: Loops in C...help needed!!
Quote ...
I am having major difficulties making this computer program for school. I was hoping someone would be able to guide me in the right direction with this post.
Can you maybe show us what you've attempted already?
If you've nothing (i.e. no code) to show us, then this (for us) means that you haven't made any effort.
http://www.daniweb.com/forums/announcement8-2.html
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 19th, 2009
-1
Re: Loops in C...help needed!!
Thank you for your reply. I'm sorry I am new at this and have been trying to find extra help. This is my code so far...

#include <stdio.h>
#include <math.h>

main ( )

int binarysearch (vector <int> vec, int low, int high, int key);

if ( high > low )
{
return -1;
}

int mid = (low + high)/2;


if ( vec[mid] == key )
{
return mid;
}

else if ( vec[mid] > key )
{
return binarysearch(vec, low, mid-1, key);
}
else
{
return binarysearch(vec, mid+1, high, key);
}

}
{
}


This is the first course I have ever taken with regard to computer programming. I hope you can take the time to help me out. Thanks for your time.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hoey33 is offline Offline
2 posts
since Oct 2009
Oct 19th, 2009
1
Re: Loops in C...help needed!!
[1
Last edited by cybertooth; Oct 19th, 2009 at 2:44 pm. Reason: delete
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cybertooth is offline Offline
1 posts
since Oct 2009
Oct 19th, 2009
1
Re: Loops in C...help needed!!
Go back and look at that link I'd posted, I'm sure it mentions code tags at some point.

Do you know which language you are using? You seem to have merrily mixed C and C++ (perhaps "borrowing" code from eleswhere?).

Have you written a "hello world" program yet?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 19th, 2009
0
Re: Loops in C...help needed!!
remeber in binary search it stuff needs to be arranged i didn't read your code but i don't see any arranging function in it
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: thrashing
Next Thread in C Forum Timeline: linking loader





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC