Thread: Number of digits in an array?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    30

    Number of digits in an array?

    How can I find out which digit appears the most in the array?
    Here is something I tried, but really messed something up...


    Code:
    #include <stdio.h>
    #define MIN 1
    #define MAX 99
    
    
    int main(){
    
    
    int polje[10],znamenke[10]={0};
    int i,j,prva,druga,max;
    
    
    srand (time(0));
    
    
    for (i=0;i<10;i++)
        polje[i] = MIN + rand()%(MAX-MIN+1);
    
    
    for (i=0;i<10;i++)
        printf ("%d ",polje[i]);
    
    
    for (i=0;i<10;i++){
        if (polje[i]>9)
        {
        prva = polje[i]/10;
        druga = polje[i]%10;
        znamenke [i] = znamenke[prva]++;
        znamenke[i] = znamenke[druga]++;
        }
        else znamenke[i] = i++;
    }
    
    
    max = znamenke[0];
    
    
    for (i=1;i<10;i++)
        if (znamenke[i]>max) max = znamenke[i];
    
    
    printf ("\n%d",max);
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    1) declare an array of int's equal to the range of your data

    2) for every number in the array, peel off the right hand digit, one by one, until there are no more digits left. As you peel them off, increment the INDEX of a digit_count[] array, with 10 places in it.

    3) If the digit you peel off is a 3, then

    digit_count[3]++;

    etc.

    The peel off is done with first number % 10 (effectively getting the one's column digit value), and then number / 10 (effectively removing the one's digit from the number.

    Between the % 10 and the division by 10, that's where you want to do the counting. In rough code:

    Code:
    n = 123
    while(n>0) {
       n2 = n % 10
       digit_count[n2]++
       n /= 10
    }

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    Thanks!

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    Code:
    for (i=0;i<10;i++){    while(polje[i]>0) {
            pom = polje[i] % 10;
            znamenke[pom]++;
            polje[i] /= 10;
            }
    }
    
    
    max=0;
    for (i=0;i<10;i++)
        if (znamenke[i]>max) max = i;
    
    
    printf ("\n%d\n",max);
    I messed something up again but I can't see where the problem is...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the whole of your current code? Please indent it properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    Here is the whole code...
    I don't get where I'm going wrong with indention... All the time I hear complaints but noone actually says where I went wrong...

    Code:
    #include <stdio.h>
    #define MIN 1
    #define MAX 99
    
    
    int main(){
    
    
    int polje[10],znamenke[10]={0};
    int i,pom,max=0;
    
    
    srand (time(0));
    
    
    for (i=0;i<10;i++)
        polje[i] = MIN + rand()%(MAX-MIN+1);
    
    
    for (i=0;i<10;i++)
        printf ("%d ",polje[i]);
    
    
    for (i=0;i<10;i++){
        while(polje[i]>0) {
            pom = polje[i] % 10;
            znamenke[pom]++;
            polje[i] /= 10;
            }
    }
    
    
    for (i=0;i<10;i++)
        if (znamenke[i]>max) max = i;
    
    
    printf ("\n%d\n",max);
    
    
    for (i=0;i<10;i++)
        printf ("%d ",znamenke[i]);
    
    
    return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Fair enough. Here is how I might format your program:
    Code:
    #include <stdio.h>
    
    #define MIN 1
    #define MAX 99
    
    int main() {
        int polje[10], znamenke[10] = {0};
        int i, pom, max = 0;
    
        srand(time(0));
    
        for (i = 0; i < 10; i++)
            polje[i] = MIN + rand() % (MAX - MIN + 1);
    
        for (i = 0; i < 10; i++)
            printf("%d ", polje[i]);
    
        for (i = 0; i < 10; i++) {
            while (polje[i] > 0) {
                pom = polje[i] % 10;
                znamenke[pom]++;
                polje[i] /= 10;
            }
        }
    
        for (i = 0; i < 10; i++)
            if (znamenke[i] > max)
                max = i;
    
        printf("\n%d\n", max);
    
        for (i = 0; i < 10; i++)
            printf("%d ", znamenke[i]);
    
        return 0;
    }
    This is what my compiler reported:
    Code:
    test.c: In function `main':
    test.c:10: warning: implicit declaration of function `srand'
    test.c:10: warning: implicit declaration of function `time'
    test.c:13: warning: implicit declaration of function `rand'
    That is, you forgot to #include <stdlib.h> and <time.h>

    EDIT:
    Looking more carefully at your code, I think that you may have a fundamental misunderstanding about your requirements. You say that you are supposed to find which digit in the array appears the most often. Does this mean the array is supposed to be an array of digits?
    Last edited by laserlight; 11-20-2011 at 06:43 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    I'm using GNU compiler and it didn't report anything but I will include them... Thanks for the tips!
    Have you found the problem?

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    In case you don't understand what I'm talking about here is an example:

    79 95 77 64 89 72 39 83 26 99
    7 -> the digit that appears the most isn't 7! It's 9...

    Control print:
    0 0 2 2 1 1 2 4 2 6

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by turke92
    Have you found the problem?
    I made an edit, but let me ask again: is the array supposed to be comprised only of digits?

    For example:
    Code:
    {111, 2, 2}
    The digit 1 appears most often, but the number 2 appears most often. What exactly are you trying to find?

    EDIT:
    Oh, okay. The problem is that you have a bug with your max finding code:
    Code:
    for (i = 0; i < 10; i++)
        if (znamenke[i] > max)
            max = i;
    By comparing znamenke[i] with max, you are using max as a value to be compared to an element's value. By assigning i to max, you are using max as the index of an element.
    Last edited by laserlight; 11-20-2011 at 06:50 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    I'm trying to find the DIGIT that appears most often... I'm sorry if I said otherwise!

    EDIT:

    for (i=0;i<10;i++) if (znamenke[i]>znamenke[max])
    max = i;
    I tried this and now it works perfectly! Thank you for your time!
    Last edited by turke92; 11-20-2011 at 06:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Different digits in number
    By cowa in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 08:29 PM
  2. convert 32 bit number to array of digits
    By droseman in forum C Programming
    Replies: 11
    Last Post: 02-18-2009, 09:37 AM
  3. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 12:12 PM
  4. Number of digits?
    By falador in forum C Programming
    Replies: 2
    Last Post: 05-13-2004, 03:52 AM
  5. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 10:39 PM