Hi,

newbie here.....this is a problem from a textbook I've been using to teach myself C (by K N KING). The text does a lousy job of (NOT) commenting. It requires that the repeated elements, only, print after ("Repeated digit\n\n");

Code:
```#include<stdio.h>
#define TRUE 1
#define FALSE 0
typedef int Bool;
int    main(){
Bool digit_seen[10]= {0};
int digit;
long int n;

printf("Enter a number: ");
scanf("%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;
}```

2. Is there a question?

3. Note: Because of
Code:
```if(digit_seen[digit])
break;```
instruction only first repeated digit will saved and displayed.
Code:
```if (n > 0)
{
printf("Repeated digits\n\n");
printf("%d\n", digit);
}
else
printf("No repeated digit\n\n");```
If you want to display all repeated digits, remove that instruction in the while loop and use for.
Code:
```if (n > 0)
{
printf("Repeated digit\n\n");
for (digit = 0; digit < 10; digit++)
{
if (digit_seen[digit])
printf("%d\n", digit);
}
}
else
printf("No repeated digit\n\n");```

4. Hi DRK,

Thank you for your response. Unfortunately, your solution doesn't work. I am going to assume I am not following the steps you provided correctly.

could you paste the entire code that worked for you

Thanks

5. Yes, please tell me how to print the repeated values in the array

6. I find it easier to structure a problem like this, where you are looking at a sequence, as a state machine. in fact it is a state machine, however you code it. but even a simple one like this benefits from clear code. something like this (pseudocode)
Code:
```state = 0;
...

LOOP
ch = get next character
switch(state) {
case 0:
prev_ch = ch;
state = 1;
break;
case 1:
if (ch == prev_ch) {
// repeated character

state = 1; // if you want to find a third repeat
OR
state = 0; // if you want to find only pairs
}
else {
state = 0;
}
break;
default:
break;
}
ENDLOOP```

7. Originally Posted by TheSprawl
It requires that the repeated elements, only, print after ("Repeated digit\n\n");
Could you elaborate? If I typed in the input number "4294467040", would you want to program to print:

"04" - printing the digits that were duplicated once
"4440" - printing each digit if it has ever appeared in the number before (left to right)
"4" - printing the digit when it's the same as the previous digit

You've got the right sort of approach for the first two. For the first one, I'd increment digit_seen[digit] rather than setting it to true or false. Then after the while loop finishes, go back through digit_seen using a for loop and print the numbers that have occurred more than once.

For the second one you could just replace your "break" with a printf of digit, that'd work, though it'll print the digits from right to left.

To handle the printing of "Repeated digit" vs "No repeated digit", I'd use another variable to record if it'd been printed, .

Code:
```    int printed = FALSE;
while(n > 0){
<if digit seen before>
if (!printed)
print "Repeated digit";
printed = TRUE;
print digit
digit_seen[digit] = TRUE;
n/=10;```

Originally Posted by TheSprawl
could you paste the entire code that worked for you
Think we're all trying not to, as you'll learn much more in making it work yourself. I can post the code though if you insist.

8. Originally Posted by TheSprawl
Yes, please tell me how to print the repeated values in the array
You need to stop asking for help and spend some time actually thinking about the problem...

1) how can you take a number entered by a user?
2) how can you get a look at each digit in that number?
3) how do you track which digits (from #2) have already been seen?
4) how do you report that at the end?

It's not one question. Like everything in computers it's a series of baby-step sized questions.

Once you understand what is required, in little tiny blobs, you can begin to plan your program...
1) What has to happen first?
2) what comes next?
etc.

Then once you have a plan, turn on the computer and begin working on the code. Write small sections of your code, get each section working before moving on to the next... compile and test often...

Finally once you have the program written, test it with valid and invalid inputs, look for improvements, fine tune things... The less "fancy" or "fanciful" your solution the better.

Then you've written a reasonable piece of code...

The bottom line is that nobody has ever solved a problem they don't understand.

FWIW... I can do this in less than 40 lines of code.

9. CommonTater and smokeyangel

I took your advice, pushed the actual problem aside and went back and read up some more on arrays. You were both right; i don't just want a solution to the problem, but a thorough understanding (foundation) of the language.

I prepared the following loop to check the array for duplicates, and then print the findings. It is not perfect in the sense that it prints multiple copies of its findings....While i try to figure out why, could you possible assist? Remember, I am a newbie and do not have clear knowledge of pointers, so they are not are option at this point.

Code:
```#include<stdio.h>
int main(){

int a[10];
int i,int j;
for (i=0; i<10;i++){
scanf("%d", &a[i]);

printf("\n\n");

for (i=0; i<10;i++){
for (j=1; j<a[i];j++ ){
if ((a[i]==a[j])&&(i!=j))
printf("%d ",a[j]);}
}
return 0;

}```

10. You can use a small distribution array:

Code:
```#include <stdio.h>

int main(void) {
int i;
int n[10] = {5, 1, 8, 9, 4, 0, 5, 4, 3, 1};
int dis[10] = {0};  //the distribution array

for(i=0;i<10;i++)  //count up the distribution of numbers
dis[n[i]]++;        //into the dis[]

//show the distribution
for(i=0;i<10;i++) {
if(dis[i] > 1)
printf("%d was found %d times\n",i, dis[i]);
else if(dis[i] == 1)
printf("%d was found one time\n", i, dis[i]);
else