Vandervidi - 1 year ago 84
C Question

# Count the number of times a number appears in an array

I'm working on a small program that counts the number of times an integer appears in an array.
I managed to do this but there is one thing I can't overcome.

My code is:

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

int count_occur(int a[], int num_elements, int value);
void print_array(int a[], int num_elements);

void main(void)
{
int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
int num_occ, i;

printf("\nArray:\n");
print_array(a, 20);

for (i = 0; i<20; i++)
{
num_occ = count_occur(a, 20, a[i]);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}

int count_occur(int a[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
int i, count = 0;
for (i = 0; i<num_elements; i++)
{
if (a[i] == value)
{
++count; /* it was found */
}
}
return(count);
}

void print_array(int a[], int num_elements)
{
int i;
for (i = 0; i<num_elements; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
``````

My output is :

``````Array:
2 5 0 5 5 66 3 78 -4 -56 2 66 -4 -4 2 0 66 17 17 -4
The value 2 was found 3 times.
The value 5 was found 3 times.
The value 0 was found 2 times.
The value 5 was found 3 times.
The value 5 was found 3 times.
The value 66 was found 3 times.
The value 3 was found 1 times.
The value 78 was found 1 times.
The value -4 was found 4 times.
The value -56 was found 1 times.
The value 2 was found 3 times.
The value 66 was found 3 times.
The value -4 was found 4 times.
The value -4 was found 4 times.
The value 2 was found 3 times.
The value 0 was found 2 times.
The value 66 was found 3 times.
The value 17 was found 2 times.
The value 17 was found 2 times.
The value -4 was found 4 times.
``````

How can I avoid double lines in the output?

You can use a parallel array, this example uses `char[20]` in order to save some space:

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

int count_occur(int a[], char exists[], int num_elements, int value);
void print_array(int a[], int num_elements);

int main(void) /* int main(void), please */
{
int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
char exists[20] = {0}; /* initialize all elements to 0 */
int num_occ, i;

printf("\nArray:\n");
print_array(a, 20);

for (i = 0; i < 20; i++)
{
num_occ = count_occur(a, exists, 20, a[i]);
if (num_occ) {
exists[i] = 1; /* first time, set to 1 */
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
}

int count_occur(int a[], char exists[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
int i, count = 0;

for (i = 0; i < num_elements; i++)
{
if (a[i] == value)
{
if (exists[i] != 0) return 0;
++count; /* it was found */
}
}
return (count);
}

void print_array(int a[], int num_elements)
{
int i;
for (i = 0; i<num_elements; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
``````

This method is faster, as it skips values already readed and starts iterating from `i` in `count_ocurr`:

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

int count_occur(int a[], char map[], int num_elements, int start);
void print_array(int a[], int num_elements);

int main(void)
{
int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
char map[20] = {0};
int num_occ, i;

printf("\nArray:\n");
print_array(a, 20);

for (i = 0; i < 20; i++)
{
if (map[i] == 0) {
num_occ = count_occur(a, map, 20, i);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
}

int count_occur(int a[], char map[], int num_elements, int start)
/* checks array a for number of occurrances of value */
{
int i, count = 0, value = a[start];

for (i = start; i < num_elements; i++)
{
if (a[i] == value)
{
map[i] = 1;
++count; /* it was found */
}
}
return (count);
}

void print_array(int a[], int num_elements)
{
int i;
for (i = 0; i< num_elements; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download