 This week's book giveaway is in the Other Languages forum.We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!See this thread for details. Win a copy of Rust Web Development this week in the Other Languages forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

Find the frequency in 2d array

Ranch Hand
Posts: 31
• • Number of slices to send:
Optional 'thank-you' note:
• • Hello!I have an 2d array which have numbers and i want to find the frequency  of each number.Can you help or give me some advice to manage this??

Thank you!

Rancher
Posts: 347
11
• • Number of slices to send:
Optional 'thank-you' note:
• • Hi Mark

Do you know the range of the numbers? If you do, and it's 'small' eg. 0..9, then you can just use an array of counts which is indexed by number. You go through the array, and for each number N you increment count[N].

But if the range is 'big', or you don't know it, then it becomes a bit more complicated. I would probably use a list of number/count pairs, but there are various ways to implement a 'list'.

I'm assuming this is C - if it's C++ (not my strong point) then there are likely to be better options for the big/unknown range case based around hashing.

So, C or C++, and do you know the number range?

Mark Spen
Ranch Hand
Posts: 31
• • Number of slices to send:
Optional 'thank-you' note:
• • John Matthews wrote:Hi Mark

Do you know the range of the numbers? If you do, and it's 'small' eg. 0..9, then you can just use an array of counts which is indexed by number. You go through the array, and for each number N you increment count[N].

But if the range is 'big', or you don't know it, then it becomes a bit more complicated. I would probably use a list of number/count pairs, but there are various ways to implement a 'list'.

I'm assuming this is C - if it's C++ (not my strong point) then there are likely to be better options for the big/unknown range case based around hashing.

So, C or C++, and do you know the number range?

I don't know how much rows have each time.Only  i know  is that my columns are 7.I have thought to create a new array to store here the frequency but i don't know what to do when i found again the element that previously  have found their frequency.

Mark Spen
Ranch Hand
Posts: 31
• • Number of slices to send:
Optional 'thank-you' note:
• • I work this exercise on C language,as well.

Marshal Posts: 74696
335
• • Number of slices to send:
Optional 'thank-you' note:
• • It should be possible simply to increment an element of an array.

Please don't quote the whole of the preceding post. Ranch Hand
Posts: 82
1
• • Number of slices to send:
Optional 'thank-you' note:
• • Hello,

Do you know it minimum and maximum?

Assume that the original array is a.

If not, find them with a single loop. Assume they are max and min.

Create a 1-d array (freq)with max-min+1 elements and initialize its elements to zeros.

Now take one element from the original array, a, and calculate k=element value - min. Increment freq[k] by one.

Like this freq array contains frequencies.

This is a linear order method.
#include<iostream>
using namespace std;
int frequency(int a[], int n, int x)
{
int count = 0;
for (int i=0; i < n; i++)
if (a[i] == x)
count++;
return count;
}
// Driver program
int main() {
int a[] = {0, 5, 5, 5, 4};
int x = 5;
int n = sizeof(a)/sizeof(a);
cout << frequency(a, n, x);
return 0;
}
I hope this will help  you Don't get me started about those stupid light bulbs.