| | |
Loops in C...help needed!!
![]() |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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
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
0
#2 Oct 19th, 2009
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
0
#3 Oct 19th, 2009
•
•
•
•
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.
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
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
-1
#4 Oct 19th, 2009
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.
#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.
1
#6 Oct 19th, 2009
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?
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Animation in pascal (Pascal and Delphi)
- Alien numbers problem (C)
- vrooom vroooom... help me speed up my app... (C#)
- backgroundworker slower process? (C++)
- Help with adding 2 arrays of different sizes (C++)
- arrays in java!!! (Java)
- Help please in STRING (C++)
- Some assistance needed (Pascal and Delphi)
- Help with while, do while, and for loops (C++)
Other Threads in the C Forum
- Previous Thread: thrashing
- Next Thread: linking loader
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






