【问题标题】:Histogram using arrays in C++在 C++ 中使用数组的直方图
【发布时间】:2016-10-27 02:29:49
【问题描述】:

我正在尝试使用来自用户输入的数组馈送到我的直方图函数中,并比较数组的每个实例中的值,以便有一个计数器打印出要显示的“*”的数量。

使用直方图的打印,我试图将其分成箱,即箱 9| *** 3 个实例的分数约为 90,bin 8| ***** 用于 80 到 89 点值数组中的 5 个实例,依此类推。

我已经尝试在我的 for 循环中使用 bins[i] 上的指针,但是它不会像我尝试过的 &bins[i] 一样有效,但这只会给出 1 个 bin 的地址,并且不会显示后续的 bin 或 * from数组中的计数器。

以下是我用来尝试创建此直方图的测试。案例 1:100、95、90、85、80、75、70、65、60、40、20 和 5。为了缩短代码,我将发布我的主要功能和直方图功能。感谢您的宝贵时间。

int main()
{
    int scores[100];
    int bins[10];
    int counter[99];
    int count = 0;

    cout << "Enter a score (-1 to stop): ";

    do {
        cin >> scores[count++];
    } while (scores[count - 1] != -1);
    count--;
    Histogram(scores, count, counter);
    int i;
    for (i = 0; i < 10; i++)
        ;
    {
        cout << &bins[i] << "| " << endl;
        for (size_t k = 0; k < counter[i]; k++) {
            cout << "*" << endl;
            break;
        }
    }
    deviation(scores, count);

    return 0;
}

int* Histogram(int scores[], int count, int counter[])
{

    int k = 0;
    for (int i = 0; i < count; i++) {
        if (scores[i] >= 90) {
            counter[k++];
        }

        else if (scores[i] >= 80 && scores[i] < 90) {
            counter[k++];
        }

        else if (scores[i] >= 70 && scores[i] < 80) {
            counter[k++];
        }

        else if (scores[i] >= 60 && scores[i] < 70) {
            counter[k++];
        }

        else if (scores[i] >= 50 && scores[i] < 60) {
            counter[k++];
        }

        else if (scores[i] >= 40 && scores[i] < 50) {
            counter[k++];
        }
        else if (scores[i] >= 30 && scores[i] < 50) {
            counter[k++];
        }
        else if (scores[i] >= 20 && scores[i] < 30) {
            counter[k++];
        }
        else if (scores[i] >= 10 && scores[i] < 20) {
            counter[k++];
        }
        else if (scores[i] < 10) {
            counter[k++];
        }
    }
    return counter;
}

【问题讨论】:

  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行逐行检查您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 到底是什么问题?编译和运行代码时会发生什么?你有任何错误吗? tyke 的输出与您想要的有何不同?
  • 我在编译运行时没有任何错误。输出如下,获取用户输入的分数,-1打破输入,显示bin的地址“|”我所追求的是9| *** 8| ** 7| ** 6| ** 5| 4| * 3| 2| * 1| 0| * 每一个单独的一行
  • 您的直方图函数没有意义。如果 scores[i] 小于 10、大于 90 或介于两者之间,它会增加局部变量 k。它什么也没做。
  • if 语句是我试图用来测试数组 score[i] 中的值。如果它等于那个集合 rang(scores[i] >= 80 && scores[i]

标签: c++ arrays function


【解决方案1】:
int k = 0;
counter[k++];

上面的语句没有做任何事情。它递增k,但不会更改counter[index] 的值。也许你的意思是counter[k]++; k++;

if (scores[i] >= 90) counter[k++];
else if (scores[i] >= 80 && scores[i] < 90) counter[k++];
else if (scores[i] >= 70 && scores[i] < 80) counter[k++];
...
else if (scores[i] < 10) counter[k++];
else 
    printf("never reaches here!\n");

if/else 条件错误。 scores[i]可以是任意值,始终满足侵权k条件,永远不会到达最后一个条件。

for (i = 0; i < 10; i++); <== extra ;
{
    cout << &bins[i] << "| " << endl;

还有其他错误,不清楚目标是什么。要打印直方图,您可以尝试以下方式(这将打印旋转 90 度的直方图,我不知道这是否接近您的想法)

#include <vector>
...
std::vector<int> scores;
for (int i = 0; i < 10; i++)
    scores.push_back(rand()%10);

for (int i = 0; i < scores.size(); i++)
{
    for (int k = 0; k < scores[i]; k++)
    {
        cout << "*";
    }
    cout << "\n";
}

或沿 x 轴打印直方图

int main()
{
    std::vector<int> scores;
    int max = 10;
    for (int i = 0; i < 20; i++)
        scores.push_back(rand() % max);

    for (auto i : scores)
        cout << i << ", ";
    cout << "\n";

    for (int x = 0; x < max; x++)
    {
        for (int y = 0; y < scores.size(); y++)
        {
            if ((max - scores[y]) > x)
                cout << " ";
            else
                cout << "*";
        }
        cout << "\n";
    }

    return 0;
}

【讨论】:

  • 感谢您的意见。我将使用向量重新开始。
  • 我想你只需要我做的直方图,但你必须把它翻转 90 度?您将不得不修改循环。
【解决方案2】:

希望对您有所帮助,它适用于 10 个输入值,整数范围为 1 到 20!

#include<iostream>
using namespace std;
int main()enter code here
{
    int array[10]={0};
    for(int i=0; i<10; i++)
    {
        cout<<"enter a number ";
        cin>>array[i];
        
    }
    for(int a=0; a<10; a++)
    {
        if(array[a]<=20)
        {
        for(int b=1; b<=array[a]; b++)
        cout<<"#";
        cout<<"\n";
        }
        else 
        cout<<"please enter number less than or equal to 20\n";
    }
}

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 2013-06-19
    • 2020-07-30
    • 1970-01-01
    • 2018-10-03
    • 2016-10-31
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多