So I'm writing a TI-BASIC algorithm for my calculator that does ratios. Here is the code:
Disp "GIVE ONE NUMBER"
Prompt A
Disp "GIVE A SECOND NUMBER"
Prompt B
While fPart(A)≠0 or fPart(B)≠0
A*10->A
B*10->B
End
gcd(A,B)->C
Disp A/C
Disp B/C
It seems to work in most cases. Are there any flaws/corner cases of this code that I have not noticed? Thanks.
It will work, but there is an even cooler way to do it with no loops.
Disp "GIVE ONE NUMBER"
Prompt A
Disp "GIVE A SECOND NUMBER"
Prompt B
1/fPart(A)->F
A*F->A
B*F->B
1/fPart(B)->F
A*F->A
B*F->B
gcd(A,B)->C
Disp A/C
Disp B/C
Yes it will work and so will Jean-Bernard Pellerin's answer, but to best clean it up:
Input "GIVE ONE NUMBER",A
Input "GIVE A SECOND NUMBER",B
1/fPart(A)->F
A*F->A
B*F->B
1/fPart(B)->F
A*F->A
B*F->B
gcd(A,B)->C
Disp A/C
Disp B/C
Related
Is it possible to have a symbol, like a dollar sign $, appear before a the cursor on the user input line, in the terminal? Like a prompt, kind of.
For example. If you had something like:
puts "How much money do you want?"
gets.to_i
and would output in the terminal
How much money do you want?
$ >
Thanks!
Here's an easy way:
print "$ >"
input = gets.chop
Since I use print instead of puts it doesn't put a newline after that.
This is kinda rudimentary, and there are certainly more advanced ways to do this (e.g. with ncurses and other libraries)
I'm trying to write a program that calculates the sum of a geometric series on a TI-84.
Prompt A
Prompt R
Prompt N
If N=100 and abs(R)<1
Disp (A/1-R)
Else
Disp (A(1-R^N))/(1-r)
It says that there is a syntax error at the Else line.
Else can only be paired with an If .. Then construct, not a plain If. So:
Prompt A,R,N
If N=100 and abs(R)<1
Then
Disp A/(1-R
Else
Disp (A(1-R^N))/(1-R
In general the If.. Then .. Else .. End construct should be closed by End but in this case the program exits anyway so it makes no difference. There is some documentation of this in the official TI-BASIC manual and you can check out a more detailed version here.
I am trying to make a console program that asks the user for their name, and greets them. I am coding in Notepad++, and running it in the git bash console. My code is:
puts "Hello, please type in your name: "
name = gets.chomp
puts "Hello #{name}, it is a pleasure to meet you!"
When it runs, it waits for user input, and once that is entered, it prints:
Hello, please type in your name:
Hello <name_the_user_entered>, it is a pleasure to meet you!
Even though the user prompt is first in the code, it accepts user input before any test is printed. Am I missing something?
Ruby might be buffering output. To force it, use flush:
$stdout.puts "Hello, please type in your name: "
$stdout.flush
name = gets.chomp
puts "Hello #{name}, it is a pleasure to meet you!"
But I think in newer Ruby versions, this shouldn't be necessary.
I have a Ruby script where I want to take money as a command line input like this:
>ruby myscript.rb Rob $100
where $100 is dollars.
However, $ is a special character in bash, which interprets the $100 as $1 and my Ruby script gets "00" as the command line input(ARGV), when I want the complete $100.
Is there a way to get the unescaped version of the dollar field? (I know that '$100' will retain the raw value, but the API I've been given means that the user can't be burdened with remembering to quote all dollar values)
Thanks in advance for any ideas here!
Can't you just use a bit different approach? Something like this:
puts "Provide your name:"
name = gets.chomp
puts "Hey, #{name}, how much are you ready to pay?"
money = gets.chomp
puts "#{name}, thanks for your #{money}"
from user's side it will look like:
# Provide your name:
# Rob
# Hey, Rob, how much are you ready to pay?
# $100
# Rob, thanks for your $100
What is the difference between using MESSAGE and tags for the last string?
user = ARGV.first
prompt = '> '
puts "Hi #{user}, im the #{$0} script."
puts "I'd like to ask you a few questions."
puts "Do you like me #{user}?"
print prompt
likes = STDIN.gets.chomp()
puts "Where do you live #{user}?"
print prompt
lives = STDIN.gets.chomp()
puts "What kind of computer do u have?"
print prompt
computer = STDIN.gets.chomp()
puts <<MESSAGE
"Alright, so you said #{likes} about liking me. You live in #{lives}. Not sure where it is.
And you have a #{computer} computer, which is nice."
MESSAGE
What you see is something called here-docs. It's a convenient way to have multiline strings without having to escape quotes. Besides this, they are just regular strings.
Some editors may offer additional features. See my other answer about this.