Board index » delphi » A little program...

A little program...

Quote
butt-kicker wrote:

Klai, people may take you more seriously if you use your real name here
...

Quote
> Below's my recent program. It may be kinda stupid and too simple for
> you guys. This is a problem from my book. Though it works, I think
> there can be much improvement. So would u mind telling me what should
> I do to improve, thx. Moreover, I've got some questions:
> 1. Now, I assume the max num is 5, well, can it be unlimited? so how
> should i declare the array?

     It can be "unlimited".  The "limit" is on your program's memory.
If, however, you decide to store everything externally in a file, first
decide on what the file record shouldbe.  Once you know, simply declare
a FILE OF recordtype.  [Note -- I only know
Standard Pascal, not Borland, so I'm assuming the Standard version of
the language].

Quote
> 2. If I declare the max num as a much larger number, eg. 1000. If
> there are only 500 Stocks in the STOCK.FIL and maybe 50 stocks sold (
> in the SALES.FIL ), then when I run this program, it wastes a lot
> resourses, rite? So what can I do?

     It depends on what you mean by "wastes".  Yes, there will be unused
memory.  However, arrays are pretty fast data structures, so your
program will fly through thedata, and will handle all sizes up to your
max, here 1000.

     If you want to use "just enough" memory, then you can build a
dynamic data structure
using pointers and "grow" it to fit your data size.  Finally, you can
use an external file, which as noted above, can be of "unlimited" size.
[Please don't bug me about file sizes
being finite -- I really DO know that ...].

Quote
> 3. This is a question about Turbo Pascal 6. Well, when i press TAB,
> the cursor goes to the position of the end of the word on the last
> line, i.e.
> begin { press ENTER then TAB }
>      writeln ...
> there are 5 spaces b4 the 'writeln'. I wanna set it to 2 or 3. I've
> tried to set the 'TAB SIZE' in the 'EDITOR OPTIONS'. However, nothing
> changes!

     I don't know.  One comment, however.  When I see phrases like "b4
the 'writeln'"
and "I wanna", I have to think that I'm dealing with someone who is
either uneducated or
flippant.  I realize this may well be incorrect in your case, but why
not strive to make a
really good impression on people whom you ask for help?

Bob Schor
Pascal Enthusiast (and Old School-Marm)

 

Re:A little program...


Below's my recent program. It may be kinda stupid and too simple for you
guys. This is a problem from my book. Though it works, I think there can
be much improvement. So would u mind telling me what should I do to
improve, thx. Moreover, I've got some questions:
1. Now, I assume the max num is 5, well, can it be unlimited? so how
should i declare the array?
2. If I declare the max num as a much larger number, eg. 1000. If there
are only 500 Stocks in the STOCK.FIL and maybe 50 stocks sold ( in the
SALES.FIL ), then when I run this program, it wastes a lot resourses,
rite? So what can I do?
3. This is a question about Turbo Pascal 6. Well, when i press TAB, the
cursor goes to the position of the end of the word on the last line,
i.e.
begin { press ENTER then TAB }
???? writeln ...
there are 5 spaces b4 the 'writeln'. I wanna set it to 2 or 3. I've
tried to set the 'TAB SIZE' in the 'EDITOR OPTIONS'. However, nothing
changes!
?

program SuperMarket;
{ This is a program to update 'STOCK.FIL' using 'SALES.FIL'?????? }
{ 'STOCK.FIL' contains : the inventory no., description, quantity }
{ 'SALES.FIL' contains : the inventory no., amount sold?????????? }
const Num = 5; { This is the max num of the stock }
type InventArray = array [ 1..Num] of integer;
???? DescriArray = array [ 1..Num] of string;
???? Quantiarray = array [ 1..Num] of integer;
???? SoldAmArray = array [ 1..Num] of integer;
var InFile, OutFile : text;
??? Invent, SoldNo? : InventArray;
??? Descript??????? : DescriArray;
??? Quantity??????? : QuantiArray;
??? SoldAmt???????? : SoldAmArray;
??? i, j??????????? : integer;
begin
???? assign ( InFile, 'STOCK.FIL' );
???? reset ( InFile );

????? { Assume that the files have been sorted in ascending order
?????? of inventory numbers and each field is stored on each line }

????? { Read the STOCK data }
???? i := 1;

???? while not eof ( InFile ) do
?????????? begin
??????????????? readln ( InFile, Invent [i] );
??????????????? readln ( InFile, Descript [i] );
??????????????? readln ( InFile, Quantity [i] );
??????????????? i := i + 1
?????????? end;

???? close ( InFile );

???? { Read the SALES data }

???? assign ( InFile, 'SALES.FIL' );
???? reset ( InFile );

???? i := 1;

???? while not eof ( InFile ) do
?????????? begin
??????????????? readln ( InFile, SoldNo [i] );
??????????????? readln ( InFile, SoldAmt [i] );
??????????????? i := i + 1
?????????? end;

???? close ( InFile );

???? { Write new data in the STOCK file }

???? Assign ( OutFile, 'STOCK.FIL' );
???? Rewrite ( OutFile );

???? i := 1;
???? j := 1;
???? while i <= Num do
???????? begin
????????????? while ( SoldNo [i] <= Invent [j] ) and ( j <= Num )do
??????????????????? begin
???????????????????????? if SoldNo [i] = Invent [j]
??????????????????????????? then Quantity [j] := Quantity [j] - SoldAmt
[i];
???????????????????????? writeln ( OutFile, Invent [j] );
???????????????????????? writeln ( OutFile, Descript [j] );
???????????????????????? writeln ( OutFile, Quantity [j] );
???????????????????????? j := j + 1
??????????????????? end;
????????????? i := i + 1
???????? end;

???? close ( OutFile )

end.

Re:A little program...


Re:A little program...


I feel absolutely sorry about my impoliteness. Actually, I'm not an
English speaker. Therefore, I really don't know that it gives others a
bad impression. I'm really sorry about that. Anyways, thanks for your
help. When any of my words shows impoliteness, I hope you would remind.
I join this newsgroup because I really love programming, I hope you
could understand. Well, sorry again.

my real name is k Lai. I'm from Hong Kong.
?
?

Quote
Bob Schor wrote:
> ?butt-kicker wrote:

> Klai, people may take you more seriously if you use your real name
> here ...

>> Below's my recent program. It may be kinda stupid and too simple for
>> you guys. This is a problem from my book. Though it works, I think
>> there can be much improvement. So would u mind telling me what
>> should I do to improve, thx. Moreover, I've got some questions:
>> 1. Now, I assume the max num is 5, well, can it be unlimited? so how
>> should i declare the array?

> ???? It can be "unlimited".? The "limit" is on your program's memory.?
> If, however, you decide to store everything externally in a file,
> first decide on what the file record shouldbe.? Once you know, simply
> declare a FILE OF recordtype.? [Note -- I only know
> Standard Pascal, not Borland, so I'm assuming the Standard version of
> the language].

>> 2. If I declare the max num as a much larger number, eg. 1000. If
>> there are only 500 Stocks in the STOCK.FIL and maybe 50 stocks sold
>> ( in the SALES.FIL ), then when I run this program, it wastes a lot
>> resourses, rite? So what can I do?

> ???? It depends on what you mean by "wastes".? Yes, there will be
> unused memory.? However, arrays are pretty fast data structures, so
> your program will fly through thedata, and will handle all sizes up to
> your max, here 1000.

> ???? If you want to use "just enough" memory, then you can build a
> dynamic data structure
> using pointers and "grow" it to fit your data size.? Finally, you can
> use an external file, which as noted above, can be of "unlimited"
> size.? [Please don't bug me about file sizes
> being finite -- I really DO know that ...].

>> 3. This is a question about Turbo Pascal 6. Well, when i press TAB,
>> the cursor goes to the position of the end of the word on the last
>> line, i.e.
>> begin { press ENTER then TAB }
>> ???? writeln ...
>> there are 5 spaces b4 the 'writeln'. I wanna set it to 2 or 3. I've
>> tried to set the 'TAB SIZE' in the 'EDITOR OPTIONS'. However,
>> nothing changes!

> ???? I don't know.? One comment, however.? When I see phrases like "b4
> the 'writeln'"
> and "I wanna", I have to think that I'm dealing with someone who is
> either uneducated or
> flippant.? I realize this may well be incorrect in your case, but why
> not strive to make a
> really good impression on people whom you ask for help?

> Bob Schor
> Pascal Enthusiast (and Old School-Marm)

Re:A little program...


Re:A little program...


Quote
Bob Schor wrote:

> butt-kicker wrote:

> Klai, people may take you more seriously if you use your real name
> here ...

And people may take you more friendly if you don't use HTML here...

Quote

> > Below's my recent program. It may be kinda stupid and too simple for
> > you guys. This is a problem from my book. Though it works, I think
> > there can be much improvement. So would u mind telling me what
> > should I do to improve, thx. Moreover, I've got some questions:
> > 1. Now, I assume the max num is 5, well, can it be unlimited? so how
> > should i declare the array?

>      It can be "unlimited".  The "limit" is on your program's memory.
> If, however, you decide to store everything externally in a file,
> first decide on what the file record shouldbe.  Once you know, simply
> declare a FILE OF recordtype.  [Note -- I only know
> Standard Pascal, not Borland, so I'm assuming the Standard version of
> the language].

I guess he knew that memory is a limit. "Unlimited" in
programming context usually means "limited only by
available ressources".

Quote

> > 2. If I declare the max num as a much larger number, eg. 1000. If
> > there are only 500 Stocks in the STOCK.FIL and maybe 50 stocks sold
> > ( in the SALES.FIL ), then when I run this program, it wastes a lot
> > resourses, rite? So what can I do?

>      It depends on what you mean by "wastes".  Yes, there will be
> unused memory.  However, arrays are pretty fast data structures, so
> your program will fly through thedata, and will handle all sizes up to
> your max, here 1000.

I think his concern was not speed.
His program will waist memory, in that it will eat up more memory
than it really needs. Now, DOS is no multitasking OS, so it may not
be that important how much memory the program really needs, unless
it calls another program in between. I'm not sure how it is handled
in multitasking OS DOS boxes.
However, TP programs usually allocate all the memory available at
program start anyway for its heap, even if the heap is not used
at all. It can be told not to do so with a special compiler
directive ($M).

Quote

>      If you want to use "just enough" memory, then you can build a
> dynamic data structure

The main advantage of a dynamic data structure is that there's
no fixed aximum value in the program. If you preallocate
1000 items, and then one time you need 1001, you lost.
With dynamic data structures, the only limit is available memory.

Quote
> using pointers and "grow" it to fit your data size.  Finally, you can
> use an external file, which as noted above, can be of "unlimited"
> size.  [Please don't bug me about file sizes
> being finite -- I really DO know that ...].

If you make this claim with memory, you *must* do the same with
disk space. Indeed, file size is not any less limited than memory
size. Today the limit on files is generally higher (but hey, there
were times when the memory could be up to 640KB, but disks were
just 360KB). But memory is _not_ "more limited" in any way.
Indeed, with modern architectures, memory may be limited
by available swap space - that is, a file limitation!

Quote

> > 3. This is a question about Turbo Pascal 6. Well, when i press TAB,
> > the cursor goes to the position of the end of the word on the last
> > line, i.e.
> > begin { press ENTER then TAB }
> >      writeln ...
> > there are 5 spaces b4 the 'writeln'. I wanna set it to 2 or 3. I've
> > tried to set the 'TAB SIZE' in the 'EDITOR OPTIONS'. However,
> > nothing changes!

There's an option for the behaviour of the Tab key somewhere
in the editor options (I forgot the name).
However, the simplest solution is to just press the space bar
two or three times...

Re:A little program...


Quote
>> > 3. This is a question about Turbo Pascal 6. Well, when I press TAB,
>> > the cursor goes to the position of the end of the word on the last
>> > line, i.e.
>> > begin { press ENTER then TAB }
>> >      writeln ...
>> > there are 5 spaces b4 the 'writeln'. I wanna set it to 2 or 3. I've
>> > tried to set the 'TAB SIZE' in the 'EDITOR OPTIONS'. However,
>> > nothing changes!

The simplest way is to set the tab size to 2 or 3, and then check the
box that is labeled "Use Tab Characters".  This seems to work in TP7,
but there's the side effect that it writes tab characters to the file
instead of whitespace, which will cause it to look funny in other
editors that use different tab settings, but it could be more consistent
and will take up less diskspace (since a tab is 1 character, whereas
each space normally counts as one character).

Jimb

Re:A little program...


Quote
Bob Schor wrote:

>      I don't know.  One comment, however.  When I see phrases like "b4
> the 'writeln'"
> and "I wanna", I have to think that I'm dealing with someone who is
> either uneducated or
> flippant.

Maybe his english is less than perfect, but at least his pascal code is
reasonably formatted and has decent comments.... I've seen many more
mediocre help posts :-)

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
PGP key available from above address,
or http://wwwkeys.pgp.net/

Re:A little program...


Yes. My english is poor because english is not my mother tongue. So maybe I
was rude to you, but actually I was unwitting. I really want to say sorry
for that. Hope you would understand and accept my apology.

k lai
?
?

Quote
Martin Harvey wrote:
> Bob Schor wrote:

> >????? I don't know.? One comment, however.? When I see phrases like "b4
> > the 'writeln'"
> > and "I wanna", I have to think that I'm dealing with someone who is
> > either uneducated or
> > flippant.

> Maybe his english is less than perfect, but at least his pascal code is
> reasonably formatted and has decent comments.... I've seen many more
> mediocre help posts :-)

> MH.

> --
> Martin Harvey.
> http://www.harvey27.demon.co.uk/mch24/
> PGP key available from above address,
> or http://wwwkeys.pgp.net/

Other Threads