help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Repeating bug (error: subscript indices must be either positive inte


From: Andy Buckle
Subject: Re: Repeating bug (error: subscript indices must be either positive integers or logicals)
Date: Wed, 27 Apr 2011 15:51:37 +0100

On Tue, Apr 26, 2011 at 12:49 PM, ragan2328 <address@hidden> wrote:
> I've been working on this program for the past few hours. It's simply a
> password generator. I had the program fully functioning, but upon restarting
> Octave, I keep on getting the message:
>
> error:  subscript indices must be either positive integers or logicals.
>
> Here is the code for the main program:
>
>
> %P1: Pseudocode for Passgen (main)
> NumberofPass = input(['How many passwords do you require?: ']);
>
> if(isempty(NumberofPass));
>   NumberofPass = 1;
> endif;
>
> NumberofChar = input(['How many characters (6-12) do you require in each
> password?: ']);
>
> if(isempty(NumberofChar));
>   NumberofChar = 8;
>   elseif(NumberofChar < 6);
>      NumberofChar = 8;
>      disp(['Number of characters is too low, defaulted to 8 characters'])
>   elseif(NumberofChar > 12);
>      NumberofChar = 8;
>      disp(['Number of characters is too great, defaulted to 8 characters'])
> endif;
>
> lowercase = (['a':'z']);
>
> uppercase = (['A':'Z' ]);
>
> digits = [ '0':'9' ];
>
> PasswordsList = zeros(NumberofPass,NumberofChar);
>
> for(passcount = 1:NumberofPass);
>   ValidPassword = false;
>   while(not(ValidPassword));
>      run OnePassword;
>      run PassCheck;
>   endwhile
>   PasswordsList(passcount,:) = Password;
> endfor;
>
> Passwords = char(PasswordsList);
>
> disp(Passwords)
>
>
>
>
> ...And the code for the program OnePassword that seems to contain the issue:
>
>
>
> %P2: Pseudocode for OnePassword (function)
>
> letters = [uppercase lowercase];
> randomletter = round(rand(1)*52);
> Password(1) = letters(randomletter);
>
> for(n = 2:NumberofChar);
>   randomchar = round(rand(1)*62);
>   allchar = [uppercase lowercase digits];
>   Password(n) = allchar(randomchar);
> endfor;
> return;
>
>
>
> The error points to the code in the OnePassword program, although when run
> separately, no issue exists:
> Password(n) = allchar(randomchar);
>
> Although all was normal up until restart. I'm not sure why it's registering
> this error. Help would be appreciated.

I fiddled with the code til it worked (attached), using Mingw Octave 3.2.4.

Notes
- I avoided improving it too much, otherwise you would not see the
bug. I leave exercises for the reader...
- The thing you call a function in the comment, is a script. A
function has a function declaration. A function would be better.
http://www.gnu.org/software/octave/doc/interpreter/Defining-Functions.html#Defining-Functions
- In your code (why are you calling it pseudocode?), randomletter
could be zero. Indices must be 1 or more. Indeed the error message you
got should have been "subscript indices must be either natural numbers
or logicals".
- I commented out the password check, as you did not give it the code.
- All your loops are unnecessary. Do something like

PasswordsList=ceil(rand(NumberofPass,NumberofChar)*length(allchar))
PasswordsList = char(allchar(PasswordsList))

-- 
/* andy buckle */

Attachment: OnePassword.m
Description: Binary data

Attachment: passgen.m
Description: Binary data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]