iPhone maths not quite adding up right - iphone

I am adding values held within an array but the sum is +1 what it actually should be.
//update totalscore
uint newTotalScore;
for (uint i=0; i< [bestscoresArray count] ; i++) {
newTotalScore += [[bestscoresArray objectAtIndex:i] intValue];
}
totalscore = newTotalScore;
//output
l1bestscore=15900, l2bestscore=7800, l3bestscore=81000, l4bestscore=81000, l5bestscore=0, l6bestscore=0, l7bestscore=0, l8bestscore=0, l9bestscore=0, l10bestscore=0, totalscore=185701
As you can see the totalscore output is 185701 but the sum of all values is 185700.
Would anyone have any ideas why this is occurring?
Thanks,
Mark

You must define newTotalScore's initial value:
uint newTotalScore = 0;
Otherwise it will be undefined. In your case it was 1 but it could have been any other value.

Not sure about this, but did you try initializing newTotalScore to zero? (See this question about variable initialization.) If that does not help, give us more code.

Related

Divide NSInteger with int

I am trying to divide NSInteger with some number, but I am receiving errors.
This is what I am trying to do:
length = [corner count];
for (int i=0; i<length; i++) {
if ([resultDate rangeOfString:[corner objectAtIndex:i]].location != NSNotFound) {
cornerResult++;
}
}
cornerResultLabel.text = [NSString stringWithFormat:#"%i", cornerResult];
This works as it should.. I search through the array and count the results and write it.
But, I need cornerResult divided with 4. When I add cornerResult / 4 it shows me the error(as I wrote in comment). I have no idea why is this making problem.
you probably meant to divide the intValue of the NSNumber, not the NSNumber* itself:
int num = number.intValue;
int result = num / 4;
(full code sample and error message would help)
You are dividing an int which becomes a float , use %f instead.
I fix it this way. First I defined int and after that I set NSInteger value to int and after that divide.
int helper = cornerResult;
helper = helper / 4;

NSArray not iterating

I am trying to iterate through an NSArray with a for loop. The result only returns the last value in the array even though the int variable i is printing correctly (0,1,2...).
Also, if I set iteration to say 5, I will get the 6th object in the array, which is correct. I did this to try to narrow down the scope of possible causes.
Any ideas?
int i;
int j;
Buffer *vocalBuffer;
for (i=0; i < numberOfBuffers; i++){ // loop through every vocal buffer
Buffer *mixedBuffer = [[Buffer alloc] init];
int array[sizeLoopBuff];
mixedBuffer.buffer = array;
mixedBuffer.numFrames = sizeLoopBuff;
NSLog(#"Vocal buffer number --> %i", i);
NSInteger iteration = i;
vocalBuffer = [arrayOfVocalBuffers objectAtIndex:iteration]; // grab the vocal buffer
for (j=0; j < sizeLoopBuff; j++){ // run through a beat loop cycle.
mixedBuffer.buffer[j] = loopBuffer.buffer[j]; // add the beats to return buffer.
if (j > insertPoint && j < insertPoint+ vocalBuffer.numFrames){
mixedBuffer.buffer[j] = loopBuffer.buffer[j] + vocalBuffer.buffer[j-insertPoint];
}
}
[mutArray addObject:mixedBuffer];
}
As figured out in the comments, the use of a pointer to stack storage has some problems. One is that while it's in scope, its content is overwritten by each use within a loop; individual objects with pointers to it do not have unique copies.
The other problem is that once the method returns and its stack space isn't needed (as far as the runtime is concerned), there's no predicting what will be done with the space.
The necessary behavior of having a unique buffer per object suggests that the object should allocate its own buffer dynamically when created.

Confusing arrays

Kinda new to iOS, Im also studying Android. kinda confuse on arrays.
How to convert this into iOS:
result as being the index
nextresult[x] array of indexes
for(x = 0; x < array.size; x++)
{
if(result < nextresult[x])
nextresult[x] -= 1;
}
It will iccheck all of the content of the arrays if it needs to be adjusted or not,
Might be following will give you some idea -
As you mentioned, i have considered following - result' is value andnextresult` is array.
for(x = 0; x < [array count]; x++)
{
if(result < [nextresult objectAtIndex:x]) {
[nextresult objectAtIndex:x] -= 1;
}
}
EDIT -
if you want to add integer in arrays -
[yourArray addObject:[NSNumber numberWithInt:1]]; // Objective C array store objects. so we need to convert primitive data type into object.
Read Apple's excellent developer documentation.
You can find the docs about the NSArray class here, or in your Xcode organizer.
I have downvoted your question, because it could very easily have been solved by just looking at the docs.

Objective-C += equivalent?

Sorry for the newbie question, but i cannot find an answer to it.
I have a simple operation. I declare a variable, and then i want to loop through an array of integers and add these to the variable. However, i can't seem to find how to get a += equivalent going in Objective C.
Any help would be awesome.
Code:
NSInteger * result;
for (NSInteger * hour in totalhours)
{
result += hour;
}
NSInteger is not a class, it's a typedef for int. You cannot put it into collections like NSArray directly.
You need to wrap your basic data types (int, char, BOOL, NSInteger (which expands to int)) into NSNumber objects to put them into collections.
NSInteger does work with +=, keep in mind that your code uses pointers to them, which is probably not what you want anyway here.
So
NSInteger a = 1, b = 2;
a += b;
would work.
If you put them with [NSNumber numberWitInt:a]; etc. into an NSArray, this is not that easy and you need to use -intValue methods to extract their values first.
If totalhours actually contains NSNumber objects you need the following:
NSInteger result = 0;
for(NSNumber* n in totalhours)
{
result += [n integerValue];
}
The problem is that you are confusing NSInteger (a typedef for int or long) with a class instance such as NSNumber.
If your totalhours object is an array of NSNumber objects, you'll need to do:
NSInteger result;
for (NSNumber *hour in totalhours)
{
result += [hour integerValue];
}
No problem using the '+=' operator, just be sure about the objects you are working with...
Your code might be :
NSNumber *n; NSUInteger t = 0;
for(n in totalHours) {
t += [n integerValue];
}
// you got your total in t...
The += operation definitly works. All you need to do is initialize your result variable so it has a start value.
E.g. NSInteger * result = 0;
Good luck!
Your problem is probably that you're using a pointer to an NSInteger instead of an actual NSInteger. You're also not initializing it. Try this:
NSInteger result = 0;
for (NSInteger * hour in totalhours)
{
result += *hour;
}

Can I add a value to a int when a button is pressed?

Hi I'm new to programming
My concept is that I want to add 1 to an int type every time I press the button
Is it possible? If yes what's the simplist code to do that?
Within the method of
-(IBAction)addTap:(id)sender;
If not, what type of variable I should use?
Thanks
All you have to do is:
-(IBAction)addTap:(id)sender {
tapCount++;
}
where tapCount would be defined as:
int tapCount = 0;
The ++ simply adds 1 to the value of tapCount.
If you want to add 2 or another number you'd do:
tapCount += 2;
or if you wanted to reduce the tapCount you'd do:
tapCount--;
or: tapCount -= 2;
Depending on the scope of your variable you just need to increment the int variable.
int j = 0;
j += 1;
The easiest would be:
-(IBAction)addTap:(UIButton*)sender
{
sender.tag += 1;
NSLog(#"count is now: %d",sender.tag);
}
which is exploiting the fact that each UIView subclass has a property tag of type int which is often not used.
warning sometimes the tag property is used, see distinguish the UI buttons on the iphone for an example.