views:

171

answers:

1

Just as a practice, I am working on an app that solves the famous middle school pythagorean theorem, a squared + b squared = c squared. Unfortunately, the out-coming answer has, in my eyes, nothing to do with the actual answer. Here is the code used during the "solve" action.

- (IBAction)solve {
 int legoneint;
 int legtwoint;
 int hypotenuseint;

 int lonesq = legoneint * legoneint;
 int ltwosq = legtwoint * legtwoint;
 int hyposq = hypotenuseint * hypotenuseint;

 hyposq = lonesq + ltwosq;

 if ([legone.text isEqual:@""]) {
  legtwoint = [legtwo.text intValue];
  hypotenuseint = [hypotenuse.text intValue];

  answer.text = [NSString stringWithFormat:@"%d", legoneint];
  self.view.backgroundColor = [UIColor blackColor];
 }
 if ([legtwo.text isEqual:@""]) {
  legoneint = [legone.text intValue];
  hypotenuseint = [hypotenuse.text intValue];

  answer.text = [NSString stringWithFormat:@"%d", legtwoint];
  self.view.backgroundColor = [UIColor blackColor];
 }
 if ([hypotenuse.text isEqual:@""]) {
  legoneint = [legone.text intValue];
  legtwoint = [legtwo.text intValue];

  answer.text = [NSString stringWithFormat:@"%d", hypotenuseint];
  self.view.backgroundColor = [UIColor blackColor];
 }
}

By the way, legone, legtwo, and hypotenuse all represent the UITextField that corresponds to each mathematical part of the right triangle. Answer is the UILabel that tells, you guessed it, the answer. Does anyone see any flaws in the program? Thanks in advance!

+4  A: 

Did not checked the program carefully but in the first lines there is already a big problem:

int lonesq = legoneint * legoneint;
int ltwosq = legtwoint * legtwoint;
int hyposq = hypotenuseint * hypotenuseint;

This vars are defined using vars that are still not assigned at all. You need to set the value of the vars taken from the text fields, then do the math. C is a sequential language, everything is executed from top to bottom, you can't say "a = b*c" and a will be b*c in any place of the program.

antirez
This is a nice example of how the use of '=' in many procedural programming languages confuses people. It's not equality, it's assignment.So language designers, please either use a different symbol (like ':=') or make it actually mean equality (like in Haskell).