-1

When trying to write a code for a quadratic equation to figure out the x's, I get an error saying there is a problem with the parenthesis Here's the code i have entered for this calculator

puts "A"
a = gets.to_f
puts "B"
b = gets.to_f
puts "C"
c = gets.to_f

d = (-b+ ((b**2 - 4ac)**1/2))/2a
f = (-b- ((b**2 - 4ac)**1/2))/2a
puts d
puts f

and i come out with this error when trying to run it

quad.rb:8: syntax error, unexpected tIDENTIFIER, expecting ')'
d = (-b+ ((b**2 - 4ac)**1/2))/2a
                     ^
quad.rb:8: syntax error, unexpected tIDENTIFIER, expecting $end

Any help? Thank you very much

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Add four spaces in front of code lines to make them format properly – Tony K. Sep 06 '12 at 01:24
  • Which calculator are you using? TI-Basic differs considerably from Casio's and HP's, for example. Some modern calculators (like the TI nspire) let you omit multiplication operators between symbols, but others require it, so we need more info. – Dai Sep 06 '12 at 01:27
  • Please update the formattings of your question now that you on several occasions have been asked to do so and have been told how. – Sune Rasmussen Sep 30 '12 at 02:44

2 Answers2

0

You are missing * in lots of places. E.g. 4 * a * c instead of 4ac

Tony K.
  • 5,535
  • 23
  • 27
0
puts "I will solve a quadratic equation"
print "What is the value of a in ax^2+bx+c=0?"
a = gets.to_f;
print "What is the value of b in ax^2+bx+c=0?"
b = gets.to_f;
print "What is the value of c in ax^2+bx+c=0?"
c = gets.to_f;

d = b*b - 4*a*c
d = Math.sqrt(d)

e= -b/(2*a)

f= d/(2*a)

puts "The value of first root (x1) is #{e+f}"
puts "The value of second root (xe) is #{e-f}"
rrb
  • 1
  • This would be a better answer if you explain how you fixed the code, and why. Also, note that this question is more than a year old. – Robert Harvey Apr 01 '14 at 19:39