
-----------------------------------
oscarwu1991
Fri Nov 09, 2007 2:06 pm

Binary Search
-----------------------------------
I need to make a program which is binary search
and that allows you to load the datafile 
search populations and flight info
can someone help me with it?
really important
thanks

-----------------------------------
Euphoracle
Fri Nov 09, 2007 3:25 pm

RE:Binary Search
-----------------------------------
You should really attempt it yourself before asking for help.  If this is for school, your teacher should have outlined the parameters you need to follow, shouldn't be that hard to at least get a trial program coded, right?

-----------------------------------
Tony
Fri Nov 09, 2007 3:28 pm

RE:Binary Search
-----------------------------------
sure, what do you need help with? Reading from a file? Sorting? Searching?

-----------------------------------
oscarwu1991
Sat Nov 10, 2007 1:18 pm

Re: Binary Search
-----------------------------------
my teacher didnt show me how to do it
and now i have no clue how
so...

-----------------------------------
Tony
Sat Nov 10, 2007 3:09 pm

RE:Binary Search
-----------------------------------
Once again, you have to be more specific. Or ask your teacher to show you.. I can only assume your teacher would know more about what's expected.

Anyway, all the binary search theory can be read about at [url=http://en.wikipedia.org/wiki/Binary_search]Wikipedia

-----------------------------------
oscarwu1991
Sat Nov 10, 2007 3:55 pm

Re: Binary Search
-----------------------------------
Sorry, but we have misplaced your code. The only way to safeguard your code is to place it in code tags. Sorry for the inconvienience.


%here is my code
but i dont know how to output the right answer when i input the correct name
i cannot use if condition
thanks

-----------------------------------
oscarwu1991
Sat Nov 10, 2007 4:14 pm

Re: Binary Search
-----------------------------------
var datafile:int
var population:array 1..11 of int
var country:array 1..11 of string
var sort:array 1..11 of int
var datafile1:int
proc hskg
    for i : 1 .. 1
        population(i):=0
        get country(i)
    end for
end hskg

proc data
    open : datafile, "data8.3.t", get
   
    for i : 1 .. 11

        get : datafile,population(i),country(i)
        
    end for
end data
proc data1
open : datafile1, "data8.2.t",get
for i:1..11
get:datafile1, population(i)
end for
end data1
proc load1
    for i : 1 .. 11
        sort (i) := population (i)
    end for
end load1
proc bubble
var flag:string(1):="N"
var temp:int
loop 
flag:="N"
for k:1..1
if sort (k) = sort (k+1) then
temp:=sort(k)
sort(k):=sort (k+1)
sort(k+1):=temp
flag:="Y"
end if
end for
if flag="N" then
exit
end if
end loop
end bubble

proc out
    for i : 1 .. 1
        put population(i)
    end for
end out

%mainline
hskg
data
data1
load1
bubble
out

here is my code
pls help
but i dont know how to output the right answer when i input the correct name 
i cannot use if condition 
thanks[/quote]

-----------------------------------
RLien
Sat Nov 10, 2007 5:35 pm

RE:Binary Search
-----------------------------------
umm weird question, are you oscar from mr saigals class because i have a project exactly like that too

-----------------------------------
oscarwu1991
Sat Nov 10, 2007 5:59 pm

Re: Binary Search
-----------------------------------
someone help

-----------------------------------
Tony
Sat Nov 10, 2007 6:21 pm

RE:Binary Search
-----------------------------------

for k:1..1
...
for i : 1 .. 1

Is it really a loop, if the block gets executed just once? ;)

Also

if sort (k) = sort (k+1) then 

Are you looking for the same values in a set? Probably not, because you swap them afterwards (why, they are the same!)

-----------------------------------
oscarwu1991
Sat Nov 10, 2007 10:24 pm

Re: Binary Search
-----------------------------------
but what should i do to complete my program?
i want to:
when i enter canada
it outputs the correct number
when i enter china
it outputs the correct number
how can i do that?

-----------------------------------
Tony
Sat Nov 10, 2007 10:28 pm

RE:Binary Search
-----------------------------------
You can start by fixing your sort procedure.

when you do

proc out
    for i : 1 .. 11
        put population(i)
        put country(i)
    end for
end out 


Make sure the numbers are put in the right order.

Also make sure that the countries have their correct population.

-----------------------------------
oscarwu1991
Sat Nov 10, 2007 11:52 pm

Re: Binary Search
-----------------------------------
no i mean
when i input china
it shows the population of china
and then cls
input other country again
output the correct population 
only output one number

-----------------------------------
Tony
Sun Nov 11, 2007 4:25 am

RE:Binary Search
-----------------------------------
well you will need an array with two pieces of data -- country name, and population. This way when you figure out the index for one, it will be the same index for another.

-----------------------------------
oscarwu1991
Sun Nov 11, 2007 12:55 pm

Re: Binary Search
-----------------------------------
I have 2 data file
one is for population 
and the other is for country
can you tell me what to do to collect two parts?
i have two arrays already

-----------------------------------
Tony
Sun Nov 11, 2007 4:20 pm

RE:Binary Search
-----------------------------------
I'm kind of wondering.. what's with the arbitrary line breaks, in the middle of your sentences?

You just read both files. If names and numbers in the files are sorted in the same order, then your arrays will share the same indexes. That is, country[n] will have a population of population[n] people.

-----------------------------------
oscarwu1991
Sun Nov 11, 2007 7:01 pm

Re: Binary Search
-----------------------------------
I dont get what you mean?
can you show me in codes?
or can you give me more details, pls?
thanks a lot

-----------------------------------
Tony
Sun Nov 11, 2007 8:34 pm

RE:Binary Search
-----------------------------------
eh... sure.

Given data1: country1, country2, ... , contryN
and data2: population1, population2, ... , populationN

load data in two arrays, such that array_country[i] == country[i]

and array_population[i] == population[i]

then given a country named "foo", you can search the array of names for a match, to locate its index.

if "foo" == array_country[x] then
   - population of "foo" is array_population[x]

-----------------------------------
oscarwu1991
Sun Nov 11, 2007 9:47 pm

RE:Binary Search
-----------------------------------
I still dont get it
can u make it simple and put it into my code??
thanks a lot

-----------------------------------
Mazer
Sun Nov 11, 2007 10:00 pm

Re: RE:Binary Search
-----------------------------------
I still dont get it
What exactly don't you get?

can u make it simple and put it into my code??
thanks a lot
Not likely. The idea is to help you figure out how to do it. So help us help you.
