一维阵列中的数字频率

时间:2014-10-25 19:17:34

标签: c++ arrays frequency

自从我编写代码以来已经过了6个小时,但无济于事,我不知道我犯了什么错误,但我正在制作一些。它的频率输出程序和输出应该是这样的:

array[8] = {6,1,7,8,6,6,1,9}

输出:

6:3
1:2
7:1
8:1
9:1

但它在我的代码中重复相同的数字。任何帮助都会非常明显。

int array[8] = {6,1,7,8,6,6,1,9};
int store[8];
int a =0;
int b =0;
int c=0;

int d = 0; 
store[d] = array[b];
for (d = 0; d < 8; d++){
  int count=0;
  c = d; 
  b = d; 
  for (int e = 0; e < d; e++){
    if (array[b] == store[e]){
      store[d] = array[b];
      b++;              
      e = 0;            

    }                   
    else   
    {      
      store[d] = array[b]; 
      break;    
    }       
  }        

  for ( int z = 0; z < 7; z++){ 
    if (store[d] == array[z])
    {          
      count++;              

    }                       
  }            
  cout << store[d] << ":" << count << endl;
}

6 个答案:

答案 0 :(得分:4)

您可以先使用地图存储num-&gt;频率,然后使用multimap存储freqeuncy =&gt; NUM。

Here是可行的解决方案。

#include <map>
#include <algorithm>
#include <iostream>

int main()
{
    int array[8] = {6,1,7,8,6,6,1,9};

    // A map to store num => freq 
    std::map <int, int> freq;

    // A map to store freq(can be duplicate) => num 
    std::multimap <int, int> freqCounts;

    // Store num => frequency
    for (int i = 0 ; i < 8; i++)
    {
        freq[array[i]] += 1;
    }

    // Now Store freq => num
    for(auto const & iter : freq)
    {
        freqCounts.insert (std::pair<int,int>(iter.second, iter.first)); 
    }

    // Print in reverse order i.e. highest frequency first
    for (std::multimap<int,int>::reverse_iterator rit=freqCounts.rbegin(); rit!=freqCounts.rend(); ++rit)
    {
        std::cout << rit->second << " : " << rit->first << '\n';
    }
    return 0;
}

答案 1 :(得分:3)

你似乎永远不会更新计数器。试试这个:

int array[8] = {6,1,7,8,6,6,1,9};

unsigned int store[10] = {};    // large enough to hold the largest array value,
                                // initialized to zero

for (int n : array) ++store[n]; // update counts

for (int i = 0; i != 10; ++i)
{
    std::cout << "Frequency of int " << i << " is " << store[i] << "\n";
}

如果出现的值集稀疏,或包含底片,或者根本不适合密集的整数范围,则可以用关联容器替换unsigned int[10],例如:

std::map<int, unsigned int> store;

// algorithm as before

for (auto const & p : store)
{
    std::cout << "Frequency of " << p.first << " is " << p.second << "\n";
}

答案 2 :(得分:1)

我不确定你要对数组做什么。我试图遵循逻辑,但很难看到它与所有匿名变量名称。看起来您正在尝试在数组中先前查找重复项,但变量e永远不会获得除0之外的任何其他值,因此您只会与数组中的第一项进行比较。 / p>

您可以查看数组本身以查找以前的出现,一旦您知道该数字是第一次出现,您只需要在数组中查找更多的出现:

int array[8] = {6,1,7,8,6,6,1,9};

for (int i = 0; i < 8; i++) {

  // look to the left in the array if the number was used before
  int found = 0;
  for (int j = 0; j < i; j++) {
    if (array[i] == array[j]) found++;
  }

  // go on if it's the first occurance
  if (found == 0) {

    // we know of one occurance
    int count = 1;

    // look to the right in the array for other occurances
    for (int j = i + 1; j < 8; j++) {
      if (array[i] == array[j]) count++;
    }

    cout << array[i] << ":" << count << endl;
  }
}

答案 3 :(得分:0)

#include<iostream>
#include<conio.h>
using namespace std;
main()
{   int count[10],key[10],n=10,m;
    int i,j,k,temp;
    cout<<"Enter The Size Of Array:-\n";
    cin>>n;
    int a[n];
    cout<<"Enter The Elements in Array:-\n";
    for(i=0; i<n; i++)
        cin>>a[i];
    for(i=0; i<n; i++)
        for(j=0; j<n-1; j++)
        {   if(a[j]>a[j+1])
            {   temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    for(i=0; i<n; i++)
        cout<<a[i]<<"\t";
    for(i=0,k=0; i<n; k++)
    {   count[k]=0;
        key[k]=a[i];
        for(j=i; j<n; j++)
        {   if(a[i]==a[j])
                count[k]++;
        }
        i=i+count[k];
    }
    for(i=0; i<k; i++)
        cout<<endl<<key[i]<<" Occurred "<<count[i]<<" Times\n";
    getch();
}

答案 4 :(得分:0)

我想提交我认为更简单的解决方案:

#include <iostream>
using namespace std;

int main() {
    int n; //number of Elements in the vector
    cin>>n;
    int vec[n]; //vector with all elements
    int v[n];  //vector with Elements without repetition
    int c[n];  // vector which stores the frequency of each element
    for(int i=0; i<n; i++)
        cin>>vec[i];
    int k=0; // number of Elements of the vector without Repetition, in the begining, it has 0 Elements
    int j=0; //logic Counter, could be replaced with bool
    for(int i=0; i<n; i++) {
        for(int h=0; h<=k; h++) {
            if(vec[i]==v[h]) {
                c[h]++;
                j=1;
                break;
            }
        }
//if element i of the original vector is equal to element h of the second vector, then increment the frequency of this element
        if(j==0) { //else if the element is not equal to any of the second vector, the Position of the 2nd vector is filled with the element, which in this case is the first of ist Kind.
            v[k]=vec[i];
            c[k]=1;
            k++;
        } //the number of Elements is increased by one to store another element;
        else {
            j=0;
        }
    }
    cout<<endl<<endl;
    for(int i=0; i<k; i++)
        cout<<v[i]<<":"<<c[i]<<endl;
    return 0;
}

答案 5 :(得分:-1)

/**
 * The methods counts the frequency of each element in an array.
 * 
 * Approach: The method checks if the element is already present in the <strong>Map of frequency</strong>.
 * If it is not present, add it to the map with the frequency 1 else put it in the map with 
 * an increment by one of it's existing frequency.
 * 
 * @param arr list of elements
 * @return frequency of each elements
 */
public static Map<Integer, Integer> countFrequency(int[] arr) {
    Map<Integer, Integer> frequency= new HashMap<Integer, Integer>();
    for(int i = 0; i < arr.length; i++) {
        if(frequency.get(arr[i])==null) {
            frequency.put(arr[i], 1);
        }
        else {
            frequency.put(arr[i],frequency.get(arr[i])+1);
        }
    }
    System.out.println("\nMap: "+frequency);
    return frequency;
}