Exercise 7.3 reads -
Modify program 7.7 7.6 to also display the resulting sum as a fraction, not just as a real number.
I'm having some weirdness with this example program, which is carrying through to this exercise.
The example in the book shows you can enter values for 5, 10, and 15 and get the answers 0.96875, 0.999023, 0.999969 respectively.
I'm getting a 0.96875 for 5, but anything 8 and above I'm just getting 1 as the answer.
My convertToNum method as per the book - -(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
And the relevant section of int main Fraction *aFraction = [[Fraction alloc] init];
Fraction *sum = [[Fraction alloc] init], *sum2;
int i, n, pow2;
[sum setTo: 0 over: 1];
NSLog(@"Enter your value for n:");
scanf("%i", &n);
pow2 = 2;
for (i = 1; i <= n; ++i) {
[aFraction setTo: 1 over: pow2];
sum2 = [sum add: aFraction];
[sum release];
sum = sum2;
pow2 *= 2;
}
NSLog (@"After %i iterations, the sum is %g.", n, [sum convertToNum]);
[aFraction release];
[sum release];
Once I sort out what's going on with this I'm confident I can answer exercise 7.3 quite easily.
Ben