Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Searchable Databases
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Token




PostPosted: Fri May 06, 2005 8:06 pm   Post subject: Searchable Databases

Databases and how to search through them

The first thing that you want to do is create your database, so we''ll set up a record. Example we'll use is an adress book

code:

type phonerecord :
    record
        first : string
        last: string
        home : string (8)
        cell : string (8)
        fax : string (11)
    end record

var entry : array 1 .. 50 of phonerecord


okay now we're all set to gather the information. so we will assume that we already have a file set up with a couple of entries in this format
first name, last name, home number, cell number, fax number.

code:

Joe, Smith, 555-1234, 555-4321,  44-171-343433


Okay so now we'll input all of this information into our reccord through a loop.

code:

var stream, counter : int := 0 %%%%% :=0 is to error proof so it dosent crash at +=
var data : string
open : stream, "archive.txt", get

loop
    counter += 1
    exit when eof (stream)

    get : stream, data
    entry (counter).first := data
    get : stream, data
    entry (counter).last := data
    get : stream, data
    entry (counter).home := data
    get : stream, data
    entry (counter).cell := data
    get : stream, data
    entry (counter).fax := data

end loop
close : stream


Okay, so our system is now created and the data is entered. so now what we have to do is find the name of who their searching for. we'll make this search by last name only.

code:

var search : string
put "Enter the persons last name: " ..
get search


Alright, so now we have our search, in this case we're searching for Smith...so we run the variable 'search' through the entire record and compare it to the other last names, if its the same then we display it on the screen


code:

for i : 1 .. counter

    if entry (i).last = search then

        put "\nFirst name: ", entry (i).first
        put "Last  name: ", entry (i).last
        put "Home: ", entry (i).home
        put "Cell: ", entry (i).cell
        put "Fax: ", entry (i).fax
    end if

end for


Tadum! and then it pops up with the results that matched, you can change it to search for first name or number or somthing just by changing where it says

code:

 if entry (i).last = search then


so there you have it, a searchable database, simple and easy to add and modify.

all that code combined is below, along with what would be in the text file, 'archive.txt'

code:



%%%

type phonerecord :
    record
        first : string
        last : string
        home : string
        cell : string
        fax : string
    end record

var entry : array 1 .. 50 of phonerecord

%%%

var stream, counter : int := 0 %%%%% :=0 is to error proof so it dosent crash at +=
var data : string
open : stream, "archive.txt", get

loop

    exit when eof (stream)
    counter += 1
    get : stream, data
    entry (counter).first := data
    get : stream, data
    entry (counter).last := data
    get : stream, data
    entry (counter).home := data
    get : stream, data
    entry (counter).cell := data
    get : stream, data
    entry (counter).fax := data

end loop
close : stream

%%%

var search : string
put "Enter the persons last name: " ..
get search

for i : 1 .. counter

    if entry (i).last = search then

        put "\nFirst name: ", entry (i).first
        put "Last  name: ", entry (i).last
        put "Home: ", entry (i).home
        put "Cell: ", entry (i).cell
        put "Fax: ", entry (i).fax
    end if

end for


Quote:

Joe Smith 555-1234 555-4321 44-171-343433



if you have any questions or comments please let me know.
Sponsor
Sponsor
Sponsor
sponsor
jamonathin




PostPosted: Fri May 06, 2005 8:44 pm   Post subject: (No subject)

Good tutorial. Our teacher was actually trying to teach us that this year. Even though that was the only thing, and he did it some wierd ass complete different way . . . nvm, lol, but yeah good work on that tut.

And im not too sure why you did this
code:
put "\nFirst name: ", entry (i).first

Im pretty sure that for C++, but anyways, good job.

+ 10 Bits

Next to work on is the option of playing around with that data. Such as (D)elete or (W)rite and so on.
GlobeTrotter




PostPosted: Fri May 06, 2005 9:38 pm   Post subject: (No subject)

You could also do a binary search for this problem. It would require sorting the array of records, but the actual searching would be much quicker. FOr example, in an array of 100 names, you way would run through 100 times. Binary Search would run through 7.
Token




PostPosted: Sat May 07, 2005 8:14 pm   Post subject: (No subject)

Hey, well i used put "\nFirst name: ", entry (i).first to display the name, the \n is to set it down a line without just doing a blank put statement (put ""). and could you please explain to me more about this binary thing?

ps. thanks for the bits jamonathin
GlobeTrotter




PostPosted: Sat May 07, 2005 11:09 pm   Post subject: (No subject)

I'll describe a case which used binary search.
Say I ask you to guess a number between 1 and 100. If after each guess, I tell you whether the guess is higher or lower, you are guaranteed to get it within 7 guesses if you make the right guesses. For example, if you picked the number 13: First you'd guess 50, then 25, then 13. In three guesses.

Basically, you do the same thing with a sorted array of records. You go the midway point of the left and right vals, and check if its higher, lower, or equal. If different, you reset the left and right vals.
md




PostPosted: Sun May 08, 2005 8:42 am   Post subject: (No subject)

You have to make sure that whatever your searching is sorted first, generally if your doing a binary search you quicksort first.
Token




PostPosted: Sun May 08, 2005 3:25 pm   Post subject: (No subject)

Hmm inturesting, this would work better for a larger database tho, with phone numbers, really you would only have a maximum of 50-75 contacts, so time isnt really of the essence, but if you were sorting information for say the school's contact information, you would want to do it in a binary sort of fashion. thanks for that info guys and i might add onto the tut. and put it in there.
Weelkid_29




PostPosted: Mon Jan 23, 2006 7:02 pm   Post subject: a different search mothod

code:

type seat :
    record
        lastname : string
        firstname : string
        address : string
        phonenum : string
    end record
var seat_record : array 1.. 10 of seat
for i : 1 .. 10
    put "enter ur last name " ..
    get seat_record (i).lastname
    put "enter ur first name " ..
    get seat_record (i).firstname
    put "enter ur address "
    get seat_record (i).address : *
    put "enter ur phone number "
    get seat_record (i).phonenum
end for
var lastname : string
put "Enter the last name"
get lastname
var flag : int := 0
for a : 1 .. 10
    if lastname = seat_record (a).lastname then
        flag := 1
        put "record found"
        put seat_record (a).lastname
        put seat_record (a).firstname
        put seat_record (a).address
        put seat_record (a).phonenum
    else
    end if
end for
if flag = 0 then
    put "record not found"
end if

enter all the info for the record
then enter the last name for the info u want Wink
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: