Why doesn't this ENVELOPE SHAPER program work?

having trouble with an archived file or want to correct something? report it here!
Commie_User
Posts: 870
Joined: Wed Jan 27, 2016 12:50 am

Why doesn't this ENVELOPE SHAPER program work?

Postby Commie_User » Wed Jul 06, 2016 6:17 pm

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: 4511
Joined: Mon Mar 31, 2008 9:04 pm
Location: Obscurity

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

Postby MartinB » Wed Jul 06, 2016 7:10 pm

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: 3554
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

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

Postby paulv » Wed Jul 06, 2016 8:14 pm

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: 4511
Joined: Mon Mar 31, 2008 9:04 pm
Location: Obscurity

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

Postby MartinB » Wed Jul 06, 2016 8:23 pm

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 8:28 pm, edited 1 time in total.

User avatar
paulv
Posts: 3554
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

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

Postby paulv » Wed Jul 06, 2016 8:27 pm

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/tutorial/chapter14.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 8:35 pm, edited 2 times in total.

User avatar
MartinB
Posts: 4511
Joined: Mon Mar 31, 2008 9:04 pm
Location: Obscurity

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

Postby MartinB » Wed Jul 06, 2016 8:31 pm

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 8:34 pm, edited 1 time in total.

User avatar
paulv
Posts: 3554
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

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

Postby paulv » Wed Jul 06, 2016 8:33 pm

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: 4511
Joined: Mon Mar 31, 2008 9:04 pm
Location: Obscurity

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

Postby MartinB » Wed Jul 06, 2016 8:36 pm

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: 4511
Joined: Mon Mar 31, 2008 9:04 pm
Location: Obscurity

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

Postby MartinB » Wed Jul 06, 2016 8:37 pm

...and now we're stuck in an editing loop :lol: =D>

User avatar
paulv
Posts: 3554
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

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

Postby paulv » Wed Jul 06, 2016 8:44 pm

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: 6372
Joined: Sat Oct 13, 2012 6:21 pm
Location: Bristol

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

Postby hoglet » Wed Jul 06, 2016 8:53 pm

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

Very inconsistent and confusing in my opinion.

Dave

User avatar
MartinB
Posts: 4511
Joined: Mon Mar 31, 2008 9:04 pm
Location: Obscurity

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

Postby MartinB » Wed Jul 06, 2016 8:56 pm

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: 3554
Joined: Tue Jan 25, 2011 6:37 pm
Location: Leicestershire
Contact:

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

Postby paulv » Wed Jul 06, 2016 9:20 pm

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: 870
Joined: Wed Jan 27, 2016 12:50 am

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

Postby Commie_User » Wed Jul 06, 2016 10:13 pm

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.

Richard Russell
Posts: 163
Joined: Sun Feb 27, 2011 10:35 am

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

Postby Richard Russell » Mon Oct 31, 2016 9:17 pm

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.


Return to “archive issues”

Who is online

Users browsing this forum: No registered users and 1 guest