August 16, 2017, 08:27:32 AM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Read this please.
♦
The Great Kangaroo Escape
♦
Looking for reviews of the 4th ed on Amazon!
♦
Twitter:
@skochan
Home
Help
Search
Login
Register
Chat
Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
>
Programming in C, 3rd ed. (Stephen Kochan)
>
Questions and Help
>
Please help me translate this code into something I can fullly understand.
Pages: [
1
]
Go Down
« previous
next »
Print
Author
Topic: Please help me translate this code into something I can fullly understand. (Read 2554 times)
spooky
Newbie
Posts: 8
Please help me translate this code into something I can fullly understand.
«
on:
June 26, 2010, 04:45:18 PM »
I have been working on the book for about 8 weeks and I'm just about to start chapter 7. I've been trying to complete all the assignments and I am noticing that they sometimes will really stretch quite far beyond what is taught in the lesson. This may not pose a problem for those of you with other 'coding' experience but for a beginner it can be downright discouraging. All that being said I hadn't a clue on how to solve the problem from Chapter 6 ex 6 and finally had to peek at the answers that others had posted.
Here is a piece of code from someone's solution that I think I might understand but I'm not sure...
if(!(p%2))
continue;
isPrime = YES;
Could someone please explain this bit of code in simple layman's terms. Here's what I read...
if the result of p / 2's remainder is not 0 then continue on and make isPrime = YES
Logged
skochan
Administrator
Hero Member
Posts: 3114
Re: Please help me translate this code into something I can fullly understand.
«
Reply #1 on:
June 26, 2010, 09:01:28 PM »
Can you post some more of that code? The
if
statement says that if
p
is evenly divisible by 2, then skip over the remaining statements in the loop but otherwise continue with its execution.
I would like to know which exercises you feel were based on material not taught in the chapter. They may require you to apply the material taught in the chapter in ways that test your understanding, but you shouldn't have to rely on using any language features or constructs that haven't been taught up to that point in the text.
Cheers,
Steve Kochan
Logged
spooky
Newbie
Posts: 8
Re: Please help me translate this code into something I can fullly understand.
«
Reply #2 on:
June 26, 2010, 10:05:56 PM »
Hi Stephen,
Thanks for the quick response.
Perhaps before moving onto to chapter 7, I should really review Chapters 1 to 6 in more detail. It might be that I am rushing along too quickly and have not quite mastered the use of the material up until this point. That would explain why I feel that I am having to make a leap in knowledge to answer the more challenging questions.
Here is the rest of the code that I grabbed the original code excerpt from. It came from another student.
Up until this point I have been able to either create the code myself or be able to look at another student's answer and understand not only how the program works but also why they took the approach they did. This one is a bit hard to follow for me and I am having trouble understanding how that ! operator changes the statement's meaning
and outcome. When I read it ( if(!(p%2)) ) I understand it now to mean if p / 2's remainder 'is not' equal to zero than the 'continue' statement is triggered and the code that immediately follows the continue statement is skipped and we pick it up at the for loop that follows.
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int p, d;
BOOL isPrime;
for (p = 2; p <= 50; ++p)
{
if(!(p%2))
continue;
isPrime = YES;
for (d = 2; d < p && isPrime; ++d)
if(!(p%d))
isPrime = NO;
if(isPrime)
NSLog(@"%i", p);
}
[pool drain];
return 0;
}
I will definitely go back and review and see if the chapter 6 homework makes more sense.
Thanks again Stephen.
Logged
skochan
Administrator
Hero Member
Posts: 3114
Re: Please help me translate this code into something I can fullly understand.
«
Reply #3 on:
June 26, 2010, 11:00:19 PM »
So the first test inside that loop is there to skip past all even numbers (the
for
statement could have also been written to count by 2's--starting at 3--to avoid this test altogether). If the number is odd, the
isPrime
flag is set to YES and then the inner loop is executed. The inner loop tests all possible divisors of
p
(and again, you don't need to test for division by multiples of 2, since we know the number is odd; so this inner loop could also count by 2's). If an even divisor is found, the
isPrime
flag is set to NO, indicating the number can't be prime. (Here again, once an even divisor is found a
break
statement could have been executed to terminate that inner loop.) When the inner loop terminates, if
isPrime
is still set to YES then no divisor was found and so the number must be prime.
The main purpose of this exercise is to get you to understand how to devise an algorithm, or a sequence of steps, to solve a particular problem, and how to translate that into a program. It's not easy for a new programmer to grasp, but a necessary skill to develop to become a good programmer. Don't worry too much at this point if it's still a little fuzzy to you.
Cheers,
Steve Kochan
«
Last Edit: June 26, 2010, 11:02:36 PM by skochan
»
Logged
A31Chris
Newbie
Posts: 20
Re: Please help me translate this code into something I can fullly understand.
«
Reply #4 on:
December 01, 2010, 12:36:12 AM »
What I did Spooky was after a few chapters I kind of skimmed over the Chapter exercises. After I read the whole book I am now in the process of going through it again more carefully and tackling the exercises I glossed over. It helped a bit. Might help a bit for you too.
Logged
Pages: [
1
]
Go Up
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
Programming in Objective-C, 5th edition
-----------------------------
=> General Discussions
=> Exercises
-----------------------------
Programming in Objective-C, 4th edition
-----------------------------
=> General Discussion
=> Exercises
===> Chapter 2
===> Chapter 3
===> Chapter 4
===> Chapter 5
===> Chapter 6
===> Chapter 7
===> Chapter 8
===> Chapter 9
===> Chapter 10
===> Chapter 11
===> Chapter 12
===> Chapter 13
===> Chapter 14
===> Chapter 15
===> Chapter 16
===> Chapter 17
===> Chapter 18
===> Chapter 19
===> Chapter 20
===> Chapter 21
=> Chapter 2
=> Chapter 3
=> Chapter 4
=> Chapter 5
=> Chapter 6
=> Chapter 7
=> Chapter 8
=> Chapter 9
=> Chapter 10
=> Chapter 11
=> Chapter 12
=> Chapter 13
=> Chapter 14
=> Chapter 15
=> Chapter 16
=> Chapter 17
=> Chapter 18
=> Chapter 19
=> Chapter 20
=> Chapter 21
=> Errata
===> 1st Printing
===> 2nd Printing
-----------------------------
Members' Corner
-----------------------------
=> Look What I'm Doing...
-----------------------------
Study Group
-----------------------------
=> Objective-C Quizzes
===> Comprehensive Quiz
===> Chapter 1 - Introduction
===> Chapter 2 - Programming in Objective-C
===> Chapter 3 - Classes, Objects, and Methods
===> Chapter 4 - Data Types and Expressions
===> Chapter 5 - Program Looping
===> Chapter 6 - Making Decisions
===> Chapter 7 - More on Classes
===> Chapter 8 - Inheritance
===> Chapter 9 - Polymorphism, Dynamic Typing, and Dynamic Binding
===> Chapter 10 - More on Variables and Data Types
===> Chapter 11 - Categories and Protocols
===> Chapter 12 - The Preprocessor
===> Chapter 15 - Numbers, Strings & Collections
===> Chapter 17 - Memory Management
-----------------------------
iOS Programming
-----------------------------
=> General Questions
-----------------------------
Programming in C, 3rd ed. (Stephen Kochan)
-----------------------------
=> Programming in C webcasts?
=> Answers to Odd-Numbered Exercises
=> Questions and Help
-----------------------------
Old Stuff
-----------------------------
=> Errata, Answers to Exercises, Code, etc.
=> Shared Code Library
=> Welcome
=> News
=> Errata
===> 1st Printing
===> 2nd printing
=> Newbie Corner
=> Chapter Study
===> Chapter 2 - Programming in Objective-C
===> Chapter 3 - Classes, Objects, and Methods
===> Chapter 4 - Data Types and Expressions
===> Chapter 5 - Program Looping
===> Chapter 6 - Making Decisions
===> Chapter 7 - More on Classes
===> Chapter 8 - Inheritance
===> Chapter 9 - Polymorphism, Dynamic Typing, and Dynamic Binding
===> Chapter 10 - More on Variables and Data Types
===> Chapter 11 - Categories and Protocols
===> Chapter 12 - The Preprocessor
===> Chapter 13 - Underlying C Language Features
===> Chapter 15 - Numbers, Strings, and Collections
===> Chapter 16 - Working with Files
===> Chapter 17 - Memory Management
===> Chapter 18 - Copying Objects
===> Chapter 19 - Archiving
===> Chapter 21 - Writing iPhone Applications
=> Answers to Exercises
===> Chapter 2
===> Chapter 3
===> Chapter 4
===> Chapter 5
===> Chapter 6
===> Chapter 7
===> Chapter 8
===> Chapter 9
===> Chapter 10
===> Chapter 11
===> Chapter 12
===> Chapter 13
===> Chapter 15
===> Chapter 16
===> Chapter 17
===> Chapter 18
===> Chapter 19
===> Chapter 21
=> General Discussion
=> Program Examples
===> Chapter 2
===> Chapter 3
===> Chapter 4
===> Chapter 5
===> Chapter 6
===> Chapter 7
===> Chapter 8
===> Chapter 9
===> Chapter 10
===> Chapter 11
===> Chapter 12
===> Chapter 13
===> Chapter 15
===> Chapter 16
===> Chapter 17
===> Chapter 18
===> Chapter 19
===> Chapter 21
=> XCode 4 Help
=> Suggestions
=> Help!!!
=> Fraction Calculator Available from the App Store
=> Resources
=> Errata
===> 1st & 2nd printings
===> 3rd & 4th printings
===> 5th printing
===> Current Errata
=> Suggestion Box
=> Tips & Tricks
=> Part 3
===> Welcome to the Class
===> Special Announcements
===> Assignments
=> Parts 1 and 2
===> Welcome to the Class
===> Questions and Discussion
===> SPECIAL ANNOUNCEMENTS
===> Assignments
===> Chapter 4, Exercise 7
===> Suggestions
=> Webcast Series Part I, Sept. 1 - 24
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Questions and Discussion
===> Assignments
===> Chapter 4, Exercise 7
===> Chapter 6, Exercise 4
===> Suggestions
=> Webcast Series Part II, Oct. 1 - 27
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Questions and Discussion
===> Assignments
===> Example using hasPrefix: and hasSufix:
===> Suggestions
=> Objective-C Workshop Aug 11 - 13
=> Webcast Series Part I, Oct. 29 - Dec. 1
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Assignments
===> Chapter 4, Exercise 7
===> Chapter 6, Exercise 4
===> Suggestions
=> Webcast Series Part II, Jan 5 - 28
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Assignments
===> Jan 7th Solutions
Loading...