【问题标题】:Breaking do while looping code properly and averaging the loops (majorly stuck on this) C++在正确循环代码并平均循环时中断执行(主要停留在此)C++
【发布时间】:2015-09-12 23:43:35
【问题描述】:

我已经放弃了这个任务,但我已经保存了旧代码,所以如果有什么可以挽救的,我会全力以赴。

我现在有一个 do while 循环,它会重复两个问题,就像用户输入的一样多,而且效果很好。但是我不知道如何将最终数字加在一起,甚至更不知道如何让它们正确平均,这样我就可以把它们变成一个百分比。

#include<iostream>
#include<iomanip>

using namespace std;

int main() 
{
    //declare variables
    int n = 0;
    int counter = 0;
    double score = 0.0;
    double total = 0.0;
    double avg = 0.0;
    double scoreTotal = 0.0;
    double totalTotal = 0.0;


    //Prompt user for number of assignments
    cout << "How many assignments are there? ";
    cin >> n;

    counter = 0;
    do
    { 
        counter++;

        //prompt user for score    */count up for each query*/
        cout << "What is the score for assignment number " << counter << "? ";
        cin >> score;

        //prompt user for totals         */count up for each total*/
        cout << "What is the total score available for assignment number " << counter << "? ";
        cin >> total;

    }
    while(counter < n);

    //calculate averages

    scoreTotal += score;
    totalTotal += total;

    avg = ((scoreTotal / totalTotal) * 100) / n; 

    //output how much it was out of and percent

    cout << "Your total is " << scoreTotal << " out of " << totalTotal << ", or " <<  avg << "%" << endl;

    return 0;
}

我只是猜测 avg 部分,但结果不正确,所以我知道那是错误的。

我的旧代码让它正确地结束循环周期,但它对新代码的影响很大,比如将计数器更改为仅偶数,并且它输出的提示比用户请求的提示高。

#include<iostream>

using namespace std;

int main() 
{

    float s;  // assignment score
    float t;  // total points worth
    float p;  // percentage     
    int n;

    //input the number of assignments
    cout << "How many assignments are there? ";
    cin >> n;

        for (int i=1; i <=n; i++)
        {
            //input the total points earned for assignment
            cout << "What is the score earned for this assignment? ";
            cin >> s;

            //input the number of points assignment is worth
            cout << "How many points was the assignment worth? ";
            cin >> t;

            //calculate percentage
            p = ((s / t) * 100) ;
            p += i;
        }


    //output score
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "Total score: " << p << "%"<< endl;

    return 0;
}

我的教科书也无济于事。它从解释 Do 和 Do While 循环跳到展示如何使用 For 循环,而没有任何关于它们的兼容性的解释。

任何帮助!谢谢!

【问题讨论】:

  • 请直接解释赋值是什么,我们不需要从你的代码中推断出来。
  • 我可以做得更好,我会复制并粘贴参数。
  • 编写一个程序,以百分比形式计算 N 个课堂练习的总成绩。用户应输入 N 的值,然后输入 N 个分数和总数中的每一个。计算总百分比(获得的总分除以可能的总分。)并将其输出为百分比。示例输入和输出如下所示。输入多少练习? 3 锻炼 1 获得的分数:10 锻炼 1 可能获得的总分:10 锻炼 2 获得的总分:7 锻炼 2 可能获得的总分:12 您的总分是 30 分中的 22 分,即 73.33%
  • 请编辑您的问题以包含此内容。
  • p += i这部分是什么意思?每次循环迭代都会为 s、t 和 p 的值分配新值,因此前一次迭代的数据会丢失。

标签: c++ sum average break do-while


【解决方案1】:

你的代码有点乱,但我有点明白你的任务的要点。

根据您的标题,您似乎正在寻找用户输入的 X 个任务的总数。现在,您只跟踪用户输入的最后一个分数和总分。

例如。 分数 = 5,总分 = 10 - 下一次迭代 分数 = 7,总分 = 20 - 5 和 10 被淘汰。

您需要做的是使用数组来跟踪所有这些(这似乎超出了您的范围),因此我建议将它们全部加起来并跟踪累积分数和总分。为此,您只需添加

scoreTotal += score;
totalTotal += total;  // Terrible name sorry

+= 的意思是 scoreTotal = score + scoreTotal,它会记录累计总和。最后你所要做的就是 scoreTotal/totalTotal 来找到平均值。

【讨论】:

  • 是的,这是我们第二周的课,所以我们还没有接触数组。从技术上讲,我们只学习了教科书的第二章,但这类工作在第三章中有介绍,而且我们没有在课堂上深入讨论,这就是为什么我在这方面苦苦挣扎的原因。你写的和我之前写的代码很相似。我试试看!
  • 所以我输入了您的建议并将其与 Neil Kirk 建议的用于查找平均值的提示相结合,但我正在使用的外壳 (cpp.sh) 警告我 scoreTotal 和 totalTotal 未初始化在那个函数中。我的谷歌搜索也没有解决这个问题。 '//计算平均值 scoreTotal += score;总计 += 总计; avg = ((scoreTotal / totalTotal) * 100) / n; '
  • 你需要声明float scoreTotal = 0;并且浮动总计= 0;循环之前
  • 你不需要一个数组 - 它会占用不必要的空间。
  • 如果你声明一个没有初始值的基本类型(int、float等)的变量,在你写入值之前读取它是错误的。
【解决方案2】:

最终代码如下:

#include<iostream>
#include<iomanip>

using namespace std;

int main() 
{
    //declare variables
    int n, counter;
    double score, total, scoreSum = 0, totalSum = 0, answer;


    //Prompt user for number of assignments
    cout << "How many exercises to input? ";
    cin >> n;

    counter = 0;
    do
        { 
        counter++;

        //prompt user for score    */count up for each query*/

        cout << "What is the score for exercise " << counter << "? ";
        cin >> score;

        //prompt user for totals         */count up for each total*/

        cout << "What is the total points possible for exercise " << counter << "? ";
        cin >> total;


        //calculate averages

        scoreSum += score;
        totalSum += total;

        answer = (scoreSum / totalSum) * 100; 

            }
        while ( counter < n);


    //output how much it was out of and percent

    cout << "Your total is " << scoreSum << " out of " << totalSum << ", or " << fixed << setprecision (2) << answer << "%" << endl;

    return 0;
}

【讨论】:

  • 你不需要在每次循环迭代中重新计算answer,除非你打算对中间值做一些事情。
猜你喜欢
  • 2017-10-17
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
相关资源
最近更新 更多