Thread: Help Counting Amount of Different Numbers in an Array

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    13

    Question Help Counting Amount of Different Numbers in an Array

    Hi, I'm pretty new to programming and I'm having some trouble with this. I need to create a program that counts the amount of different numbers in an array, for example if the user entered the numbers: 858443 the result would be:
    8 occurs 2 times
    5 occurs 1 time
    4 occurs 2 times
    3 occurs 1 time

    I can get the prgram to ask the user to enter the amount of numbers they're going to enter and I can get those numbers and store them in the array but then I'm not sure what to do. They also need to be in descending order, heres what I have so far:

    Code:
    #include <stdio.h>
    #define NUMBERS 10
    int main ()
    {
    int count[NUMBERS];
       int i = 0, num_st, input_ok = 0, total = 0;
       /* initialise the array */
       for (i = 0; i < NUMBERS; i++)
             count[i] = 0;
    /* get the amount of numbers */
       printf("Enter the amount of numbers > ");
       
       do 
       {
              scanf("%d", &num_st);
              if (num_st >=0 && num_st <= 10)
                  input_ok = 1;
             else
                 printf("Error> Max number is %d, Try again \n", NUMBERS);
       }
       while (input_ok == 0);
    /* get values */
       for (i = 0; i < num_st; i++){
            printf("Enter #%d > ", i+1);
            scanf("%d", &count[i]);
            total = total + count[i];
       }
    }
    Any help would be greatly appreciated

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Am I barking up the wrong tree here altogether?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    74
    Try converting the number to string (you can use sprintf for that).

    Then you can travel through that string (with a pointer) and compare each character separately, and then add to the counter.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Thanks for the reply, we haven't moved on to that stuff in class yet so I don't think I can do it. I'm wondering if I should just start again from scratch?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I think the count array is supposed to sum all the occurrences of the digits as they are entered. So if the user enters digit '8', count[8] gets incremented.
    Something like:
    Code:
    scanf("%d", &num_st);
    count[num_st]++;
    What do you think? There is no need to save the digits any longer than that in an array. Just sum in one by one as they occur.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Sort your array, than you can iterate.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Note that for an integer, you can pick off the last digit by taking num%10. Then if you do newnum=num/10, the new last digits (the 10s place of the original number) is newnum%10.

    If you keep modding by 10 and dividing by 10, you strip off one digit at a time which you can keep count of.

    Might make for a simpler approach than storing digits in an array


    Example:

    User enters 5567

    5567%10 = 7 - so we have a seven
    5567/10 = 556
    556%10 = 6 - we have a six
    556/10 = 55
    55%10 = 5 - we have a five
    55/10 = 5
    5%10 = 5 - we have another five
    5/10 = 0 - we are done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP:::MIX numbers in array
    By ypramesh in forum C Programming
    Replies: 9
    Last Post: 03-30-2006, 07:47 PM
  2. Standard Deviation of an array of numbers...
    By Xenofizz in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2003, 10:48 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. counting even numbers
    By slamit93 in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2002, 01:54 PM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM