Thread: I "Stepped" Through a C Program Using Visual Studio, but Getting Unexpected Results

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    45

    I "Stepped" Through a C Program Using Visual Studio, but Getting Unexpected Results

    I've been teaching myself C with KN King's book: Amazon.com: C Programming: A Modern Approach, 2nd Edition (8601300250168): K. N. King: Books

    I am currently on chapter 8, which deals with arrays. I got to an example program that stumped me. I'm not sure how it works. I'm having trouble parsing the scant description that the author wrote with the code he provided, and understanding what's going on line-by-line.

    I decided to open the program with Visual Studio and "step through" the code.

    I provided a screenshot of the unexpected result below. After entering the number 28212 into the input, I would have expected to see variable n have the value of 28212 on the first line, but instead VS is displaying it as 2821.

    This is confusing to me because the statement n/=10; comes several lines later. This would indeed result in a truncated integer of 2821, but at this point in the program n should still be 28212.

    I provided two screenshots below of what I mean.

    I also provided the text describing the example in question, as well as the code.

    Any help would be greatly appreciated! I'm losing my mind over this. It seems so elementary but I'm not sure what I'm doing wrong. I trust the VS debugger more than me at this point!

    P.S.: Once I figure out what I'm doing wrong here I'd like to discuss a question about the array behavior in orange below. I'm having trouble reconciling the orange text in the description with what's going on in the program.


    Attachment 14954Attachment 14955

    ----------------------------------------------------------------------

    Checking a Number for Repeated Digits

    Our next program checks whether any of the digits in a number appear more than once. After the user enters a number, the program prints either Repeated Digit or No Repeated Digit

    Enter a number: 28212
    Repeated Digit

    The number 28212 has a repeated digit (2)l a number like 9357 doesn't.

    The program uses an array of Boolean values to keep track of which digits appear in a number. The array named digit_seen is indexed from 0 to 9 to correspond to the ten possible digits. Initially, every element of the array is 0 (false). When given a number n, the program examines n's digits one at a time, storing each into the digit variable and then using it an index into digit_seen. If digit_seen[digit] is true, then digit appears at least twice in n. On the other hand, if digit_seen[digit] is FALSE, then digit hasn't been seen before, so the program sets digit_seen[digit] to TRUE, and keeps going

    Notice that n has a type long int, allowing the user to enter numbers up to 2,147,483,647 (or more, on some machines)



    -----------------------------------------------
    Code:
    #include <stdio.h>
    Code:
    
    
    #define TRUE 1
    #define FALSE 0
    
    
    typedef int Bool;
    
    
    
    
    main()
    {
      Bool digit_seen[10] = {0};
      int digit;
      long int n;
    
    
      printf("Enter a number: ");
      scanf_s("%ld", &n);
    
    
      while (n > 0) {
        digit = n % 10;
        if (digit_seen[digit])
          break;
        digit_seen[digit] = TRUE;
        n /= 10;
      }
    
    
      if (n > 0)
        printf("Repeated digit\n\n");
      else
        printf("No repeated digit\n\n");
    
    
      return 0;
    }
    Attached Images Attached Images I &quot;Stepped&quot; Through a C Program Using Visual Studio, but Getting Unexpected Results-no-match-jpg I &quot;Stepped&quot; Through a C Program Using Visual Studio, but Getting Unexpected Results-break-jpg 
    Last edited by potomac; 12-03-2016 at 08:06 PM.

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    It's not unexpected at all. You have your breakpoint on the loop end brace. Look at the last line of the loop. So it runs the full loop once before it hits the breakpoint.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    Thanks for the quick response, Hobbit. I knew I had done something elementally wrong like that. It looks like my unexpected result stemmed from a misunderstanding of where to position the break point. I moved the red dot to the top line of the loop and I'm getting the expected result now. (I just started getting serious with C today. Still need to sit down with the Microsoft video tutorials)
    Attached Images Attached Images I &quot;Stepped&quot; Through a C Program Using Visual Studio, but Getting Unexpected Results-1-jpg 

  4. #4
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    What is it that you don't understand about the code?
    What has the book explained poorly?

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    So I am able to better understand the example now by stepping through the code. I've had minimal experience with arrays until now. (Just knowing what they are, and the different types of arrays)

    My question confusion about the orange text was regarding whether the program is treating the subscripts as the numbers we are keeping track of. This now seems to be the case from my stepping exercise with VS. This wasn't explicitly stated in the orange and I was having trouble visualizing.

    Would it be correct to say that the statement if (digit_seen[digit]) break; is like saying in natural language: "If the new digit you just computed with the modulo operator has a "1" for True above it in the array, break the loop."

    I drew out my visualization below. This is the array when n is 282, just before the loop is to be broken when the if statement realizes that 2 has been "seen" already

    I &quot;Stepped&quot; Through a C Program Using Visual Studio, but Getting Unexpected Results-array-jpg

    Also, the orange text from the book pasted below so you don't have to scroll up

    When given a number n, the program examines n's digits one at a time, storing each into the digit variable and then using it an index into digit_seen. If digit_seen[digit] is true, then digit appears at least twice in n. On the other hand, if digit_seen[digit] is FALSE, then digit hasn't been seen before, so the program sets digit_seen[digit] to TRUE, and keeps going

  6. #6
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Yes that's right. You are keeping a map of digits_seen (good variable name) and if the check shows that the digit map for that digit shows true it does break out of the loop. That is what the break command does.

    Code:
    if (digit_seen[digit]) break;
    is a more readable and less verbose way of saying
    Code:
    if (digit_seen[digit] == true) break;
    You seem to have your head around this already. I've seen worse understanding from students with 3 months under their belt and you've been coding for a day.

  7. #7
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    Well thanks. I've read the first 10 chapters of that book so far and I'm finishing up the exercises now.

    The first 10 chapters are "basics." The final 15 are more advanced and will delve into pointers, etc. This forum looks like a great resource for when the road gets bumpier

    My end goal is to be proficient enough in C to make some modifications to some existing open source code for a scientific instrument with a microcontroller. I've built all the hardware

    Also glad I have sat down today and figured out Visual Studio. MS seems make the best IDE around. I looked at Eclipse and Codeblocks too and found the interfaces a tad dated looking. Visual Studio "Community" edition is totally free as of last year, which is astonishing. Exact same thing as the Professional version which costs thousands, minus the technical support staff

    Which IDE do you use?
    Last edited by potomac; 12-03-2016 at 09:53 PM.

  8. #8
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    It would depend what I am working on. At home I mostly use Visual Studio nowadays.

    I too am extremely pleased that M$ finally decided to give away a full featured visual studio rather than introductory/express stuff.

    I've only been coming here for a few days myself. It does seem a decent and helpful forum, I'll probably stick around to help where I can.

  9. #9
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    Do you have a favorite text on C to recommend to students?

    I have used copies of each of these, each bought on the used $5 bargain bin on Amazon. I picked the KN King book because it was got raves as a lucid book for beginners and intermediates. It's been good so far.

    I've stayed away from K&R because of its age and format as more of an expert's reference than a beginner textbook


    Beginner books

    C Programming: A Modern Approach - K. N. King 4.5/5 - 128 reviews
    Programming in C - Stephen Kochan 4.5/5 - 149 reviews
    C Primer Plus - Stephen Prata 4.5/5 - 411 reviews
    A Book on C - Al Kelley/Ira Pohl 4/5 - 60 reviews
    Practical C Programming - Steve Oualline 4/5 - 620 reviews
    C: How to Program- Paul Deitel & Harvey M. Deitel 4/5 - 346 reviews
    Head First C - David & Dawn Griffiths 4/5 - 36 reviews
    The C Book - Mike Banahan, Declan Brady and Mark Doran




    Reference-style

    The C Programming Language - K&R 4.5/5 - 611 reviews
    C: A Reference Manual - Samuel P. Harbison and Guy R. Steele 4.5/5 - 73 reviews
    C in a Nutshell - Peter Prinz 4.5/5 - 26 reviews
    C Traps and Pitfalls - Andrew R. Koenig (Bell Labs) 4/5 - 25 reviews
    C Pocket Reference - Peter Prinz, Ulla Kirch-Prinz 4.5/5 - 30 reviews
    The comp.lang.c FAQ - Steve Summit

  10. #10
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    I don't have any books on straight C, I mostly program in C++. The K&R book should be good, but it's also very dated now and probably only covers C89. I would happily read anything Andrew Koenig wrote but I suspect that's not a beginner's book. I've seen the Deitel and Deitel book for C++ and wasn't overly impressed. They don't teach the language the way I would but that niggle maybe C++ specific as i can't comment on their C book.

    I'm sure if you get stuck or find something the books don't explain all that well then there's plenty of people here to help, and there's also the comp.lang.c usenet group here which is frequented by many professional programmers that are normally willing to help too.

  11. #11
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    Do you think it's sensible to learn C first? Some have steered me on to that path

    My plan is to get comfortable with C before even attempting C++ or C#. Learn the procedural stuff first, then move on to objects. I did go through a Lynda.com video beginner course on high-level concepts in object oriented stuff. It just gave me exposure to ideas like abstraction, encapsulation, inheritance, etc

    It will take many years to get good

    I was inspired to learn C before Python or even C++ by this editorial: You Can't Dig Upwards

  12. #12
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Personally I would learn C++ first. Most of C is also valid C++, but then who am I to say that when my first programming experience was assembly language on the first home computers. I also learnt C++ the hard way, learning the C bit first then the ++ bit but back when I started learning it that was normal. Nowadays I think it's better to start with the standard containers then later learn what's under the hood. I think there's positives and negatives to both methods. In C you have to do all memory management yourself, in C++ you don't have to so much because a lot of dynamic memory use is covered by the standard containers.
    You're not planning on being a computer programmer or software engineer, you're learning C for a specific reason. I would stick at it. C is fairly ideal for what you plan to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio - "stdafx.h" Questions
    By setleaf in forum C++ Programming
    Replies: 3
    Last Post: 01-29-2015, 06:34 AM
  2. Visual Studio - "Single-EXE" output
    By Petike in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2009, 05:54 AM
  3. Visual Studio C++ 2005 first task "Create a window"
    By csonx_p in forum Windows Programming
    Replies: 3
    Last Post: 04-15-2008, 05:49 AM
  4. Replies: 14
    Last Post: 12-26-2004, 11:18 AM

Tags for this Thread