Amazon.com Widgets Please help me translate this code into something I can fullly understand.
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 19, 2014, 12:51:23 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ 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
Print
Author Topic: Please help me translate this code into something I can fullly understand. (Read 1787 times)
spooky
Newbie
*
Posts: 8


Email




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







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


Email




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







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


Email




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
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.