【问题标题】:Histogram of arrays (C++)数组直方图 (C++)
【发布时间】:2021-07-02 03:07:31
【问题描述】:

我正在编写一个代码,它使用一个函数来生成一个长度为 10 的随机数组,其中填充了 1 到 20 之间的随机整数。然后它将打印出一个星号直方图,该直方图的数量乘以在数组中打印的数字。

这是一个例子:

Element      Value      Histogram
0            12         ***********
1            5          ******
2            3          ***
etc...

问题是我不知道如何让代码打印出星号的数量以及如何将它们与整数出现的次数相关联。我尝试的是几个用于遍历数组和值的 for 循环,以找出已创建的值。然后还有另一个 for 循环,用于计算整数出现的次数。我知道最后的法庭陈述中有错误。我不确定如何将该部分放入“图表”中。我可以肯定的是,应该有一个 for 循环,用于在另一个 for 循环中打印星号,检查数组的数量。

这是我的代码:

#include <iostream>
#include <iomanip>
#include<ctime>

using namespace std;

void initialize(int arr[]);

int main(){

    int arr[10];

    initialize(arr);

    cout<<endl;

    cout<< left <<setw(10)<< left << "Element"<< right<< setw(10) << "Value" << right<<setw(20)<<"Histogram "<< right<<endl;

    

    cout<<" " << endl;

    int x;

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

    for(x = 1; x <= 20; x++){
        
        if(x != arr[i])
        x++;
        }

        for(int i = 0; i < x; i++){
            cout<<"*";
        }
//exuse the error message, I know it is inncorrect
        cout<< left <<setw(10)<< left << i << right<< setw(10) << x << right<<setw(20)<<for(int i = 0; i < x; i++){
            cout<<"*";
        }<< right<<endl;
    }
    

return 0;

}

void initialize(int arr[]){
       

    for(int i = 0; i < 10; i++){
        arr[i] = rand() % (20) + 1;
    }

    for(int i = 0; i < 10; i++){
        cout<< arr[i]<<" ";
    }
}

【问题讨论】:

  • 这是地图的用例。您将整数存储为键,将出现次数存储为值。 en.cppreference.com/w/cpp/container/map
  • 我可以肯定的是,应该有一个用于打印星号的 for 循环——这当然不是真的。 std::cout &lt;&lt; std:::string(count, '*');,其中count 是星号的数量。这使您的程序的解决方案大约是 2 行 for 循环,可能是单行 for 循环。

标签: c++ arrays loops for-loop


【解决方案1】:

你的代码是c风格的程序,容易出错,我已经尽力达到你的要求了

#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std;

void initialize(int arr[]);

const size_t kArrayNum = 10;
const size_t kMaxVal = 21;
int main() {
  int arr[kArrayNum];

  srand(time(nullptr));
  initialize(arr);

  cout << endl;

  cout << left << setw(10) << left << "Element" << right << setw(10) << "Value"
       << right << setw(20) << "Histogram " << right << endl;

  cout << " " << endl;

  int cnt[kMaxVal] = {0};

  for (int i = 0; i < kArrayNum; i++) {
    cnt[arr[i]]++;
  }
  for (int i = 0; i < kMaxVal; ++i) {
    int x = cnt[i];
    // exuse the error message, I know it is inncorrect
    cout << left << setw(10) << left << i << right << setw(10) << x << right
         << setw(20);
    for (int i = 0; i < x; i++) {
      cout << "*";
    }
    cout << right << endl;
  }

  return 0;
}

void initialize(int arr[]) {
  for (int i = 0; i < kArrayNum; i++) {
    arr[i] = rand() % (kMaxVal);
  }

  for (int i = 0; i < kArrayNum; i++) {
    cout << arr[i] << " ";
  }
}

我强烈建议使用像 vector 这样的 c++ STL 容器重写它。

具有更多 c++ 风格代码的示例:

#include <algorithm>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

std::vector<int> initialize();
const size_t kArrayNum = 10;
const size_t kMaxVal = 21;
int main(int argc, char* argv[]) {
  srand(time(nullptr));
  vector<int> arr = initialize();

  cout << endl;

  cout << left << setw(10) << left << "Element" << right << setw(10) << "Value"
       << right << setw(20) << "Histogram " << right << endl;

  cout << " " << endl;

  std::vector<size_t> cnt(kMaxVal, 0);

  for (int i = 0; i < kArrayNum; i++) {
    cnt[arr[i]]++;
  }
  for (int i = 0; i < kMaxVal; ++i) {
    size_t x = cnt[i];
    cout << left << setw(10) << left << i << right << setw(10) << x << right
         << setw(20);
    cout << string(x, '*');
    cout << right << endl;
  }

  return 0;
}

std::vector<int> initialize() {
  std::vector<int> ans(kArrayNum, 0);
  std::generate(std::begin(ans), std::end(ans),
                [] { return rand() % kMaxVal; });
  for (auto i : ans) {
    cout << i << " ";
  }
  cout << endl;
  return ans;
}

或者对于生产级使用,我建议查看 boost::histogram 或 folly::histogram。

【讨论】:

  • for (int i = 0; i &lt; x; i++) { cout &lt;&lt; "*"; } --> 整个循环可以替换为std::cout &lt;&lt; std::string(x, '*');。或者简单地将&lt;&lt; std::string(x, '*'); 添加到循环上方的cout 行并完全删除循环。
猜你喜欢
  • 2013-06-19
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 2013-08-14
  • 2021-04-19
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多