How does one count an occurrence of values in an array.

Page 1 of 1

3 Replies - 1032 Views - Last Post: 08 April 2012 - 07:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=274415&amp;s=78b8d034643f73a827f7625b6c9acf4b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Sean7_Jr

Reputation: 0
• Posts: 1
• Joined: 08-April 12

How does one count an occurrence of values in an array.

Posted 08 April 2012 - 06:30 PM

I am working on a project for school and I need to count the instances of values enter by user that are in an array.
I already have the arrays brought in and sorted, but I cannot figure out how many times each occurs in an array.
Here is my current code:
```#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void GetValues(int x[],int *pN);
void bubbleSort(int a[], int n);
void main()
{
int a[100], b[100],i,n;
float Avg;

do {
GetValues(a,&n);
if(n>0 && n<101)
{
for(i=0; i<n;i++)
{
b[i]=a[i];
}

bubbleSort(b,n);
//print

printf("Original     Sorted \n" );

for (i=0; i<n;i++)
{
printf("%7d %10d \n", a[i], b[i]);
}
printf("Value     Count\n");
//5 spaces
}
} while (n>0);
}
void GetValues(int x[],int *pN)
{
int i;
int temp;
printf("Enter number of integer values: " );
scanf("%d",pN);
// incase the user asks for more than 100
if(*pN>100)
{
printf("The value you have entered is greater than 100.\n");

}
else
{
for(i=0;i<*pN;i++)
{
printf("Enter Value %d: \n", i+1);
scanf("%d", &temp);
x[i]=temp;

}
}

}
void bubbleSort(int a[], int n)
{
int ra1,ra2,temp;
//ra=rearrange
for(ra1=0;ra1<n;ra1++)
{
for(ra2=0;ra2<n;ra2++)
{
if(a[ra1]<a[ra2])
{
temp = a[ra1];
a[ra1]=a[ra2];
a[ra2] = temp;
}
}
}
}

```

Is This A Good Question/Topic? 0

Replies To: How does one count an occurrence of values in an array.

#2 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

Re: How does one count an occurrence of values in an array.

Posted 08 April 2012 - 07:00 PM

Since the array is sorted, you can loop through the array comparing current and next values

algorithm:

Loop through the array
Compare the next value with the current value
if they are the same, then increment the counter
else print the value and the count, then reset the count
continue looping

-jjl

#3 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

Re: How does one count an occurrence of values in an array.

Posted 08 April 2012 - 07:03 PM

it's worth mentioning that there is an algorithm the in standard library that does just this, std::count.

#4 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

Re: How does one count an occurrence of values in an array.

Posted 08 April 2012 - 07:04 PM

Suppose someone gave you a stack of cards, sorted in order of value, with an unknown quantity of each value, and you could only turn over and look at 1 card at a time.

And you are also given a sheet of paper and a pencil. How would you count the number of times each card-value occurs in the stack?