Why doesn't this ENVELOPE SHAPER program work?

having trouble with an archived file? post in here!
Post Reply
Commie_User
Posts: 1309
Joined: Wed Jan 27, 2016 12:50 am
Contact:

Why doesn't this ENVELOPE SHAPER program work?

Post by Commie_User »

http://stardot.org.uk/mirrors/www.bbcdo ... _mm_02.htm

The long one there.


I honestly typed the listing as printed but get Array at line 1060 whenever I try to run it.

I have Beebsynth, which does this and more but I wanted to see if this was different.
User avatar
MartinB
Posts: 5635
Joined: Mon Mar 31, 2008 10:04 pm
Location: Obscurity
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by MartinB »

Check carefully that you have the DIM Min%(14) in line 150 and that in line 1060, you have typed FORX%=0TO14 and READ Min%(X%)

I suspect that in one of those elements you have made a typo.... :-k
User avatar
paulv
Posts: 3866
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by paulv »

MartinB wrote:Check carefully that you have the DIM Min%(14) in line 150 and that in line 1060, you have typed FORX%=0TO14 and READ Min%(X%)

I suspect that in one of those elements you have made a typo.... :-k
Is that really right? DIM Min%(14) in most languages would create 14 elements in the array...

X%=0 to 14 is accessing 15 elements....

Paul
User avatar
MartinB
Posts: 5635
Joined: Mon Mar 31, 2008 10:04 pm
Location: Obscurity
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by MartinB »

No, it DIMs 0-14 which is 15 elements and this is what is being used.

I still think you have a typo somewhere in one of the items I mentioned.... :wink:
Last edited by MartinB on Wed Jul 06, 2016 9:28 pm, edited 1 time in total.
User avatar
paulv
Posts: 3866
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by paulv »

MartinB wrote:No, it DIMs 0-14 which is 15 elements and this is what is being used.
Wow, I never realised that before. :shock: it's a bit counter intuitive too. Just reading the code, unless you know that DIM x%(14) will give you 15 elements, it could lead to some very interesting errors.

EDIT 1:
I was sure that although the array is 0 indexed, DIM x%(14) would give you 14 elements so you'd access them as 0 through to 13.

http://www.bbcbasic.co.uk/bbcwin/tutori ... ter14.html seems to imply that the arrays are 1 indexed and not 0 indexed which complicates things...

As I read it the code should be

DIM Min%(14)

FOR X%=1 TO 14

EDIT2: Reading further down that page it then goes on to confirm that DIM Min%(14) would indeed give you 15 elements and as such the original code as written in the OP should work just fine.

Paul
Last edited by paulv on Wed Jul 06, 2016 9:35 pm, edited 2 times in total.
User avatar
MartinB
Posts: 5635
Joined: Mon Mar 31, 2008 10:04 pm
Location: Obscurity
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by MartinB »

I suspect the concept of, and difference in, 'count from zero' versus 'count from one' is the cause of many hiccups in many aspects of modern life..... :roll:

Paul - no, I'm absolutely certain and can assure you it's correct. There are 15 data items too to underline the use here....
Last edited by MartinB on Wed Jul 06, 2016 9:34 pm, edited 1 time in total.
User avatar
paulv
Posts: 3866
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by paulv »

MartinB wrote:I suspect the concept of, and difference in, 'count from zero' versus 'count from one' is the cause of many hiccups in many aspects of modern life..... :roll:
Sorry, I mis-posted so have edited my response above to discuss 0 vs 1 indexed arrays etc.

Paul
User avatar
MartinB
Posts: 5635
Joined: Mon Mar 31, 2008 10:04 pm
Location: Obscurity
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by MartinB »

Ha, we're stuck in a loop..... :lol:

I repeat....
I wrote:I'm absolutely certain and can assure you it's correct. There are 15 data items too to underline the use here....
User avatar
MartinB
Posts: 5635
Joined: Mon Mar 31, 2008 10:04 pm
Location: Obscurity
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by MartinB »

...and now we're stuck in an editing loop :lol: =D>
User avatar
paulv
Posts: 3866
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by paulv »

Marvellous! I have to say, some of the examples on that page I linked to are quite confusing and/or wasteful

DIM Month%(12) gives 13 elements for instance and the examples access elements 1 to 12, meaning there's a chunk of memory lost for the first integer in the array.

Another example that says...

Code: Select all

DIM AlienAlive(50)
REM ...
NewLevel=TRUE : REM Assume new level
FOR I%=1 TO 50
  IF AlienAlive(I%) THEN NewLevel=FALSE
NEXT I%
then we have this...

Code: Select all

DIM MyArray%(3)
      
MyArray%() = 1,2,3,4
FOR I%=0 TO 3
  PRINT MyArray%(I%)
NEXT I%
Inconsistent examples lead to inconsistent programming practices which leads to errors. Grrr

Paul
User avatar
hoglet
Posts: 12654
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by hoglet »

This is also the way it was done in Atom Basic.

Very inconsistent and confusing in my opinion.

Dave
User avatar
MartinB
Posts: 5635
Joined: Mon Mar 31, 2008 10:04 pm
Location: Obscurity
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by MartinB »

I'm pretty sure that all BASIC implementations I've ever encountered have always implicitly included element (0) in array dimensioning. I never even flinched when I looked at that envelope shaper code, it just seemed perfectly 'normal' to me but perhaps there's a cultural difference between you guys who 'do' programming and software compared to the likes of myself who are just hobby/self-taught...?

I have done programming at work but it's always been various flavours of assembler or machine code where this sort of thing doesn't rear its head. (Well, the difference in counting from 0 or 1 does but not array dimensioning.)
User avatar
paulv
Posts: 3866
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by paulv »

MartinB wrote:I'm pretty sure that all BASIC implementations I've ever encountered have always implicitly included element (0) in array dimensioning. I never even flinched when I looked at that envelope shaper code, it just seemed perfectly 'normal' to me but perhaps there's a cultural difference between you guys who 'do' programming and software compared to the likes of myself who are just hobby/self-taught...?

I have done programming at work but it's always been various flavours of assembler or machine code where this sort of thing doesn't rear its head. (Well, the difference in counting from 0 or 1 does but not array dimensioning.)
It's more a question of the languages you've used I think. I used Turbo Pascal and Delphi heavily for many years which uses 1 indexed arrays and for the past 16 years, I've used a mix of languages from Delphi to CFML to C# where CFML is 1 indexed and C# is 0 indexed.

My confusion though was brought about not by the indexing, once you know that an array for the language is 0 or 1 indexed, then you just run with that, it was more of a thing where you say DIM X%(15) which when reading it back looks like you're asking for 15 elements whereas it gives you 16...

Indexing and capacity are two different things so for code such as

Code: Select all

DIM X%(15)
I'd expect to see loop code as follows...

Code: Select all

FOR Y%=0 to 14 
....
NEXT

- OR -

FOR Y% = 1 to 15
...
NEXT
Obviously this would depend on the language and the initial index value of 0 or 1.

What I wouldn't expect to see however is

Code: Select all

FOR Y%=0 to 15
...
NEXT
which is what BBC BASIC allows because it is apparently giving you n+1 elements :shock: :!:

In C# you'd see

Code: Select all

int[] x = new int[15];

for (int y=0; y<=14; y++)
{
  do something with x[y];
}
So you ask for 15 integers and access them using 0 through to 14 in the index.

Paul
Commie_User
Posts: 1309
Joined: Wed Jan 27, 2016 12:50 am
Contact:

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by Commie_User »

There were no typing errors. I double and triple checked. I checked the data stream too. Had the program not been badly saved, hence I lost it now, I would have taken a snap.

Is the program actually around in a text format I can just paste into Beeb-Em, or BBC BASIC For Windows? If it just makes envelopes, it doesn't matter how I sample it.
Deleted User 9295

Re: Why doesn't this ENVELOPE SHAPER program work?

Post by Deleted User 9295 »

hoglet wrote:Very inconsistent and confusing in my opinion.
I'm late to this topic (only just noticed it) but I can explain why BBC BASIC's DIM works the way does.

Microsoft, and similar, BASICs often have a special command OPTION BASE to determine whether array indices start at zero or at one. By making all arrays zero-based (internally), but with the DIM statement interpreting its parameter(s) as the maximum index (not the number of elements), BBC BASIC manages to achieve compatibility with both conventions without the complication of a command. The only significant price paid is the wastage of memory resulting from either the first or last element being unused.

It's why you'll see some BBC BASIC examples using one-based indexing and others using zero-based indexing, because it's compatible with both. Personally I think it's clever, not confusing.

Richard.
Post Reply

Return to “archive issues”