So I'm trying to count the number of occurrences of each digit within an array.

My code I've got so far looks like the following:

#include <stdio.h>
#include <string.h>

int main()
{
  int numbers [10]= {1, 4, 5, 5, 5, 6, 6, 3, 2, 1};
  int count = 0;

  for(int i = 0; i < 10; i++)
  {
    for (int j = 0; j < 10; j++)
    {
      if (numbers[i] == numbers[j])
      {
        count++;
      }
    }
    printf("Number %d has occured %d many times \n", numbers[i], count);
    count = 0;

  }
} 

Only the output I get is the following:

Number: 1       Occurence: 2
Number: 4       Occurence: 1
Number: 5       Occurence: 3
Number: 5       Occurence: 3
Number: 5       Occurence: 3
Number: 6       Occurence: 2
Number: 6       Occurence: 2
Number: 3       Occurence: 1
Number: 2       Occurence: 1
Number: 1       Occurence: 2 

I only want to count the occurrence of EACH digit, it seems to be counting duplicates.

How can I correct this code? Can someone point me in the right direction.

Many thanks

Alonso

Dani AI

Generated

You are counting correctly, but you print a line for every element, so duplicates get reported multiple times. Two simple ways to print each value once are: (1) track which values you have already reported, or (2) sort then do a single pass over the sorted data. Since your domain is small, 's frequency-table idea is ideal; alternatively, sorting keeps the code short and avoids an extra 10-element table.

Here is a sort-and-scan solution that prints each distinct number exactly once:

#include <stdio.h>
#include <stdlib.h>

static int cmp_int(const void *a, const void *b) {
    int x = *(const int *)a, y = *(const int *)b;
    return (x > y) - (x < y);
}

int main(void) {
    int numbers[] = {1,4,5,5,5,6,6,3,2,1};
    size_t n = sizeof numbers / sizeof *numbers;

    qsort(numbers, n, sizeof *numbers, cmp_int);

    for (size_t i = 0; i < n; ) {
        int val = numbers[i];
        size_t cnt = 1;
        while (++i < n && numbers[i] == val) ++cnt;
        printf("%d occurs %zu time(s)\n", val, cnt);
    }
}

If the title really means digits in a string (e.g., "a1b223c056"), scan the characters and count only when isdigit is true, using *p - '0' as the index. Remember that declaring loop variables inside for (...) needs C99 or later as noted; compile with -std=c99 (or declare indexes before the loops on older compilers).

Recommended Answers

All 3 Replies

I would have to say that you need a flagging system which marks a integer element when its been counted.

Are you sure this is C?

for(int i = 0; i < 10; i++)
{
...
}

Yes, this is C. Changing the outer for loop to be < 10 counts each integer element. Thanks very much.

Keep a frequency table:

int numbers[] = {1, 4, 5, 5, 5, 6, 6, 3, 2, 1};
int freq[10] = {0};

for (int i = 0; i < sizeof numbers / sizeof *numbers; i++) {
    if (numbers[i] >= 0 && numbers[i] < 10) {
        ++freq[numbers[i]];
    }
    else {
        printf("Invalid digit: %d\n", numbers[i]);
    }
}

for (int i = 0; i < 10; i++) {
    printf("%d has occured %d time(s)\n", i, freq[i]);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.