2

I wrote a program (on my TI-84 calculator) to satisfy the following exercise:

Write a program that will print all solutions of the inequality ax + b < c, where a, b, and c are entered by the user. In this program the domain of x will be a set of consecutive integers, where the smallest and largest members of the set will also be entered by the user. (Hint: Use a FOR . . . NEXT loop to test each integer from smallest to largest.)

This is the code I have:

:Input "A=",A
:Input "B=",B
:Input "C=",C
:Disp "DOMAIN FOR X"
:Input "MIN=",D
:Input "MAX=",E
:For(X,D,E,1)
:If AX+B<C
:Disp X
:End

I sort of figured it out by chance; I don't really know how 'If' works inside 'For.' I wanted to have it tell me if there is no solution, though, so I tried:

:Input "A=",A
:Input "B=",B
:Input "C=",C
:Disp "DOMAIN FOR X"
:Input "MIN=",D
:Input "MAX=",E
:For(X,D,E,1)
:If AX+B<C
:Then
:Disp X
:Else
:Disp "NO SOLUTION"
:End

But this returns the value for "MIN=" Why is this? Can anyone help me understand these work?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
John
  • 21
  • 2

4 Answers4

2

Perhaps try adding an additional End at the end of the program? As far as I know, you need to have a corresponding End for each For or If, etc.

  • Ah, yes. This will print any solutions and then "NO SOLUTION" for the integers that produce a false statement. How can I make it so if there is at least one solution, it doesn't display the no solution, and if there aren't any solutions at all, it will display the no solution only once? – John Dec 31 '09 at 16:58
  • Perhaps use an additional variable, and then set it to 1 if you find a solution, 0 otherwise. Once you get out of the loop, you can check it, and do your stuff from there. I don't have my calculator on me, but I'm sure theres a `Break` command in list somewhere. Just stick that after the `Disp X` in the `If` statement. –  Jan 01 '10 at 00:37
0
:Input "A=",A
:Input "B=",B
:Input "C=",C
:Disp "DOMAIN FOR X"
:Input "MIN=",D
:Input "MAX=",E
:For(X,D,E,1)
:If AX+B<C
:Then
:Disp X
:Else
:Disp "NO SOLUTION"
:Stop
:End

Basically, by putting in Stop it will stop the loop as soon as there is no solution.

dgund
  • 3,459
  • 4
  • 39
  • 64
0

When the variable you're asking for has the same name as the question, you can use ":Prompt" instead of ":Input". Also, ":Prompt" allows for multiple variables. For example, instead of

:Input "A=",A
:Input "B=",B
:Input "C=",C

use

:Prompt A,B,C

In TI BASIC, every :If, :For, :While, and :Repeat requires an :End; otherwise, it won't work at all. This is how to use :If and :For

:If condition
:Then:commands(condition=True)
[:Else:commands(condition=False)]
:End

:For(var,start,end[,interval])
:commands
:End

If the interval=1, you don't need to put it there.

To use :If inside of :For, just make sure your :End's are in the right places. You may also wish to use the ":" character to condense multiple commands on to one line. As for GDund's answer, that sort of works if you want to end the program there, but if you want to simply leave the loop, what you can do is change the value of the counter variable to the end value, like this:

:For(X,D,E)
:If AX+B<C:Then
:commands:Else
:E-->X:End
:End

Where "-->" is the STO arrow.

If you do want the program to stop right there, you can use :Stop in place of :E --> X. If you're using this program inside of another program, you can use :Return, instead.

So your code should look like this:

:Prompt A,B,C  
:Disp "DOM. FOR X"
:Input "MIN=",D
:Input "MAX=",E
:For(X,D,E)
:If AX+B<C:Then:Disp X
:Else:Disp "NO SOL.":E-->X:End
:End
ReGuess
  • 1
  • 1
  • 2
0

Have a variable to see whether or not there was a solution.

:Input "A=",A
:Input "B=",B
:Input "C=",C
:Disp "DOMAIN FOR X"
:Input "MIN=",D
:Input "MAX=",E
:0->Q
:For(X,D,E,1)
:    If AX+B<C
:    Then
:        Disp X
:        1->Q
:    End
:End
:If Q=0
:    Disp "NO SOLUTION"

The variable Q is a boolean to see whether or not there is a solution. If there is, Q is set to true(1). If not, Q remains false(0).

puzzler7
  • 16
  • 2