Why is my code only writing to the array 4 times?
Show older comments
word = input("Type a scrambled string: ", 's');
len = length(word)
totalPoss = factorial(len)
divisions = totalPoss/len
space = zeros(totalPoss,len);
%disp(space);
for i = 1:len
for j = 1:divisions
space((j + len*(i-1)),1) = word(i);
end
end
disp(space);
When I run the above code in a Linux environment using octave, I expect to see the array space's firec column to be filled with the letters of the input, word.
For example, if the input were to be "lime," I would expect that space[1:6][1] to be "l," space[7:12][1] to be "i," etc. Instead, this is the output I am getting:
Type a scrambled string: lime
len = 4
totalPoss = 24
divisions = 6
108 0 0 0
108 0 0 0
108 0 0 0
108 0 0 0
105 0 0 0
105 0 0 0
105 0 0 0
105 0 0 0
109 0 0 0
109 0 0 0
109 0 0 0
109 0 0 0
101 0 0 0
101 0 0 0
101 0 0 0
101 0 0 0
101 0 0 0
101 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
I am unsure as to why the first three characters are only being inserted 4 times and why all of the extra spaces are at the bottom of the array. (I also don't know why the letters are ascii numbers, but that is a different problem I think).
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!