iPhone SDK math - pythagorean theorem problem! - iphone

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!

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 = bc" and a will be bc in any place of the program.

Related

Optimizing Objective-c code

I have a block of code that I am trying to optimize. The method is being used a lot so any little improvement would greatly increase performance.
- (CGRect)calculateRectForItemAtIndex:(NSIndexPath*)path {
//Get the x position stored in an NSMutableArray
double x = [self.sectionXPlacementArray[path.section] doubleValue];
double y = 0;
double height = 0;
//If this is the first row it is a header so treat it different
if (path.row == 0) {
height = self.defaultHeaderHeight;
y = 0;
}
else {
height = self.defaultHeight;
//Calculate the Y placement
y = (path.row-1)*self.defaultHeight+self.defaultHeaderHeight;
}
//Build and return a CGRect
return CGRectMake(x, y, [self.headerSizes[self.headers[path.section]] doubleValue],height);
}
Here is some more information:
1) headerSizes is a NSMutableDictionary that looks like so:
{
Header1 = 135;
Header2 = 130;
Header3 = 130;
}
2) headers is a NSMutableArray that looks like this:
(
Header1,
Header2,
Header3
)
These values in the app will not be Header_. They will be dynamic NSStrings like "City" or "State". headerSizes will contain the width that should be used for each header.
As another person commented, this does NOT look like a method that would be slowing anything down. It's involved in laying something out, right? Or drawing something? That should not be happening very often (i.e. 60 times per second at worst). Do you actually have any evidence that this is the bottleneck? Like, have you run your code through the Profiler template in Instruments? And this showed up as the #1 top method in an inverted-call-tree view of the data?
That said, there's not much to pare down here. I did my best...
- (CGRect)calculateRectForItemAtIndex:(NSIndexPath*)path
{
//Get the x position stored in an NSMutableArray
const NSUInteger pathSection = path.section;
const NSUInteger pathRow = path.row;
const float x = [self.sectionXPlacementArray[pathSection] floatValue];
float y = 0;
float height = 0;
//If this is the first row it is a header so treat it different
if (pathRow == 0) {
height = self.defaultHeaderHeight;
y = 0;
}
else {
const float defaultHeight = self.defaultHeight;
height = defaultHeight;
//Calculate the Y placement
y = (pathRow-1)*defaultHeight+self.defaultHeaderHeight;
}
//Build and return a CGRect
return CGRectMake(x, y, [self.headerSizes[self.headers[pathSection]] floatValue], height);
}

Changing button label not working properly with if-else method

it is probably a very simple problem but I spent already a lot of time on it and just have given up...
I have a button and a textfield for speed calculations. I want the button label change once the button is pressed (km/h >> mph >> m/s >> km/h and so on) and speed recalculated. It sometimes works fine but very often it jumps to "else" statement even if the CurrentSpeedValue is #"km/h". Could anyone help? Maybe it would be better to use switch-case method but how should it be stated?
- (IBAction)speedChange:(id)sender {
//CurrentSpeedUnit is saved to NSUserDefault in another action
if (CurrentSpeedUnit == #"km/h") {
[sender setTitle:#"mph" forState:UIControlStateNormal];
CurrentSpeedUnit = #"mph";
float speedToPrint = ([textSpeed.text floatValue]) / 1.609344;
textSpeed.text = [[NSString alloc] initWithFormat:#"%.3f", speedToPrint];
} else if (CurrentSpeedUnit == #"mph") {
[sender setTitle:#"m/s" forState:UIControlStateNormal];
CurrentSpeedUnit = #"m/s";
float speedToPrint = ([textSpeed.text floatValue]) * 1.609344 / 3.6;
textSpeed.text = [[NSString alloc] initWithFormat:#"%.3f", speedToPrint];
} else {
[sender setTitle:#"km/h" forState:UIControlStateNormal];
CurrentSpeedUnit = #"km/h";
float speedToPrint = ([textSpeed.text floatValue]) * 3.6;
textSpeed.text = [[NSString alloc] initWithFormat:#"%.3f", speedToPrint];
}
}
For string comparison you need to use
isEqualToString
For Example:
if ([CurrentSpeedUnit isEqualToString:#"km/h"])
{
// Perfrom Action
}...
You should not compare strings like that (you compare pointers but not the contents). Use isEqualToString.
i.e.
if ([CurrentSpeedUnit isEqualToString:#"km/h"]) {
...
but not your
if (CurrentSpeedUnit == #"km/h") {
It may work sometimes, but just remember to avoid comparing strings with ==

Reduce number in text box by one for each button tap

I'm relatively new to Objective-C so this might be really simple to do: I have a text box in my application that displays an ammo count, so each time the user taps a fire button the number in the text box will go down by one (12 > 11 > 10, etc) to 0. I have tried using for and if statements, but they are not working (I may have used incorrect syntax). This is what I'm using right now, but obviously I need the
- (IBAction)fire {
[ammoField setText:#"11"];
}
- (IBAction)reload {
[ammoField setText: #"12"];
}
The simplest way would be to convert the text to a number, decrement that and the reset the text, i.e. replace the code in the fire method with:
NSInteger ammoCount = [ammoField.text integerValue];
ammoCount--;
ammoField.text = [NSString stringWithFormat:#"%d", ammoCount];
But don't do this, it will make baby Steve Jobs cry.
A better way would be to add a new variable to the class of type UIInteger that tracks the the number of bullets, i.e.:
// in interface
NSInteger _ammoCount;
...
// in implementation
- (IBAction)fire {
_ammoCount--;
if (_ammoCount <= 0) {
_ammoCount = 0;
fireButton.enabled = NO;
}
[ammoField setText: [NSString stringWithFormat:#"%d", _ammoCount]];
}
- (IBAction)reload {
_ammoCount = 12;
[ammoField setText: [NSString stringWithFormat:#"%d", _ammoCount]];
fireButton.enabled = YES;
}
Oh, and don't forget to call reload at some point early on to ensure _ammoCount and ammoField get initialised.
Set Instance Integer
int x;
set value of it
x = 12;
do change in method
- (IBAction)fire {
[ammoField setText:[NSString stringWithFormat:#"%i",x]];
x--;
}
set the value of count in viewdidload with an int variable
fire method decrease the count by 1
and reload method to return value to 12
log or use values accordingly
Try this:-
int i;
-(void)ViewDidLoad
{
i=12;
}
- (IBAction)fire
{
[ammoField setText:[NSString stringWithFormat:#"%d",i]];
i--;
}
- (IBAction)reload {
i = 12;
[ammoField setText: [NSString stringWithFormat:#"%d", i]];
}
Hope it will work for you. Thanks :)

App crashes when comparing two strings of type NSString

I just just "isEqualToString:" to compare to strings in my iPhone App. The var value is a text and "subInput" is a string out of a text field. Till now everything went well, when the first character of the input field has been the same as the first character of the text. But when they don't fit, the App crashes. When typing in a wrong character after starting well, everything is ok.
subInput = [[NSString alloc] initWithString:[theInput text]];
compare = [[NSString alloc] initWithString:[value substringToIndex:subInput.length]];
if([subInput isEqualToString:compare]){
//Here the app stops working
I also tried to compare the result with "!= 0" or "!= nil" but also didn't work.
ADDITIONAL CODE
if([subInput isEqualToString:compare] != nil){
NSLog(#"any");
if([subInput isEqualToString:value]){
NSLog(#"other");
NSLog(#"well done");
theInput.enabled = FALSE;
lastValid = theInput.text;
theMessage.backgroundColor = [UIColor orangeColor];
theMessage.text = #"WELL DONE!!!!";
theMessage.textColor = [UIColor blackColor];
//save points for remaining seconds
score += seconds*10;
//invalidate counter when typed in all text correct
[count invalidate];
[self newTimer:3.0];
}else{
if(theInput.text.length >= range){
NSLog(#"SCROLLEN");
[theText setSelectedRange:NSMakeRange(range, addRange)];
range += addRange;
}
//NSLog(#"String is ok");
//self.view.backgroundColor = [UIColor blackColor];
lastValid = theInput.text;
theMessage.backgroundColor = [UIColor greenColor];
theMessage.text = #"GO GO GO";
theMessage.textColor = [UIColor whiteColor];
}
} else{
//incrementing the fail var for every wrong letter
fails++;
theInput.text = [#"" stringByAppendingString:lastValid];
theMessage.backgroundColor = [UIColor redColor];
theMessage.text = #"WRONG CHARACTER!!!";
theMessage.textColor = [UIColor whiteColor];
if (grade >= 2) {
seconds -= 2;
}
if(grade == 3){
int actual = theInput.text.length;
actual--;
NSString *shorterString = [theInput.text substringToIndex:actual];
lastValid = shorterString;
theInput.text = shorterString;
}
//change bg-color to iritate the player
self.view.backgroundColor = [UIColor colorWithRed:[self r:255]/255.0 green:[self r:255]/255.0 blue:[self r:255]/255.0 alpha:1];
}
You also need to check that [theInput text] is NOT nil.
subInput = [[NSString alloc] initWithString:[theInput text]];
Docs on [NSString initWithString:(NSString *)aString] are clear that you will get an exception if you attempt to initialize with a nil value.
You need to check and see if value's length is greater than subInput's length. If it is not, you are going out of bounds there.

How to Highlight word of UILabel in iphone

I am loading Lyrics of song on UILabel With Timer. Now I want to highlight Each word of that label with different color. How can I do this ?
I am showing lyrics like this
- (void)setCurrentAudioProgress:(NSTimeInterval)time duration:(NSTimeInterval)duration
{
float progress = time/duration;
long currentPlaybackTime = audioPlayer.currentTime;
long remainingTime = (duration-time);
int remainingHours = (remainingTime /3600);
int remainingMinutes = ((remainingTime /60 -remainingHours*60));
int remainingSeconds = (remainingTime %60);
int currentHours = (currentPlaybackTime / 3600);
int currentMinutes = ((currentPlaybackTime / 60) - currentHours*60);
int currentSeconds = (currentPlaybackTime % 60);
currentTimeLabel.text = [NSString stringWithFormat:#"%i:%02d:%02d", currentHours, currentMinutes, currentSeconds];
remainingTimeLabel.text = [NSString stringWithFormat:#"%i.%02d.%02d", remainingHours , remainingMinutes, remainingSeconds];
[progressView setProgress:progress];
for (int i = 0; i<[lyricsArray count]; i++) {
NSString *lyricsTime = [[lyricsArray objectAtIndex:i]valueForKey:#"startTime" ] ;
if ([currentTimeLabel.text isEqualToString:lyricsTime]) {
songLyricsLabel.text = [NSString stringWithFormat:#"%#",[[lyricsArray objectAtIndex:i]valueForKey:#"songLyrics" ]];
}
}
}
You have to use the trick some thing like this one label doesn't support the highlighting
iPhone "slide to unlock" animation
You can use following whenever you want to highlight the label text then just add the following lines for respective label
label.highlighted = YES;
label.highlightedTextColor = [UIColor blueColor];
for different color you can add an array with color names and select randomly....