C++ 一维数组中的数字频率

C++ 一维数组中的数字频率,c++,arrays,frequency,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

我写代码已经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;
}

您似乎从未更新计数器。试试这个:

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";
}

您似乎从未更新计数器。试试这个:

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";
}

我不确定你想用数组做什么。我试着遵循这个逻辑,但很难用所有匿名变量名看到它。看起来您正试图在数组的前面查找重复项,但是变量e从未获得除0以外的任何其他值,因此您将只与数组中的第一项进行比较

您只需在数组中查找以前发生的事件,一旦知道该数字是第一个发生的事件,您只需在数组中查找之后的更多发生的事件:

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;
  }
}

我不确定你想用数组做什么。我试着遵循这个逻辑,但很难用所有匿名变量名看到它。看起来您正试图在数组的前面查找重复项,但是变量e从未获得除0以外的任何其他值,因此您将只与数组中的第一项进行比较

您只需在数组中查找以前发生的事件,一旦知道该数字是第一个发生的事件,您只需在数组中查找之后的更多发生的事件:

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;
  }
}

您可以先使用映射来存储num->frequency,然后使用多重映射来存储frequency=>num

这是有效的解决办法

#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;
}

您可以先使用映射来存储num->frequency,然后使用多重映射来存储frequency=>num

这是有效的解决办法

#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;
}

我想提交我的解决方案,我认为这是一个更简单的方案:

#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;
}

我想提交我的解决方案,我认为这是一个更简单的方案:

#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;
}

没有一个答案:没有一个答案:你能修改一下代码吗,我不能单独理解。我会很感激的。这里的大创意是存储数组,u有从0到9的元素,所以他做foreach,他读取的元素是n,所以假设元素是1,然后存储[1]和1。存储是元素出现的次数。我只是一个初学者,所以我需要在代码中进行一些修改。一个单独的代码在这个阶段很难理解。嗯,问题是so不是一个要求修改代码的地方,你甚至可以得到一个-1,因为你这样要求。你需要了解它是如何工作的,而不是改变你的编码。请你调整代码,我不能单独理解它。我会很感激的。这里的大创意是存储数组,u有从0到9的元素,所以他做foreach,他读取的元素是n,所以假设元素是1,然后存储[1]和1。存储是元素出现的次数。我只是一个初学者,所以我需要在代码中进行一些修改。一个单独的代码在这个阶段很难理解。嗯,问题是so不是一个要求修改代码的地方,你甚至可以得到一个-1,因为你这样要求。你需要了解它是如何工作的,而不是改变你的代码。我希望这能起作用,你会觉得它有用。我希望这能起作用,你也会觉得它有用。