More Challenges/Practice?


#1

This is about the 5rth Objective-C book that I’ve started since most of the other ones require a background in C or VB, and I must say…This is exactly what I’ve been looking for! Except for some basic HTML I have no programming experience whatsoever and I have been dreaming of programming for iDevices since the iPhone first came out. The book seems to be laid out in such a simple and logical way for noobs such as myself…

Here’s my question…
Are there any resources for additional practice or challenges for each chapter? While I find the book easy to read, some of the concepts just aren’t sticking in my brain and I’d love to have some more challenges for each concept and/or chapter. I may be a slower learner than many of your students :blush: but I am bound and determined to learn what I can asap. Please help!


#2

Some of us are masochists at heart: if you provide some suggestions on topics you’re concerned about, I imagine we could throw a few problems your way. A chapter # might also be a good starting point.

I just wish I had some programming projects that I needed for work that I could throw your way.


#3

Thanks for the quick response macintux. I’m only on chapter 6. I’ve started taking notes within Evernote so that I can remind myself of what each command does. I find myself rereading the chapters quite a bit. The concepts seem fairly simple but for some reason they just aren’t sticking… Maybe I’m to old. LOL


#4

I blame my misuse of “masochist” (instead of “sadist”) on the fact that it’s 1:30 in the morning.

Anyway, here is a challenge for you in the spirit of chapters 5 and 6. This is fairly straightforward; I have a related project that I’m thinking about, but might be horribly unfair, so I’m going to make sure I can write it before I post it.

First challenge:

Start a new command-line project. Replace main with:

int main (int argc, const char * argv[])
{
    interpretFraction(3, 5);
    interpretFraction(2, 4);
    interpretFraction(5, 4);
    interpretFraction(5, 0);
    return 0;
}

Write the function interpretFraction using printf such that your program’s output looks like this:

3/5 = 0.600000
2/4 = 0.500000
5/4 = 1.250000
5/0 is invalid: denominator may not be 0

#5

Ok, the second challenge is indeed much more difficult, so I’m going to post some of the answer tonight, and once you’ve taken a look at it and tried it, I’ll be glad to offer more of the solution.

You will need a while loop nested inside a for loop, both of which are described in chapter 7.

Take the same project, and add another function, reduceFraction. This will eliminate any common factors so that your final fraction is in its simplest form.

You’ll call the interpretFraction function you wrote for the first challenge.

Here’s the output and framework for the full solution:

3/5 = 0.600000
1/2 = 0.500000
5/0 is invalid: denominator may not be 0
3/2 = 1.500000
1/1 = 1.000000
7/1 = 7.000000
void interpretFraction(int numerator, int denominator)
{
    // Your code from first challenge
}

void reduceFraction(int numerator, int denominator)
{
    if (denominator != 0) {
        /*
         * Ternary operator ?: is like a terse if/else statement
         *
         * Everything before the ? is evaluated, and if the result
         * is true, the value preceding the : is substituted for the
         * whole expression, otherwise the last value is used.
         */
        int maxVal = (numerator > denominator ? numerator : denominator);
        
        /*
         * We only actually need to review possible factors up to half
         * the larger of the two values. I think the +1 is unnecessary,
         * but it can't hurt.
         */
        maxVal = maxVal / 2 + 1;

        // Your new code goes here
    }
    interpretFraction(numerator, denominator);
}

int main (int argc, const char * argv[])
{
    reduceFraction(3, 5);
    reduceFraction(2, 4);
    reduceFraction(5, 0);
    reduceFraction(18, 12);
    reduceFraction(9, 9);
    reduceFraction(21, 3);
    return 0;
}

#6

I’m close I think…

[code]#include <stdio.h>

void interpretFraction (float a, float b)
{
float answer = a/b;
printf("%f / %f = %f\n", a, b, answer);
}

int main (int argc, const char * argv[])
{
interpretFraction(3, 5);
interpretFraction(2, 4);
interpretFraction(5, 4);
interpretFraction(5, 0);
return 0;
}[/code]

Output:
3.000000 / 5.000000 = 0.600000
2.000000 / 4.000000 = 0.500000
5.000000 / 4.000000 = 1.250000
5.000000 / 0.000000 = inf

BTW, I’ll delete my answer when I’m done so that others can benefit from your challenge! Thanks!


#7

Looks good so far. You’ll need an if statement from chapter 4 to handle the 5/0 problem, and you’ll want to reconsider %f for the first two placeholders in your printf.


#8

Got it!

[code]#include <stdio.h>

void interpretFraction (int a, int b)
{
float answer = a/b;
printf("%d/%d = %f\n", a, b, answer);
}

int main (int argc, const char * argv[])
{
interpretFraction(3, 5);
interpretFraction(2, 4);
interpretFraction(5, 4);
interpretFraction(5, 0);
return 0;
}[/code]


#9

Still need to deal with 5/0 more cleanly.


#10

Oops, for the second challenge you’ll also need a while loop (will update the original message). I’ll definitely look for something simpler for challenge #3.

Wish there were a phpBB BBCode for expandable code blocks, so I could post a solution that you’d have to choose whether to view. I briefly looked into creating a custom one, but I’m not sure it’s practical to implement unless I wanted to create my own phpBB environment to test (and maybe not even then).


#11

Full solution: idevdev.wordpress.com/2011/12/04 … fractions/


#12

Ok, intermediate challenge. The for loop in my code comes from chapter 7, but you don’t need anything beyond chapter 6.

Don’t forget that functions can return values. The book illustrates many functions with no return (“void” functions), but here you’ll need a function that returns an integer to represent a truth value.

Define checkMultiple such that you can use the code below and get the output specified.

int main (int argc, const char * argv[])
{
    for (int i = 30; i >= 0; i--) {
        if (checkMultiple(i, 3)) {
            printf("%d is evenly divisible by 3\n", i);
        }
    }
    return 0;
}
30 is evenly divisible by 3
27 is evenly divisible by 3
24 is evenly divisible by 3
21 is evenly divisible by 3
18 is evenly divisible by 3
15 is evenly divisible by 3
12 is evenly divisible by 3
9 is evenly divisible by 3
6 is evenly divisible by 3
3 is evenly divisible by 3
0 is evenly divisible by 3