Computer Science Canada

[Tutorial] Records

Author:  Andy [ Tue Nov 25, 2003 1:10 pm ]
Post subject:  [Tutorial] Records

A record is a structure of data with the same name but different subscripts. Structures are not a necessity for any program, but it will make your source look much more pretty.

If we were to create a database on each student in the school, we may need several arrays:
code:

var firstname :array 1.. 1400 of string
var lastname :array  1..1400 of string
var height: array 1..1400 of int
var age: array 1..1400 of int
var grade: array 1..1400 of int
var average: array 1..1400 of int
var haircolor: array 1..1400 of int
var gender: array 1..1400 of string
var eyecolor: array 1..1400 of string


as you can see, this is very annoying, not mentioning confusing if u were to access the info on a student

however, with records, this can be very easy
code:

type studentData:
    record
        firstname: string
        lastname: string
        height: int
        age: int
        grade:int
        average:int
        haircolor:int
        gender: string
        eyecolor: string
    end record


now that student Data is a type, you can declare an array of it

code:

var student : array 1..1400 of studentData


to call up each elements of the structure

code:

student(1).age:=15


another great thing about records is that you can copy an entire record into another for example

code:

student(1):=student(2)


the structure of student(1) will now be the same as student(2)

well thats about it.

Author:  Dan [ Tue Nov 25, 2003 5:13 pm ]
Post subject: 

thanks for the tutorial and i know have been whonting some bits so i gusse i will give you some for this Razz better then spamming Rolling Eyes

Author:  AsianSensation [ Tue Nov 25, 2003 6:20 pm ]
Post subject: 

wow, dodge not spamming? Wait....I just saw a pig go flying across my window, wow, these things actually do happen.

anyways, always try to use records. Unless you are so good that you won't ever get confused with codes and what they do, and can handle large multi-dimensional arrays with ease, then use records, they are great for organization.

Author:  Andy [ Tue Nov 25, 2003 7:01 pm ]
Post subject: 

lol, dan got pissed, so i got scared... unlike some other disrespectful lil kids here... recursion is nxt

Author:  Mazer [ Tue Nov 25, 2003 9:49 pm ]
Post subject: 

nice tutorial. you can't tell, but i'm clapping right now. here, have some bits Wink

Author:  Atma Weapon [ Mon Dec 08, 2003 1:40 am ]
Post subject: 

This tutorial has been extremely helpful. Take all my bits, with my thanks,

Author:  junkpro11 [ Sun May 09, 2004 10:42 pm ]
Post subject: 

one question....if i want to store the input from the user for each category, like firstname i would use

get firstname

student(1).firstname:=firstname

is this correct?

if i want to make this into like a database, which allows users to create records....... do i use array 1..100 of studentdata (eg 100 students)?
how do i write each student file according to student number....like 10023.t or 15246.t etc

Author:  AsianSensation [ Mon May 10, 2004 2:22 pm ]
Post subject: 

code:
type StudentType :
    record
        FirstName : string
    end record

var database : array 1 .. 100 of StudentType

for rep : 1 .. 100
    get database (rep).FirstName
end for


use intstr to manipulate strings of the file name if you want a specific text file to be the student number of that student.

Author:  junkpro11 [ Mon May 10, 2004 10:06 pm ]
Post subject: 

code:

open: stremout, intstr(istudentnum).t, put


an error pops up saying "expression cannot be followed by a '.' "

Author:  Tony [ Mon May 10, 2004 10:56 pm ]
Post subject: 

i'd assume it's suppost to be
code:

intstr(istudentnum)+".t"

Author:  lee_Amilghty [ Wed May 26, 2004 10:14 am ]
Post subject:  how to pass the record to procedure to procedure

Shocked

since the record is made up by array, how can i declare the variable in the procedure?

i wanna the record can be used in a wider range?

need help!!!!

~.~ Smile

Author:  AsianSensation [ Wed May 26, 2004 7:32 pm ]
Post subject:  Re: how to pass the record to procedure to procedure

lee_Amilghty wrote:
since the record is made up by array, how can i declare the variable in the procedure?

i wanna the record can be used in a wider range?


no, records are not made up of arrays. You can have an array of records, but doesn't mean it's made up of arrays.

secondly, what do you mean using records in a wider range?

if you want to pass record from procedure to procedure, it's best to just declare a global record, and have procedures and functions that changes it. That would be the easiest way.

Author:  Omicron91 [ Mon Dec 20, 2004 5:54 pm ]
Post subject: 

Weird, I was just takign a break from making a program that used types/records, to bad I didn't see this about three hours ago, oh well, it worked out. Not done though.

code:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
type Seat :
    record
        Price : real
        Occupied : boolean
        Number : int
        Letter : string
        Name : string
        Select : boolean
    end record
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var Auditorium : array 1 .. 20, 1 .. 25 of Seat
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var Chair : int := Pic.FileNew ("Chair.jpg")
var DarkChair : int := Pic.FileNew ("DarkChair.jpg")
var SoldChair : int := Pic.FileNew ("SoldChair.jpg")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var DisplayWin : int := Window.Open ("position:top;center,graphics:330;530,nobuttonbar,offscreenonly")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var font1 : int := Font.New ("Courier New:10")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var tempString : string
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var SaveFile : int
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure DrawAuditorium
    for x : 1 .. 20
        for y : 1 .. 25
            if Auditorium (x, y).Occupied = false then
                Pic.Draw (DarkChair, x * 15, y * 20, picMerge)
            elsif Auditorium (x, y).Occupied = true then
                Pic.Draw (SoldChair, x * 15, y * 20, picMerge)
            end if
        end for
    end for
    View.Update
end DrawAuditorium
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
process SaveToDisk
    open : SaveFile, "Backup.txt", put
    loop
        for x : 1 .. 20
            for y : 1 .. 25
                put : SaveFile, "Price : ", Auditorium (x, y).Price, " | Occupied : ", Auditorium (x, y).Occupied, " | Letter : ", Auditorium (x, y).Letter, " | Ticket Holder : ", Auditorium (x,
                    y).Name, " | Number : ", Auditorium (x, y).Number
            end for
        end for
        exit
    end loop
end SaveToDisk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for x : 1 .. 20
    for y : 1 .. 25
        Auditorium (x, y).Price := 19.99
        Auditorium (x, y).Occupied := false
        Auditorium (x, y).Number := y
        Auditorium (x, y).Letter := chr (x + 64)
        Auditorium (x, y).Name := "N/A"
        Auditorium (x, y).Select := false
    end for
end for
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DrawAuditorium
Input.Pause
fork SaveToDisk

Author:  wtd [ Mon Dec 20, 2004 7:10 pm ]
Post subject:  Re: how to pass the record to procedure to procedure

AsianSensation wrote:
if you want to pass record from procedure to procedure, it's best to just declare a global record, and have procedures and functions that changes it. That would be the easiest way.


No, it wouldn't.

This is never a good idea.

Author:  Andy [ Mon Dec 20, 2004 7:33 pm ]
Post subject: 

i think he means it would be easier

Author:  wtd [ Mon Dec 20, 2004 7:43 pm ]
Post subject: 

dodge_tomahawk wrote:
i think he means it would be easier


No, it isn't.

It makes it much harder to make adjustments to a program.

Author:  Andy [ Tue Dec 21, 2004 2:47 pm ]
Post subject: 

true, but much easier to program it rite the first time

Author:  wtd [ Tue Dec 21, 2004 3:48 pm ]
Post subject: 

dodge_tomahawk wrote:
true, but much easier to program it rite the first time


Doing things the wrong way is not a path to doing things the right way.

It just reinforces bad habits, and makes it ultimately harder to adopt a correct approach to programming.

Author:  Andy [ Tue Dec 21, 2004 3:53 pm ]
Post subject: 

its hard to start nailing oop if they have no basic tho...

Author:  wtd [ Tue Dec 21, 2004 4:04 pm ]
Post subject: 

Object-oriented programming isn't even the issue here. Good, structured programming is. Before students even begin to think about object-oriented programming they have to understand the concept of an interface... the proverbial "black box".

What you're suggesting is the roots of "spaghetti code".

Author:  Andy [ Tue Dec 21, 2004 4:27 pm ]
Post subject: 

no what im trying to say is.. they may not understand the importance of passing it through as a parameter, and resulting the object instead of just making it global...

Author:  wtd [ Tue Dec 21, 2004 4:43 pm ]
Post subject: 

So make them learn a bunch of bad habits so they can see the beauty of doing it the right way?

I can get behind that, except that CS classes move slowly enough already, and it takes time to unlearn all of the bad habits.

Author:  gnarky [ Thu Mar 17, 2005 9:47 am ]
Post subject: 

Ok im pretty new at this.
code:
type userData:
    record
        Username: string
        Password: string
    end record

    var user : array 1..1400 of userData
    var username:string
    get username   
    user(1).Username:=username
   

var temp : string := "temp.txt"
var fileNo : int
open : fileNo, temp, put
put : fileNo, userData
close : fileNo


A couple questions:

It says that expression is not a variable when I try to run it. What does that mean? It highlights the second last line.

Also, how do I make it so that a user inputs his/her own data without writing over someone else's.

Author:  Andy [ Thu Mar 17, 2005 4:07 pm ]
Post subject: 

userData is a type... you stilll have to declare a variable like var user:userData

Author:  Cervantes [ Thu Mar 17, 2005 4:14 pm ]
Post subject: 

Or you could go
code:

var userData :
   record
       %lots of stuff
   end record


Depending on what you want to do with userData, that may or may not be better.
Also, when you say user inputs his/her own data... do you mean on screen, or to a file, or something else?

Author:  gnarky [ Thu Mar 17, 2005 5:51 pm ]
Post subject: 

ok...Ive got what I want to do...But I know, somehow, someway, there's an easier was to do this. Something with arrays would probably make it easier. Hope you can help me out.

code:
type userData :
    record
        Username : string
        Password : string
        Lines : string
    end record

var user : array 1 .. 1400 of userData

var username, password, confirm : string
var lines := "--------------"

loop
    put "Enter desired username: " ..
    get username
    user (1).Username := username
    cls
    put "Desired password: " ..
    get password
    user (1).Password := password
    user (1).Lines := lines

    var temp : string := "temp.txt"
    var stream : int
    open : stream, temp, put
    put : stream, user (1).Lines
    put : stream, user (1).Username
    put : stream, user (1).Password
    put : stream, user (1).Lines
    close : stream

    cls
    put "-------------"
    put "Username: " ..
    put user (1).Username
    put "Password: " ..
    put user (1).Password
    put "-------------"
    put "Confirm? (Y/N)"
    get confirm
    if confirm = "y" then
        exit
    else
        cls
        File.Copy ("blank.txt", "temp.txt")
        put "Data erased."
    end if
end loop
cls
put "Data successful entered into the database"
cls

loop
    put "Enter desired username: " ..
    get username
    user (2).Username := username
    cls
    put "Desired password: " ..
    get password
    user (2).Password := password
    user (2).Lines := lines

    var temp : string := "temp2.txt"
    var stream : int
    open : stream, temp, put
    put : stream, user (2).Lines
    put : stream, user (2).Username
    put : stream, user (2).Password
    put : stream, user (2).Lines
    close : stream

    cls
    put "-------------"
    put "Username: " ..
    put user (2).Username
    put "Password: " ..
    put user (2).Password
    put "-------------"
    put "Confirm? (Y/N)"
    get confirm
    if confirm = "y" then
        exit
    else
        cls
        File.Copy ("blank.txt", "temp.txt")
        put "Data erased."
    end if
end loop
cls
put "Data successful entered into the database"
cls

var stream, stream2 : int

open : stream, "temp.txt", get
assert stream > 0

loop
    exit when eof (stream)
    get : stream, skip
    get : stream, user(1).Lines
    get : stream, user(1).Username
    get : stream, user(1).Password
    get : stream, user(1).Lines
end loop
close : stream

open : stream, "temp2.txt", get
assert stream > 0

loop
    exit when eof (stream)
    get : stream, skip
    get : stream, user(2).Lines
    get : stream, user(2).Username
    get : stream, user(2).Password
    get : stream, user(2).Lines
end loop
close : stream

open : stream, "data.txt", put
assert stream > 0

    put : stream, user (1).Lines
    put : stream, user (1).Username
    put : stream, user (1).Password
    put : stream, user (1).Lines
    put : stream, user (2).Lines
    put : stream, user (2).Username
    put : stream, user (2).Password
    put : stream, user (2).Lines
close : stream

Author:  Cervantes [ Thu Mar 17, 2005 7:47 pm ]
Post subject: 

Just use one file, and use a for loop to get information for theentire array. Also, I would suggest using a flexible array here. Are you certain that there are 1400 users? Does that number ever change? If it does, you'll want to use a flexible array. Also, by using a flexible array, you could create a "Register" button, and when it's clicked, a new user is created. You could also make a "Delete Account" button.

Author:  atrain [ Fri Mar 18, 2005 12:48 am ]
Post subject: 

Wow thanx so much for this tutorial...

had i read it before, it could have helped me quite a bit...
i only ended up with 3 arrays though....

Author:  Drake [ Wed Jun 22, 2005 3:56 pm ]
Post subject: 

How can I get one record to extend another record? For example, I'm making an RPG and I want to make a Monster record with the names of every monster in it. And I want each of those names to extend to another record so that I'm only using two records for the entire list. Is it possible to have this happen? I thought it would be something like this:
code:
var Stats :
    record
        Health : int
        Mana : int
        AttackMax : int
        AttackMod : int
        Defense : int
    end record

var Monster :
    record
        Druid : Stats
        Dog : Stats
        Rat : Stats
        Snake : Stats
        Zombie : Stats
    end record
But that doesn't seem to work quite right. Especially when I try:
code:
Monster.Druid.Health := 25

Is it even possible to do this in as simple or a more simpler fashion?

Author:  Cervantes [ Wed Jun 22, 2005 4:19 pm ]
Post subject: 

Good question. It's possible. But let's look at what you've got first:
You've made a variable that contains fields for health and mana etc. Then, you've made a variable for several monsters, with fields whose typeSpec is a variable. This is not allowed; we want the typeSpec to be a type. So, change the first line from
code:
var Stats :

to
code:
type Stats :

Should work nicely now. Smile

The other thing to think about is that you don't want to have to make variables for all the enemies in the game. A better way than what you've got there follows:
code:

var Monster : array 1 .. 10 of
    record
        Name : string
        Health : int
        Mana : int
        AttackMax : int
        AttackMod : int
        Defense : int
    end record

But, what would be even better is reading data in from a file, and using flexible arrays. I don't know how this RPG will work, but if it were to be a complex graphical RPG, I suggest making groups of monsters that are sorted by the map/level in which they appear and then by their x and y coordinates on the map. Then the individual monsters have their starting coordinates within the group.

Author:  Drake [ Wed Jun 22, 2005 5:01 pm ]
Post subject:  Thanks! I'll use them both.

Using the array, although not what I want to use, would probably work better for me. This would be due to the fact that random monsters will be poping up instead of pre-placed monsters. If I use the array then I can just get a random number for the monsters and be done with it. Or if I want to pick a specific monster just choose that number.

Now all I have to do is to figure out how to do the darn battle system...


: