having trouble entering more than one entire into a record i need to something similar to a flexible array
Author |
Message |
dog44
|
Posted: Fri Jan 04, 2013 10:46 pm Post subject: having trouble entering more than one entire into a record i need to something similar to a flexible array |
|
|
What is it you are trying to achieve?
I am trying to make a marks manager program that allows the user to enter multiple marks for that person and then calculates their average. The amount of marks to be entered are unknown and the user must be able to add new entries if need be. All this info is going inside a file by the way
What is the problem you are having?
Since i am using records for the student names and sub records for the marks averages and comments(i need these three to associated with the person) i need to be able to add multiple marks for that person
within the marks array and i am unsure how to accomplish this without "cheating"(learning pointers or classes to be exact as we have not learned this yet i figured it would be unfair)
Describe what you have tried to solve this problem
the first thing i tried was associating the marks record with a flexible array i soon found out there is almost no possible way to actually do that since its not implemented although i did try changing were the array was within or on the type
i also tried changing the type to a var turns out that it doesn't let you put records under that.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
%this is what i started off with this works but is not what i want because how am i supposed to add multiple entries to marks that way
type students :
record
name : string
stats :
record
mark : int
average : real
comments : string
end record
%this was my attempt at fixing it of course i ran face first into a syntax error
type students :
record
mark : flexible array 1 .. 0 of int
average : real
comments : string
end record
% another thing i tried but unfortunately the same problem hits me "how am i supposed to get marks to hold multiple mark entries" also item refers to what the mark is for ie. a test.
type students :
record
name : string
stats :
record
item : string
mark : int
average : real
comments : string
end record
%this is all my code if anyone's interested to see the entire thing
%summative program
%By:
%marks mannager program
%VARS
var name, filename : string := ""
var choice, file : int
%Record and fields
type students :
record
name : string
stats :
record
item : string
mark : int
average : real
comments : string
end record
%FONT SIZES
var font1, font2, font3, font4, font5 : int
font1 := Font.New ("Rockwell:12")
font2 := Font.New ("Rockwell:10")
font3 := Font.New ("Rockwell:25")
font4 := Font.New ("Rockwell:18")
%vars used for mouse
var x, y, btnnumber, btnupdown, buttons : int
%PROCEDURES AND FUNCTIONS -------------------------------
proc creat (input : string, var output : int)
filename + = input + ".txt"
open : file, filename, put
assert file > 0
loop
put "please enter the frist name and the last name of the student"
end loop
close : file
end creat
%--------------------------------------------------------
%INTRO SCREEN---------------------------------------------
loop
drawfillbox (0, 0, 640, 480, 32)
Font.Draw ("Marks Mannager Program", 125, 350, font3, 11)
drawfillbox (170, 15, 500, 45, green)
Font.Draw ("click me to continue", 180, 20, font3, 11)
Mouse.ButtonWait ("down", x, y, btnupdown, buttons )
if buttons = 1 then
if x > 170 and x < 500 and y > 15 and y < 45 then
exit
end if
end if
end loop
%--------------------------------------------------------
%MAIN MENU/MAIN PROGRAM --------------------------------------------------
loop
choice := 0
x := 0
y := 0
%this drawfill is for the back ground
drawfillbox (0, 0, 640, 480, 32)
%title of screen
Font.Draw ("Marks Manager Program", 180, 380, font4, 11)
Font.Draw ("mainscreen", 250, 358, font4, 11)
%creat file button
drawfillbox (5, 330, 180, 370, gray)
Font.Draw ("Creat file", 20, 340, font3, black)
%add to file button
drawfillbox (5, 280, 200, 315, grey)
Font.Draw ("Add entries", 12, 285, font3, black)
%delete records button
drawfillbox (5, 220, 230, 260, grey)
Font.Draw ("Delete entries", 10, 230, font3, black)
%edit file button
drawfillbox (5, 160, 250, 200, grey)
Font.Draw ("Edit file entries", 8, 170, font3, black)
% Display a record button
drawfillbox (5, 100, 270, 140, grey)
Font.Draw ("Display a record", 8, 110, font3, black)
%display an entire file button
drawfillbox (5, 40, 320, 80, grey)
Font.Draw ("Display file contents", 8, 50, font3, black)
%shearch a file for a field button
drawfillbox (390, 300, 600, 340, grey)
Font.Draw ("search a file", 400, 310, font3, black)
%sort a file button
drawfillbox (390, 240, 550, 280, grey)
Font.Draw ("Sort a file", 400, 250, font3, black)
%exit button
drawfillbox (545, 10, 615, 50, grey)
Font.Draw ("exit", 550, 20, font3, black)
Mouse.ButtonWait ("down", x, y, btnupdown, buttons )
if x > 5 and x < 180 and y > 330 and y < 370 then
drawfillbox (0, 0, 640, 480, 32)
%title of page
Font.Draw ("Creat new file", 180, 370, font3, 11)
%instructons
Font.Draw ("Please enter the name of the file you are creating.", 20, 350, font4, 11)
%this button will exit the program completly
drawfillbox (5, 10, 160, 40, grey)
Font.Draw ("Exit program", 10, 20, font4, black)
%this button will return the user back to the main menu
drawfillbox (170, 10, 410, 40, grey)
Font.Draw ("Return to main menu", 175, 20, font4, black)
%this button will alow them to creat another file
drawfillbox (420, 10, 630, 40, grey)
Font.Draw ("Creat another file", 430, 20, font4, black)
drawfillbox (21, 302, 400, 321, white)
locatexy (25, 310)
get name : *
creat (name, choice )
exit
elsif x > 5 and x < 200 and y > 280 and y < 315 then
cls
put "add to file"
exit
elsif x > 5 and x < 230 and y > 220 and y < 260 then
cls
put "delete records"
exit
elsif x > 5 and x < 250 and y > 160 and y < 200 then
cls
put "edit file entries"
exit
elsif x > 5 and x < 270 and y > 100 and y < 140 then
cls
put "display a record"
exit
elsif x > 5 and x < 320 and y > 40 and y < 80 then
cls
put "display an entire file"
exit
elsif x > 390 and x < 600 and y > 300 and y < 340 then
cls
put " shearch inside a file"
exit
elsif x > 390 and x < 550 and y > 240 and y < 280 then
cls
put "sort button"
exit
elsif x > 545 and x < 615 and y > 10 and y < 50 then
cls
put "exit"
exit
end if
end loop
|
Please specify what version of Turing you are using
truing 4.1.1
any help or suggestions about how i should change my coding style will be greatly appreciated |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Sat Jan 05, 2013 12:46 am Post subject: RE:having trouble entering more than one entire into a record i need to something similar to a flexible array |
|
|
To get around the restriction on flexible array in record, you could cheat a bit and use a string instead. Since a mark can't be higher than 100, save every mark as 3 characters in a string. Say a student had 3 tests and got 75, 87, and 100 on them. The mark string would look like "075087100". To retrieve a mark from the string, you would use something like 'strint (tests(test_number*3, test_number*3+2))'. To add a mark, just use intstr() to convert to a string, and add that to the mark string.
There's probably a better way, but this is what I came up with off the top of my head. |
|
|
|
|
|
Tony
|
Posted: Sat Jan 05, 2013 12:59 am Post subject: Re: RE:having trouble entering more than one entire into a record i need to something similar to a flexible array |
|
|
Insectoid @ Sat Jan 05, 2013 12:46 am wrote: To get around the restriction on flexible array in record, you could cheat a bit and use a string instead.
Turing Strings have an upper bound of 255 characters. It would be better to just use a fixed (but large) sized array inside of the record.
The problem is that implementation of records requires them to be of a known fixed size, at compile time. That is, size of record for student A should also have enough space to be replaced with a record for student B. Having a flexible array grow in size from the inside, would obviously cause problems here.
So yes, you would need
dog44 @ Fri Jan 04, 2013 10:46 pm wrote: learning pointers or classes to be exact as we have not learned this yet i figured it would be unfair
at least in some capacity.
What I mean is that you have to store the actual marks outside of the record, but there are a number of ways of getting the two associated. Besides a pointer to a memory address, it could also be something as simple as an array index (which... is really implemented as a pointer, if you look deep enough). |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
dog44
|
Posted: Sat Jan 05, 2013 11:33 am Post subject: Re: having trouble entering more than one entire into a record i need to something similar to a flexible array |
|
|
hmmmm both your ideas sound like they can work with some limitations the 255 characters one with strings obviously. as for the giant array record i don't know if my teacher would approve of that methodology. Another problem would be the user has to be able to edit all those entries so i don't know how either of those methods pan out all that well. also tony were would i find information about array indexes would that be in the arrays tutorial from 2005? |
|
|
|
|
|
Raknarg
|
Posted: Sat Jan 05, 2013 6:07 pm Post subject: RE:having trouble entering more than one entire into a record i need to something similar to a flexible array |
|
|
One thing I did when I wanted to have a flexible array in a record was making an array larger than I would likely need, and then set a variable inside as a cap, so something like this:
Turing: |
type pseudoFlexibleArray :
record
elements : array 1 .. 100
upperLimit : int
end record
var marks : pseudoFlexibleArray
marks.upperLimit := 0
|
Voila! Basically a flexible array in functionality. |
|
|
|
|
|
Tony
|
Posted: Sat Jan 05, 2013 6:08 pm Post subject: RE:having trouble entering more than one entire into a record i need to something similar to a flexible array |
|
|
The suggestion was to go ahead with the flexible arrays, the way you figured they would work. But since we can't keep them inside of the records, place that data elsewhere. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
dog44
|
Posted: Sat Jan 05, 2013 6:49 pm Post subject: Re: having trouble entering more than one entire into a record i need to something similar to a flexible array |
|
|
i finally did manage to get the results I wanted all i had to do was go with your suggestions tony about the very large static arrays and then have a flexible array out side which is attached to those records so each student now has his own set of records which is exactly what I wanted to do i have the code down below if anyone is interested to see what i did. thanks to everyone who suggested ideas to me
Turing: |
type classmarks :
record
name : string
item : array 1 .. 100 of string
mark : array 1 .. 100 of int
outof : array 1 .. 100 of int
average : real
comments : string
end record
var stumarks : flexible array 1 .. 0 of classmarks
|
|
|
|
|
|
|
Tony
|
Posted: Sun Jan 06, 2013 3:13 pm Post subject: RE:having trouble entering more than one entire into a record i need to something similar to a flexible array |
|
|
That works, but you should be aware of the limitations -- it seems that no student can have more than 100 marks entered for them. That might be a reasonable number, but who knows, the requirements don't specify
dog44 @ Fri Jan 04, 2013 10:46 pm wrote: The amount of marks to be entered are unknown and the user must be able to add new entries if need be.
So just something to double check on. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Sponsor Sponsor
|
|
|
|
|