An arithmetic progression 1/2, 1/4, 1/8, etc. needs summation after n loops.
The relevant code from the main procedure:
The relevant code from the add: method follows:
Regarding the lines in red in main's FOR LOOP, Kochan writes:
"... is then added to the cumulative sum by using by using the add: method. The result
from add: is assigned to sum2 and not to sum, to avoid memory leakage... The old sum
is then released, and the new sum, sum2, is assigned to sum for the next iteration...."
This newbie to OOP needs a better understanding of this problem. Could someone
please clarify this problem for me? I don't quite get this memory-tag concept.
E.g., why are pointers used for objects, yet not for defined integers?
(For an old-time procedural coder, this looks like a lot of heavy extra baggage.)
I thank you.
The relevant code from the main procedure:
Code:
Fraction *aFraction = [[Fraction alloc] init];
Fraction *sum = [[Fraction alloc] init], *sum2;
int i, n;
int pow2 = 2;
[sum setTo: 0 over: 1];
NSLog(@"Enter your value for n.");
scanf("%i", &n);
for (i=0; i<n; i++) {
[aFraction setTo: 1 over: pow2];
[COLOR="Red"]sum2 = [sum add: aFraction];
sum = sum2;
[/COLOR] pow2 *= 2;
}
Code:
-(Fraction *) add: (Fraction *) f
{
Fraction * result = [[Fraction alloc] init];
result.numerator = (numerator * f.denominator + denominator * f.numerator);
result.denominator = denominator * f.denominator;
return result;
}
"... is then added to the cumulative sum by using by using the add: method. The result
from add: is assigned to sum2 and not to sum, to avoid memory leakage... The old sum
is then released, and the new sum, sum2, is assigned to sum for the next iteration...."
This newbie to OOP needs a better understanding of this problem. Could someone
please clarify this problem for me? I don't quite get this memory-tag concept.
E.g., why are pointers used for objects, yet not for defined integers?
(For an old-time procedural coder, this looks like a lot of heavy extra baggage.)
I thank you.