• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Find the frequency in 2d array

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 291
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I work this exercise on C language,as well.
 
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 79
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[0]);
cout << frequency(a, n, x);
return 0;
}  
I hope this will help  you




 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic