3 Replies - 487 Views - Last Post: 08 April 2012 - 07:04 PM Rate Topic: -----

#1 Sean7_Jr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1