Redim'd array error in commodore 64 basic? - arrays

I am getting a redim'd array error in my commodore 64 basic project
I am not however re dimensioning my 2d array nor do I go through the line of code more than once!
Error is on line 1140
Can anyone help me out?
Thanks!
Code:
10 print "start"
20 rem: go to line 1100 in order to fill board with "."s because this is
30 rem: the board's initialization
40 gosub 1100
50 rem: looping from i to x allows for horizontal aspect of board to be printed
60 rem: x represents the width dimension of board, in this case, 8
70 for i = 1 to x
80 rem: looping from j to x allows for vertical aspect of board to be printed
90 rem: x represents the height dimension of board, in this case, 8
100 for j = 1 to x
110 rem: board initialized with "."s is printed
120 print b3$(i,j),
130 rem: end of first for loop, looping from i to x put on 130; , USED 4 TAB
140 next
150 print
160 rem: end of second for loop, looping from j to x
170 next
180 rem: checks what at the random number is equal to; places word vertically
190 rem: if rand is < 50 and places the word horizontally if rand is > 50
200 if r1 < 50 then gosub 1510
210 if r1 > 50 then print "no"
1000 rem: random num generator generates a random integer
1050 rem: between 0 and 100
1040 rem: values between 0 and 100, inclusive
1050 r1 = int(100*rnd(1))+1
1060 rem: Subroutine Fill
1070 rem: Purpose: read and data construct which fills b3$(x,x) with
1080 rem: either "."s or other random words depending on whether or not
1090 rem: the subroutine has been run before.
1100 x = 8
1110 rem: x represents the dimension for the board; in this case,8
1120 rem: took out
1130 rem: array b3 = board = specifications for width and height (8)
1140 dim b3$(x, x)
rem: i to x allows the horizontal aspect of board to be filled with "."s
1150 for i = 0 to x
1160 rem: j to x allows the vertical aspect of board to be filled with "."s
1170 for j = 0 to x
1180 rem: board filled with dots horizontally and vertically
1190 b3$(i, j) = "."
1200 rem: end of first nested for loop
1210 next
1220 rem: end of second nested for loop
1230 next
1240 return

You need an end statement before your subroutines. To make sure you don't interfere with anything else, something like:
1099 end
Otherwise, your program "completes," and then runs through all your subroutine code again just for fun.

It is because the line rem: i to x allows the horizontal aspect of board to be filled with "."s after the 1140 line doesn't have any number. To solve the problem, you can remove the line or put the number 1145 (for example) before.

Line 200 looks suspect, because line 1510 doesn't exist.
200 if r1 < 50 then gosub 1510

Related

Personalized sorting algorithm that operates on just half of the array. (in C)

I have a simple task but I found myself running in circles.
I need to "create" a sorting algorithm using already seen classic ones that will sort an array using the lowest possible number of memory accesses.
The generated array has some rules though:
the first half is generated like this A[i]=rand() % (n/10);, so we have small numbers spanning from 0 to 9.
the second half is like that A[i]=(n-i)*(n-i);, here we have bigger numbers, but they're generated in a descending order.
For the first half I found that using counting sort is very effective, giving me around 4.5k memory accesses. For the second half, the best way is to just reverse the array since it's already decreasing, but I do want it in an ascending order.
My idea was splitting the array in two sub arrays, sorting the first one, reversing the second one and then merging and printing them.
I wrote a split function:
void split(int* A, int* A1, int* A2, int q){
for(int i=0; i<q; i++){
A1[i]=A[i];
ct_op=ct_op+2; //ci perdo un 10k accessi
}
for(int i=q+1; i<q*2-1; i++){
A2[i]=A[i];
ct_op=ct_op+2;
}} //where q is the dim of the array /2
Then I apply the counting sort to my array A1 that works and gives me back my B array which is now ordered. Now, I can't seem to reverse the second half of the array (which should be contained in A2).
void reverse(int* A, int dim){
int i; int j; int temp;
for(i=n/2, j=n-1; i<j; i++,j--){
temp=A[i];
A[i]=A[j];
A[j]=temp;
}}
That doesn't work and the array doesn't get reversed.
Another problem that I have is that the A2 array doesn't start from the half of the A1 array. It's just a bunch (array dim/2) of 0s and then I get the second half of the arrays:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2500 2401 2304 2209 2116 2025 1936 1849 1764 1681 1600 1521 1444 1369 1296 1225 1156 1089 1024 961 900 841 784 729 676 625 576 529 484 441 400 361 324 289 256 225 196 169 144 121 100 81 64 49 36 25 16 0 0
why is that? I could just start printing from half of it, but that's not quite what I'm looking for.
I guess the TLDR is how do I properly split an array and how do I reverse the content of it?
Thank you.
I' ll just use the variables from your code and try to answer the TLDR, because your code is very inefficient.
The following picture shows the array A (which is just a pointer), the pointer A1 and the pointer A2.
As you can see here there is no need for splitting it with the for-loops you used. You just call your count sort algorithm with the pointer A or A1 and the length q and you pass the pointer A2 and the length q to a array reverse algorithm.
I found some implementations for such an array reverse algorithm here.

Ramdomly replace each entry by one of its four neighbours

I iterate through a 100x100 array and pick every time four neighbours (one left of the center node, one above, one right and one below), like in the picture below
the red one is the center node and the blue ones are the neighbours. I struggle to find a convenient way in MATLAB to pick randomly one of the neighbours.
The following assumes that
Each entry is replaced by one of its original neighbours, independently of what happens to other entries.
Each neighbour has the same probability of being picked.
Neighbourhood is defined cyclically. Thus, for example, in the first column the "left" neighbour belongs to the last column.
The code builds a cyclically extended matrix for convenience, and then uses linear indexing to (randomly) select the neighbours.
x = [10 20 30 40; 50 60 70 80; 90 100 110 120]; % example data
x_ext = x([end 1:end 1], [end 1:end 1]); % cyclically extended matrix
ind = bsxfun(#plus, (2:size(x,1)+1).', (1:size(x,2))*(size(x,1)+2)); % linear indices
% of the original matrix in the extended matrix
delta = [-1 1 -size(x_ext,1) size(x_ext,1)]; % possible displacements for neighbours,
% as linear indices
r = delta(randi(4, size(x))); % generate random displacements
result = x_ext(ind + r); % pick neighbours in extended matrix
Example:
>> x
x =
10 20 30 40
50 60 70 80
90 100 110 120
>> result
result =
20 30 70 30
90 100 60 120
50 110 70 40

How is an array sliced?

I have some sample code where the array is sliced as follows:
A = X(:,2:300)
What does this mean about the slice of the array?
: stands for 'all' if used by itself and 2:300 gives an array of integers from 2 to 300 with a spacing of 1 (1 is implicit) in MATLAB. 2:300 is the same as 2:1:300 and you can even use any spacing you wish, for example 2:37:300 (result: [2 39 76 113 150 187 224 261 298]) to generate equally spaced numbers.
Your statement says - select every row of the matrix A and columns 2 to 300. Suggested reading

Correct usage of subroutine in commodore basic 4.0?

I have a subroutine which fills an array with "."s
In my main program I am trying to call this subroutine and then print the array; however, it doesn't seem to be working. I think I am incorrectly calling the subroutine?
This is my code:
subroutine:
1070 dim a$(x,x)
1080 for aa = 0 to x
1090 for bb = 0 to x
2000 a$(x,x)="."
2010 next
2020 next
main code:
10 input "please enter a number"; x
20 gosub 1070
30 for i = 1 to x
40 for j = 1 to x
50 print a$(i,j);
60 next
70 print
80 next
Nothing happens when run; but when I run it all in one program (not calling gosub) it works?
Any help?
In line #2000, I believe you want a$(aa,bb)=".", otherwise you're just hammering the same location with the initialization.
Also, and probably more important to your question, every GOSUB needs a RETURN to get back to the main line of execution. In your case, that's probably line 2030.

How can I create an array of ratios inside a for loop in MATLAB?

I would like to create an array or vector of musical notes using a for loop. Every musical note, A, A#, B, C...etc is a 2^(1/12) ratio of the previous/next. E.G the note A is 440Hz, and A# is 440 * 2^(1/12) Hz = 446.16Hz.
Starting from 27.5Hz (A0), I want a loop that iterates 88 times to create an array of each notes frequency up to 4186Hz, so that will look like
f= [27.5 29.14 30.87 ... 4186.01]
So far, I've understood this much:
f = [];
for i=1:87,
%what goes here
% f = [27.5 * 2^(i/12)]; ?
end
return;
There is no need to do a loop for this in matlab, you can simply do:
f = 27.5 * 2.^((0:87)/12)
The answer:
f =
Columns 1 through 13
27.5 29.135 30.868 32.703 34.648 36.708 38.891 41.203 43.654 46.249 48.999 51.913 55
Columns 14 through 26
58.27 61.735 65.406 69.296 73.416 77.782 82.407 87.307 92.499 97.999 103.83 110 116.54
Columns 27 through 39
123.47 130.81 138.59 146.83 155.56 164.81 174.61 185 196 207.65 220 233.08 246.94
Columns 40 through 52
261.63 277.18 293.66 311.13 329.63 349.23 369.99 392 415.3 440 466.16 493.88 523.25
Columns 53 through 65
554.37 587.33 622.25 659.26 698.46 739.99 783.99 830.61 880 932.33 987.77 1046.5 1108.7
Columns 66 through 78
1174.7 1244.5 1318.5 1396.9 1480 1568 1661.2 1760 1864.7 1975.5 2093 2217.5 2349.3
Columns 79 through 88
2489 2637 2793.8 2960 3136 3322.4 3520 3729.3 3951.1 4186
maxind = 87;
f = zeros(1, maxind); % preallocate, better performance and avoids mlint warnings
for ii=1:maxind
f(ii) = 27.5 * 2^(ii/12);
end
The reason I named the loop variable ii is because i is the name of a builtin function. So it's considered bad practice to use that as a variable name.
Also, in your description you said you want to iterate 88 times, but the above loop only iterates 1 through 87 (both inclusive). If you want to iterate 88 times change maxind to 88.

Resources