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; }