how do I make a limit in up counter? - objective-c

I am new to Objective-C and I made an up counter. By tapping on a button it will increment by one and made another button to reset to 0 but the problem is I could not make is stop in a spacific number e.g I want it when it reaches 15 reset again to zero
-(IBAction)up:(id)sender{
numbercount = numbercount +1 ;
counterdisplay.text = [NSString stringWithFormat:#"%i" , numbercount];
}
-(IBAction)reset:(id)sender{
numbercount = 0 ;
counterdisplay.text = [NSString stringWithFormat:#"%i" , numbercount];
that is my project

Let's say the you have the value of your counter stored in a variable, say, counterValue.
Every time you add 1 to counterValue, simply check if the new value equals 15, and if it does, reset the value back to 0.
- (void)buttonTapped {
counterValue++;
if(counterValue == 15) {
counterValue = 0;
}
}
I can't give you a more specific example because you didn't share your current code, so hopefully this is enough to get you started in your own project.
Welcome to Stack Overflow!

Related

Objective C Math - Geometric sequence results

For my app (OSX, not IOS) i have a geometric sequence (stored in container array) generated like this:
- (void)initContainerFor:(NSInteger)maxRows
{
self.container = [[NSMutableArray alloc] init];
NSInteger start = [self.firstTextFieldValue integerValue];
NSInteger ratio = [self.secondTextFieldValue integerValue];
// ASCENDING
for (NSInteger i = 1; i < (maxRows +1 ); i++)
{
NSInteger currentValue = start * pow(ratio,i-1);
[self.container addObject:currentValue];
}
}
User can enter the "start" and "ratio" integer. I want to give a feedback if limit (MAX_INT) is exceeded. I wrote this function:
- (BOOL)maxCheck
{
if ([self.container lastObject] > INT_MAX)
return false;
return true;
}
But this seems not to work. If i enter 2 for start and 200 for ratio i have this container content:
Container: (
2,
400,
80000,
16000000,
"-1094967296",
49872896,
1384644608,
2051014656,
"-2113929216",
0
)
Please help to understand what i see in the container (strings??) and how to get a correct check for the maximum value.
As you can see from log of array, you actually exceed INT_MAX limit twice, when next element become negative. So you can just add check to initContainer: method - if element is less then the previous, INT_MAX limit is reached.
TIP: INT_MAX is a signed value.
You can not compare the value after overflow with INT_MAX, as the overflow already happened. Or put differently, by its very definition and semantics, no integer can be bigger than INT_MAX.
What you can test is
[self.container lastObject] > INT_MAX/ratio
to find the sequence element that would cause overflow in the next step.

integer that becomes 0 in a strange way

I have an integer : (using xcode for iPhone ) .
int wordCounter=1;
Later on, when i fill arrays with it-as a pointer,in a for loop, it somehow becomes 0 after it becomes a certain number . i have checked my program many times for bugs, and i realize that i dont even have a decrement on it anywhere . i do have wordCounter++.
Now i have seen that i have many lines like this,that after them it becomes 0 :
if(tempBinary[countWords-1][j] != tempBinary[countWords][j])
so i was thinking that countWords-1 decrement it down again and again, is that possible?
i dont have any other decrement or initialization on this variable in my whole program.
i COULD see that when the tempBinary defined as size 7, it initialize at 5, and if tempBinary is 5, it zeroing on 3 ..
is it possible that the array is overflow and is zeroing it? i dont think so..
whats wrong here ?
thanks .
EDIT (one of the problematic states)
int countWords=1;
int stabilityK=0;
tempBinary[0][0]= tempBinary[0][1]=tempBinary[0][2]=tempBinary[0][3]=tempBinary[0][4]=tempBinary[0][5]=tempBinary[0][6]=tempBinary[0][7]=1 ;
for(int k=0;k<numOfBuffers;k++)
{
NSLog(#"countwords:%d",countWords-1);
float *temp=getFFT(buffersRing[k],buffersRing[k][0]);
for(int j=0;j<wordSize;j++)
{
switch(state_on_signal)
{
case WAIT_FOR_SECOND_CHANGE:
//get new word
if(temp[goodBins[j]] > decisionLine[j])
tempBinary[countWords][j]=1;
else
tempBinary[countWords][j]=0;
if(tempBinary[countWords-1][j] != tempBinary[countWords][j])
newData=1;
NSLog(#"s1: countwords:%d",countWords-1);
if(j==wordSize-1)
{
NSLog(#"s2: countwords:%d",countWords-1);
NSLog(#"PRE TEMP:%d%d%d%d%d%d%d%d",tempBinary[countWords-1][0],tempBinary[countWords-1][1],tempBinary[countWords-1][2],tempBinary[countWords-1][3],tempBinary[countWords-1][4],tempBinary[countWords-1][5],tempBinary[countWords-1][6],tempBinary[countWords-1][7] );
NSLog(#"NEW TEMP-WAIT FOR SECOND CHANGE :%d%d%d%d%d%d%d%d",tempBinary[countWords][0],tempBinary[countWords][1],tempBinary[countWords][2],tempBinary[countWords][3],tempBinary[countWords][4],tempBinary[countWords][5],tempBinary[countWords][6],tempBinary[countWords][7] );
NSLog(#"s3: countwords:%d",countWords-1);
//TAKE NEW DATA
if(newData==1)
{
NSLog(#" TOOK new BINARY at current k:%d, so took data at: %d",k,(k+markedK)/2);
for(int s=0;s<wordSize;s++)
{
if( getFFT(buffersRing[(k+markedK)/2],buffersRing[(k+markedK)/2][0])[goodBins[s] ] >decisionLine[s] )
binary[countWords-1][s]= 1;
else
binary[countWords-1][s]= 0;
}
NSLog(#"s4: countwords:%d",countWords-1);
NSLog(#"BINARY%d: %d%d%d%d%d%d%d%d :%d",(countWords-1), binary[(countWords-1)][0], binary[(countWords-1)][1], binary[(countWords-1)][2], binary[(countWords-1)][3], binary[(countWords-1)][4], binary[(countWords-1)][5], binary[(countWords-1)][6], binary[(countWords-1)][7],[self getDecimal:binary[countWords-1]]);
countWords++;
markedK=k;
state_on_signal=WAIT_FOR_STABILITY;
}
newData=0;
}
break;
so i was thinking that countWords-1 decrement it down again and again,
is that possible?
No
Usually this happens when you misuse a pointer and write a value to the wrong address in memory.
For example: where and how is declared tempBinary ?
As it's not possible (at least for me) to see the problem just looking at your code I'd suggest to step through your code with the debugger and see when countWords gets changed. Here lies the problem (at least one of them).

How do you check if an NSInteger is greater than another NSinteger?

I'm trying to write code that detects if an integer is greater than another integer. Is this possible?
Here is what i've done so far.
if (NumCorrect >> NumWrong) {
btnCool.title = #"Awww";
}
else {
btnCool.title = #"Cool!";
}
All its doing is going to the else
EDIT:
NSString *numCorrect = [NSString stringWithFormat:#"%d",NumCorrect];
NSString *numWrong = [NSString stringWithFormat:#"%d", NumWrong];
lblWrong.text = numWrong;
lblCorrect.text = numCorrect;
if (NumCorrect > NumWrong) {
btnCool.title = #"Awww";
} else {
btnCool.title = #"Cool!";
}
Use single >
if (NumCorrect > NumWrong) {
btnCool.title = #"Awww";
} else {
btnCool.title = #"Cool!";
}
Double >> is a bit shift operation. You shift every bit in the binary representation of your variable NumCorrect NumWrong amount of bytes to the right. In almost all cases this will return in a number other then 0, which will then treated as a false value and thus the else block is executed.
Almost perfect - just take off one of those >'s. >> and << are for "bit-shifting", a weird hold-over from the earliest days of programming. You're not gonna use them much. What you really want is > and <, which is for testing if numbers are greater than each other or less than each other.
In addition, you may remember from math class that ≥ and ≤ (greater-than-or-equal-to and less-than-or-equal-to) are useful operations as well. Because there's no symbols for those on most keyboards, however, C and Xcode use >= and <= instead.
Finally, you may already know this, but to check if two numbers are exactly equal to each other you can use == (because = is used for setting the contents of variables).
Hope that's helpful!

Using NSStepper to set ceiling value instead of incrementing value

I am using a NSStepper along with a NSTextField. The user can either set the value using the text field or can use the NSStepper to change the value. I will quote my question using the example below:
Suppose the current value of my stepper is 4 and increment value of the stepper is 2:
After I click the UP arrow on the NSStepper the value becomes:
Now Suppose the current value would have been 4.5 i.e.:
After using the UP arrow the value becomes:
What I require is that when the current value is 4.5, after using the UP arrow, the value becomes 6 instead of 6.5
Any ideas to accomplish this are highly appreciated!
What I require is that when the current value is 4.5, after using the
UP arrow, the value becomes 6 instead of 6.5
Hard to tell exactly what you are asking but taking a guess: it sounds like you want to remove the decimal part of the number and increment by your defined step amount (2). You can do this through the floor() function. See here for other Objective-C math functions
double floor ( double ) - removes the decimal part of the argument
NSLog(#"res: %.f", floor(3.000000000001));
//result 3
NSLog(#"res:%.f", floor(3.9999999));
//result 3
If I understand what you want, this code will give you the next even number (up or down depending on which arrow you click), but still allow you to enter non-integer numbers in the text filed. tf and stepper are IBOutlets and num is a property (a float) that keeps track of the value of the stepper before you click an arrow so you can compare with the new number to see if the up or down arrow was clicked.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.num = 0;
self.tf.intValue = 0; //the stepper is set to 0 in IB
}
-(IBAction)textFieldDidChange:(id)sender {
self.num = self.stepper.floatValue = [sender floatValue];
}
-(IBAction)stepperDidChange:(id)sender {
if (self.num < self.stepper.floatValue) { //determines whether the up or down arrow was clicked
self.num = self.stepper.intValue = self.tf.intValue = [self nextLargerEven:self.num];
}else{
self.num = self.stepper.intValue = self.tf.intValue =[self nextSmallerEven:self.num];
}
}
-(int)nextLargerEven:(float) previousValue {
if ((int)previousValue % 2 == 0) {
return (int)previousValue + 2;
}else
return (int)previousValue + 1;
}
-(int)nextSmallerEven:(float) previousValue {
if ((int)previousValue % 2 == 0) {
if ((int)previousValue == previousValue) {
return (int)previousValue - 2;
}else{
return (int)previousValue;
}
}else
return (int)previousValue - 1;
}

How do I compare a constant value to a continuously-updated Accelerometer value each time round a loop?

As it was suggested to me in a previous post of mine, the following code takes the data coming from the accelerometer the "minute" the assignment : CMAccelerometerData* data = [manager accelerometerData]; is performed, and then extracts from that data the acceleration exercised on the x-Axis and stores its value in a double (double x) :
CMMotionManager* manager = [[CMMotionManager alloc] init];
CMAccelerometerData* data = [manager accelerometerData];
double x = [data acceleration].x;
Suppose the value stored is 0.03 and suppose that I want to use it in a while loop as follows :
while (x > 0)
{
// do something
}
the above loop will obviously run forever
However, what if I used the following code instead :
CMMotionManager* manager = [[CMMotionManager alloc] init];
while([[manager accelerometerData] acceleration].x > 0)
{
// do something
}
wouldn't I be now comparing zero to a different value each time round the loop?
(which is what I'm going for in my project anyway..)
any thoughts?
the reason I'm asking this is the following :
I want to check the values coming from the x-Axis over a certain period of time, rather than keep checking them at regular intervals, so I basically want to write a loop that would look something like this :
if ([[manager accelerometerData] acceleration].x > 0 )
{
// initialiseTimer
}
while ([[manager accelerometerData] acceleration].x > 0 )
{
if( checkTimer >=250ms )
{
stopTimer;
printOut("X-Axis acceleration was greater than zero for at least 250ms");
breakFromLoop;
}
}
I know the code in my 2nd if-block isn't valid Objective-C..This was just to give you an idea of what I'm going for..
This has a simple solution.
1)Declare an instance variable x that you update each time the accelerometer tell you to.
2)Compare this x to whatever value you need in the loop .
Hope this helps.
Regards,
George