How do you get the total sum of the contents of an array? I have an array filled with integers and here is my code. Instead of objectAtIndex I need something like SumofObjects:1,2,3,4,5; is this possible? Any help would be great thanks!
How do you get the total sum of the contents of an array? I have an array filled with integers and here is my code. Instead of objectAtIndex I need something like SumofObjects:1,2,3,4,5; is this possible? Any help would be great thanks!
I am not at my mac right now to test this but it should work.
Code:
int sum=0;
for(int x=0; x < [array count]; x++)
{
sum += [array objectAtIndex:x];
}
Thanks! The code dosent give any errors but it gives two warnings. One in your code and the other when i try to set the cells text as "sum". They are "makes integer from a pointer without a cast" You know what that means?
int sum=0;
for(int x=0; x < [array count]; x++)
{
sum += [[array objectAtIndex:x] intValue];
}
//to set the text
label.text = [NSString stringWithFormat:@"%d", sum];
int sum=0;
for(int x=0; x < [array count]; x++)
{
sum += [[array objectAtIndex:x] intValue];
}
//to set the text
label.text = [NSString stringWithFormat:@"%d", sum];
Um I'm pretty sure my array dosent have NSNumber objects. It's just a normal NSMutable array initwithObjects:@"1",@"2","3",nil.
How do I make it into that? Thanks!