Amazon.com Widgets Debugging with Xcode
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 17, 2014, 12:52:27 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
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 6 - Making Decisions
| | | |-+ Debugging with Xcode
Pages: [1] Go Down
Print
Author Topic: Debugging with Xcode (Read 1878 times)
i-ash
Newbie
*
Posts: 10







on: February 25, 2009, 02:51:37 PM

Hi All,

I remember when programming in Delphi so may years ago that you could "watch" variable values as you stepped through a program line-by-line.

Does anyone know whether this functionality is available in Xcode?

I would find this handy for the last two exercises of chapter 6, e.g. following the values of integers 'p' and 'd' through the following loops:

Code: (Objective-C)
for (p = 2; p <= 50; ++p) {
        isPrime = YES;
       
        if (p % 2 == 0 && p != 2)
            continue;
       
        for (d = 2; d < p && isPrime; ++d){
            if (d % 2 == 0)
                continue;
            if (p % d == 0)
                isPrime = NO;
        }
               
        if (isPrime == YES)
            NSLog(@"%i", p);

Thanks

Ash
Logged

Ash of Melbourne town
mdeh
Full Member
***
Posts: 166






Reply #1 on: February 25, 2009, 04:21:24 PM

Hi All,

I remember when programming in Delphi so may years ago that you could "watch" variable values as you stepped through a program line-by-line.

Does anyone know whether this functionality is available in Xcode?

I would find this handy for the last two exercises of chapter 6, e.g. following the values of integers 'p' and 'd' through the following loops:

Code: (Objective-C)
for (p = 2; p <= 50; ++p) {
        isPrime = YES;
       
        if (p % 2 == 0 && p != 2)
            continue;
       
        for (d = 2; d < p && isPrime; ++d){
            if (d % 2 == 0)
                continue;
            if (p % d == 0)
                isPrime = NO;
        }
               
        if (isPrime == YES)
            NSLog(@"%i", p);

Thanks

Ash

Have you tried placing a breakpoint in your code? If so, once it stops, there is "debug" icon, which once clicked, should show you what you are looking for.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #2 on: February 27, 2009, 08:46:13 AM

Look under Run->Variables View->Watch Variable

Cheers,

Steve
Logged
los1sol
Newbie
*
Posts: 4






Reply #3 on: March 11, 2009, 11:39:24 AM

Steve, I think there's a mistake in the second printing. I spent a long time trying to match the description in the book (2nd paragraph after "Program 6.10 Output", pp 125) of what was happening to the actual code but it didn't make sense. The book I have has the following code:

         for (p = 2; p <= 50; p++) {
      isPrime = YES;
      
      for (d =2; d < p; d++)
         if(p % d == 0)
            isPrime = NO;
      if (isPrime == YES)
         NSLog (@"%i", p);
   }

Which as you can see, is different from what the book describes or what others have posted on this thread. The result is the same but the explanation on my book doesn't make sense with this particular exercise.
I followed the code via the debugger and I think I understand it now.

Correct me if I'm wrong but is this the correct order of how the computer is processing the code in my book:
             
               (1)       (2)          (9)
    for (p = 2; p <= 50; p++) {
      isPrime = YES;
                  (3)
                     
                       (4)       (5)    (7)
      for (d =2; d < p; d++)
         if(p % d == 0)
                isPrime = NO;
                      (6)

                         ( 8  )       
               if (isPrime == YES)
      NSLog (@"%i", p);
   }

Thanks for your help.
Last Edit: March 11, 2009, 11:45:26 AM by skochan Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #4 on: March 11, 2009, 11:58:50 AM

Hi,

I'm a little confused.  First, I think you meant you to say you had the second edition, not the second printing.  There will be multiple printings of the second edition, and the second printing was just delivered to the publisher at the end of last week.  I don't think it's been distributed yet (I haven't see a copy myself).  Please correct me if I'm wrong.

Second, I'm not sure I understand your question.  I reread the description in the book, and I think it's accurate.  The program code you show is for Program 6.10A on page 127, not 6.10.

Anyway, please clarify the question for me.  Your numbering of steps  seems correct and I don't see any discrepancy with the text.

Cheers,

Steve Kochan
Logged
los1sol
Newbie
*
Posts: 4






Reply #5 on: March 11, 2009, 02:37:15 PM

Hi Steve, yes  I was referring to 6.10 not 6.10a, my mistake in posting the code for 6.10a. Here is the code for 6.10:


int p, d, isPrime;
   
   for (p = 2; p <= 50; p++) {
      isPrime = 1;
      
      for (d = 2; d < p; d++)
         if (p % d == 0)
            isPrime = 0;
      
      if (isPrime != 0)
         NSLog(@"%i ", p);
   }

And this is the part I'm having trouble with matching up to the code:
"A second loop is set up to divide p by the integers 2 through p-1."


Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #6 on: March 11, 2009, 03:20:44 PM

This line of code:

Code: (Objective-C)
 for (d = 2; d < p; d++)

is the start of the second loop in question.  The value of d starts at 2 when the loop starts.  Now the first time through the loop, p = 2 and  d < p is false (2 < 2 is false), so the if statement that follows doesn't even get executed (remember, the test is done before the body of the loop is executed).   Therefore, 2 is printed as a prime number.

Remember that the loop exits as soon as the value of d equals p.  In other words, the loop executes for d taking on values of 2 through p - 1.

Try to go through the loop the next time when p is set to 3 for a better  understanding of its operation.

Hope this helps

Cheers,

Steve
Last Edit: March 12, 2009, 08:47:03 AM by skochan Logged
los1sol
Newbie
*
Posts: 4






Reply #7 on: March 11, 2009, 09:18:57 PM

Steve, thanks for your help. I'm not very good at this so it takes me a long time to grasp these concepts. It's really cool that you took the time to answer my question though. Thank you very much.
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.