【问题标题】:Dynamically add variables in C++在 C++ 中动态添加变量
【发布时间】:2012-11-04 23:52:15
【问题描述】:

我正在为学校做一个项目。是这样的:

您应该能够输入 n 名学生的权重。计算学生的平均体重并输出有多少学生体重低于 65 公斤。

现在我有这个 C++ 源代码示例:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int number_of_students;

    cout << "How many students would you like to add?: ";
    cin >> number_of_students;
    cout << endl;
    cout << endl;

    cout << "--------------------------------------------" << endl;
    cout << "---------- ENTER STUDENT'S WEIGHT ----------" << endl;
    cout << "--------------------------------------------" << endl;
    cout << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

这基本上没什么,因为我目前被卡住了。当用户输入例如 6 个学生时,我不知道我可以为谁添加例如 6 个新的权重变量。

编辑:

我可以计算平均体重,找出有多少学生的体重低于 65 公斤。只有我坚持定义将添加多少学生的变量数量。 计算学生的平均体重并输出有多少学生体重低于 65 公斤。

【问题讨论】:

    标签: c++ dynamic-variables


    【解决方案1】:

    您可以在循环中使用一个变量。示例:

    for (int i = 0; i < number_of_students; i++) {
        int weight;
        cin >> weight;
        if (weight < 65)
            result++;
    }
    

    【讨论】:

    • 这实际上比存储所有权重然后计算平均值更有效(+1),但对于更复杂的问题,它通常更容易出错,而且功能更强大的方法的开销通常不会没关系。
    • 我只是觉得如果是你的学业,你还没有学过数组,你应该在循环中使用局部变量
    【解决方案2】:

    您需要将权重存储在某种大小可变的容器中。非常推荐使用标准库中的容器,最典型的选择是std::vector

    #include<vector>
    #include<algorithm>  //contains std::accumulate, for calculating the averaging-sum
    
    int main(int argc, char *argv[])
    {
        int number_of_students;
    
        cout << "How many students would you like to add?: ";
        cin >> number_of_students;
        cout << endl;
        cout << endl;
    
        cout << "--------------------------------------------" << endl;
        cout << "---------- ENTER STUDENT'S WEIGHT ----------" << endl;
        cout << "--------------------------------------------" << endl;
        cout << endl;
    
        std::vector<float> weights(number_of_students);
    
        for(int i=0; i<number_of_students; ++i) {
          cin >> weights[i];
        }
    
        cout << "Average is: " << std::accumulate(weights.begin(), weights.end(), 0.f)
                                          / number_of_students
          << std::endl;
    
        return EXIT_SUCCESS;
    }
    

    【讨论】:

    • std::accumulate 是做什么的?
    • @Zlatan The documentation 无所不知。
    • @Zlatan:实际上我没有正确使用它,等等......现在修复。
    【解决方案3】:

    使用 new 运算符创建一个整数数组

    cin >> number_of_students;
    int* x = new int[number_of_students];
    

    您现在有一个 size=number_of_students 数组。用它来存储重量。


    编辑 以这种方式处理它并不是最好的(处理内存泄漏等)。请注意 cmets 和其他答案,尤其是。使用 no intermediate storage 和基于 std::vector 的解决方案。

    【讨论】:

    • 我来自 JavaScript 和 PHP 背景...这就像一个数组?
    • 请记住,在您使用 new[...] 的地方,您需要在其他地方使用 delete[] 以确保没有内存泄漏。
    • 是的,真的......不要这样做,@Zlatan。标准容器更安全、更可靠、更简单……如今,使用手动分配的数组几乎没有任何真正的优势,它只会让您多担心一件事。
    【解决方案4】:

    也许没有得到这个问题,但你可以创建一个一定长度的数组,比如

    int* a = new int[number_of_students];
    
    for (int i = 0; i < number_of_students; i++) {
        cin >> a[i];
    }
    

    希望它有所帮助,祝你好运......

    【讨论】:

      猜你喜欢
      • 2014-04-13
      • 2013-09-03
      • 2021-11-01
      • 1970-01-01
      • 2015-02-22
      • 1970-01-01
      • 2014-02-14
      • 2016-09-15
      相关资源
      最近更新 更多