0

I am trying to display the an array with different factors of a number ("prime"). But instead of giving me the int numbers I always get 0,1,2,3,4,5,... .

        factors.text = @"";

        int factorsNumber;

        NSMutableArray *array;
        array = [NSMutableArray arrayWithCapacity:5];

        for (factorsNumber=1; factorsNumber<=prime; factorsNumber++) {
            if (prime%factorsNumber == 0) {
                [array addObject:[NSString stringWithFormat:@"%d", factorsNumber]];
            }

        }


        for (int i = 0; i < [array count]; i++) {

            [array replaceObjectAtIndex:1 withObject:@"4"];

            NSString *temp = [NSString stringWithFormat:@"%d, ", i, [[array objectAtIndex:i] intValue]];
            factors.text = [factors.text stringByAppendingString:temp];
        }
0

1 Answer 1

1

Replace

NSString *temp = [NSString stringWithFormat:@"%d, ", i, [[array objectAtIndex:i] intValue]];

with

NSString *temp = [NSString stringWithFormat:@"%d, ", [[array objectAtIndex:i] intValue]];

The problem was you were only printing the array index, not the value.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry for the dumb question simply didn't notice!!
No problem, sometimes it helps to have fresh eyes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.