Computer Science Canada

Binary Search

Author:  oscarwu1991 [ Fri Nov 09, 2007 2:06 pm ]
Post subject:  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

Author:  Euphoracle [ Fri Nov 09, 2007 3:25 pm ]
Post subject:  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?

Author:  Tony [ Fri Nov 09, 2007 3:28 pm ]
Post subject:  RE:Binary Search

sure, what do you need help with? Reading from a file? Sorting? Searching?

Author:  oscarwu1991 [ Sat Nov 10, 2007 1:18 pm ]
Post subject:  Re: Binary Search

my teacher didnt show me how to do it
and now i have no clue how
so...

Author:  Tony [ Sat Nov 10, 2007 3:09 pm ]
Post subject:  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 Wikipedia

Author:  oscarwu1991 [ Sat Nov 10, 2007 3:55 pm ]
Post subject:  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

Author:  oscarwu1991 [ Sat Nov 10, 2007 4:14 pm ]
Post subject:  Re: Binary Search

code:
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]

Author:  RLien [ Sat Nov 10, 2007 5:35 pm ]
Post subject:  RE:Binary Search

umm weird question, are you oscar from mr saigals class because i have a project exactly like that too

Author:  oscarwu1991 [ Sat Nov 10, 2007 5:59 pm ]
Post subject:  Re: Binary Search

someone help

Author:  Tony [ Sat Nov 10, 2007 6:21 pm ]
Post subject:  RE:Binary Search

code:

for k:1..1
...
for i : 1 .. 1

Is it really a loop, if the block gets executed just once? Wink

Also
code:

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!)

Author:  oscarwu1991 [ Sat Nov 10, 2007 10:24 pm ]
Post subject:  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?

Author:  Tony [ Sat Nov 10, 2007 10:28 pm ]
Post subject:  RE:Binary Search

You can start by fixing your sort procedure.

when you do
code:

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.

Author:  oscarwu1991 [ Sat Nov 10, 2007 11:52 pm ]
Post subject:  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

Author:  Tony [ Sun Nov 11, 2007 4:25 am ]
Post subject:  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.

Author:  oscarwu1991 [ Sun Nov 11, 2007 12:55 pm ]
Post subject:  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

Author:  Tony [ Sun Nov 11, 2007 4:20 pm ]
Post subject:  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.

Author:  oscarwu1991 [ Sun Nov 11, 2007 7:01 pm ]
Post subject:  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

Author:  Tony [ Sun Nov 11, 2007 8:34 pm ]
Post subject:  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]

Author:  oscarwu1991 [ Sun Nov 11, 2007 9:47 pm ]
Post subject:  RE:Binary Search

I still dont get it
can u make it simple and put it into my code??
thanks a lot

Author:  Mazer [ Sun Nov 11, 2007 10:00 pm ]
Post subject:  Re: RE:Binary Search

oscarwu1991 @ Sun Nov 11, 2007 9:47 pm wrote:
I still dont get it

What exactly don't you get?

Quote:
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.


: