Thread: Please help with Printing duplicate array elements

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    USA
    Posts
    4

    Please help with Printing duplicate array elements

    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. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Is there a question?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    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");
    Last edited by DRK; 11-18-2011 at 02:57 AM.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    USA
    Posts
    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. #5
    Registered User
    Join Date
    Nov 2011
    Location
    USA
    Posts
    4
    Yes, please tell me how to print the repeated values in the array

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    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
    Last edited by dmh2000; 11-18-2011 at 05:28 PM.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by TheSprawl View Post
    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;

    Quote Originally Posted by TheSprawl View Post
    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. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TheSprawl View Post
    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. #9
    Registered User
    Join Date
    Nov 2011
    Location
    USA
    Posts
    4
    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. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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
            printf("%d was not found in the array\n",i);
      }
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and printing distinct elements in array
    By dave_the_bear10 in forum C Programming
    Replies: 2
    Last Post: 10-29-2011, 01:31 AM
  2. Printing out Array Elements
    By BB89 in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2010, 02:00 PM
  3. printing elements of an array using pointers
    By zeebo17 in forum C Programming
    Replies: 3
    Last Post: 05-22-2009, 09:30 PM
  4. Small problem with printing elements of an array
    By DLR in forum C Programming
    Replies: 17
    Last Post: 03-09-2006, 06:57 PM
  5. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM

Tags for this Thread