【问题标题】:C++ inserting array values into another array, but combining similar values into the same elementsC ++将数组值插入另一个数组,但将相似的值组合到相同的元素中
【发布时间】:2018-02-21 06:07:16
【问题描述】:

到目前为止,我的大部分功能都已正常工作。我现在被困在最后一步。我需要从我的主函数到我的“组织”函数中获取一个数组。我需要在特定范围内获取重复值,并将它们分别放入同一个数组元素中并计算它们。完成后,我将打印出带有星号的直方图。范围是-----bin 0. if score = 10 but

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

/*int histo(int x);*/
double dev(int count, int* scores, double mn);
double mean(int count, int* stats);

int main()
{
    int scores[101];
    int count = 0;
    int bin[10];
    double mn;

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

    do
    {
        cin >> scores[count++];
    } while (scores[count - 1] != -1);
    count--;
    mn = mean(count, scores);
    cout << "mean: " << mean(count, scores) << endl;
    cout << "dev: " << dev(count, scores, mn) << endl;
    system("pause");
    return 0;
}

int histo(int* scores)
{
    int bins[10]{};
    int counter = 0;

    for (int i = 0; i < *scores; i++)
    {
        if (*scores < 10)
        {
            bins[counter++];
        }
        else if (*scores >= 10 && *scores < 20)
        {
            bins[counter++];
        }
        else if (*scores >= 20 && *scores < 30)
        {
            bins[counter++];
        }
        else if (*scores >= 30 && *scores < 40)
        {
            bins[counter++];
        }
        else if (*scores >= 40 && *scores < 50)
        {
            bins[counter++];
        }
        else if (*scores >= 50 && *scores < 60)
        {
            bins[counter++];
        }
        else if (*scores >= 60 && *scores < 70)
        {
            bins[counter++];
        }
        else if (*scores >= 80 && *scores < 90)
        {
            bins[counter++];
        }
        else if (*scores >= 90)
        {
            bins[counter++];
        }
        for (int j = 0; j < )
    }
}

double dev(int count, int* scores, double mn)
{
    double x = 0;
    double y = 0;
    double d = 0;

    for (int i = 0; i < count; i++)
    {
        x = pow(scores[i] - mn, 2);
        y += x;
    }
    d = sqrt(y / count);
    return d;
}
double mean(int count, int* scores)
{
    double total = 0;

    for (int i = 0; i < count; i++)
    {
        total += scores[i];
    }

    return total / count;
}

我知道我已经用 if 语句杀死了。这就是我不确定该怎么做的地方。

【问题讨论】:

  • 'else' 已经暗示分数大于或等于前面的语句。此外,取消引用分数指针并不表示数组的结尾。你现在拥有的几乎拥有它
  • 如果您确实保持在分数范围内,您将超出垃圾箱的范围,因为您每次遇到任何语句时都会增加计数器。换句话说,您当前的解决方案充满了未定义的行为

标签: c++ arrays counter


【解决方案1】:
 for (int i = 0; i < *scores; i++)

好的,所以 score 是一个指向数组的指针。通过取消引用它,您可以获得该数组的第一个元素的值,但不是它的计数/大小。

 if (*scores < 10)
 {
    bins[counter++];
 }
 else if (*scores >= 10 && *scores < 20)
 {
    bins[counter++];
 }

bins[counter++] 使用此设置很快就会超出范围,因为您的 scores 数组 > 10(不考虑未定义的行为)。此外,else 语句已经暗示 *scores >= 前一个语句。

所以解决这个问题的方法是这样的,假设你每次都想增加索引 n 上的 bin:

int histo(int *scores, int scoreCount)
{
    int bins[9]{};

    for (int i = 0; i < scoreCount; i++)
    {
        if (scores[i] < 10)
        {
            bins[0]++;
        }
        else if (scores[i] < 20)
        {
            bins[1]++;
        }
        else if (scores[i] < 30)
        {
            bins[2]++;
        }
        else if (scores[i] < 40)
        {
            bins[3]++;
        }
        else if (scores[i] < 50)
        {
            bins[4]++;
        }
        else if (scores[i] < 60)
        {
            bins[5]++;
        }
        else if (scores[i] < 70)
        {
            bins[6]++;
        }
        else if (scores[i] < 90)
        {
            bins[7]++;
        }
        else
        {
            bins[8]++;
        }
    }

    // Do stuff with your bins
}

【讨论】:

  • 这帮了很多忙。我在接下来要采取的步骤上弄错了。我的 histo 函数只是像您展示的那样重新创建 bins 数组。有道理,我不知道为什么我没有像以前那样考虑越界。所以这很好。我现在应该将此数组从该函数传递给主函数,然后以直方图样式布局打印该数组。我知道它需要使用两个 for 循环,但是我在弄清楚如何将数组传递回 main 时遇到了问题。
  • 您要么在histo 函数中分配新内存并返回(不要忘记删除它),要么将bins 作为参数传递给函数。我建议第一个选项,因为如果您更改评分机制,您不会忘记增加/减少 bins 数组的大小。
  • @J_Roost312 看起来像这样:int *bins = new int[9]; 然后返回。当你完成它时,你打电话给delete[] bins;
【解决方案2】:

我相信你让 histo 函数变得比它需要的复杂得多。

void histo(int* scores)
{
    int bins[10] = {0};

    for (int i = 0; scores[i] != -1 ; i++)
    {
        int index = scores[i]/10;
        bins[index]++;
    }

    for(int i = 0; i < 10; i++){
        cout << i + 1;
        for(int j = 0; j < bins[i]; j++){
            cout << "*";
        }
        cout << endl;
    }
}

【讨论】:

  • 这假定分数数组的最后一个条目 == -1。不过,我在 OP 的代码中看不到任何地方发生这种情况。
  • @Neijwiert while (scores[count - 1] != -1);
  • 好吧,我错过了。但这仍然希望用户输入 -1 作为他的最后一个分数。这只是糟糕的代码实践 IMO
  • @Neijwiert 我只是使用他定义输入的方式。
  • 我明白了,但这并不意味着您不能就只要求未定义行为的内容提供建议。在这种情况下,您还依赖于 0 到 9 的分数。但我认为这种混乱也应归咎于 OP,因为他/她并不清楚所需的输出是什么。
猜你喜欢
  • 2022-01-25
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多