variable not defined in a if statement

We're glad you came by, but you might find what you're looking for elsewhere.

TI-Basic Developer is not the site it once was. While its information on commands and other calculator features remains almost second-to-none, its forum, archives, and even hosting service, Wikidot, have been decaying for years. The calculator community would love to see what you're working on, or help you in your next coding adventure, but TI-Basic Developer is no longer the place to do it.

Instead, you should head over to Cemetech (primarily American) or TI-Planet (primarily international). Both are active, well-established forums with their own archives, chatrooms, reference material, and abundant coding tools and resources. We'll see you there, we hope.

Forum » Programming & Design / TI-Nspire Programming » variable not defined in a if statement
Started by: Pemby Pemby
Date: 10 May 2020 19:14
Number of posts: 2
rss icon RSS: New posts
Summary:
I am attempting to write a modular inverse function based on this c++ version in my TI-Nspire CX So far I have the following code, but am getting variable not defined in if statement.
variable not defined in a if statement
Pemby Pemby 10 May 2020 19:14

I am attempting to write a modular inverse function based on this c++ version

#include <iostream>
 using namespace std;
int mul_inv(int a, int b)
{
 int b0 = b, t, q;
 int x0 = 0, x1 = 1;
 if (b == 1) return 1;
 while (a > 1) {
 q = a / b;
 t = b, b = a % b, a = t;
 t = x0, x0 = x1 - q * x0, x1 = t;
 }
 if (x1 < 0) x1 += b0;
 return x1;
}
int main(void) {
 cout<<mul_inv(42, 2017)<<endl;
 return 0;
}

in my TI-Nspire CX So far I have the following code but am getting variable not defined in if statement.

Here is the code I have in my Nspire CX so far.

Define LibPub modinv(a,b)=
Func
 Local b0,t,q,x0,x1
 0→x0
 1→x1
 If b=1 Then
 Return 1
 EndIf
 While a>1
 q:=((a)/(b))
 t:=b
 b:=mod(a,b)
 a:=t
 t:=x0
 x0:=x1-q*x0
 x1:=t
 EndWhile
 If x1<0 Then
 x1:=x1+b0
 EndIf
 Return x1
 EndFunc

the inspire is throwing an error on this line.

 If x1<0 Then
 x1:=x1+b0
 EndIf

Not sure what I am doing wrong here! Any insight would be appreciated!

by Pemby Pemby , 10 May 2020 19:14
Re: variable not defined in a if statement
Xeda Elnara Xeda Elnara 16 May 2020 19:31

I don't have my nspire to test on, but it looks like you are creating a function, so maybe you need something like this at the top:

Local x0,x1,q,t

Also, what is b0? Do you mean to use b? You don't seem to have it declared anywhere. I hope you figure it out; I always like a good mod inverse :)

EDIT: Wow, I have no idea how I missed that you already had that Local definition at the top, sorry!


Z80 Assembly>English>TI-BASIC>Python>French>C>0

Last edited on 16 May 2020 19:33 by Xeda Elnara
by Xeda Elnara Xeda Elnara , 16 May 2020 19:31
/forum/t-13303912/variable-not-defined-in-a-if-statement#post-
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
General Wikidot.com documentation and help section.
Wikidot.com Terms of Service - what you can, what you should not etc.
Wikidot.com Privacy Policy.

AltStyle によって変換されたページ (->オリジナル) /