Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在正确循环代码并平均循环(主要停留在这一点上)C++;_C++_Sum_Average_Break_Do While - Fatal编程技术网

C++ 在正确循环代码并平均循环(主要停留在这一点上)C++;

C++ 在正确循环代码并平均循环(主要停留在这一点上)C++;,c++,sum,average,break,do-while,C++,Sum,Average,Break,Do While,我已经放弃了这项任务一次,但我保存了旧代码,所以如果有什么可以挽救的,我洗耳恭听 我现在有一个do-while循环,它将重复两个问题,就像用户输入的问题一样多,而且效果非常好。但我不知道如何将最终的数字相加,更不知道如何将它们正确地平均,以便将它们转换为百分比 #include<iostream> #include<iomanip> using namespace std; int main() { //declare variables int n

我已经放弃了这项任务一次,但我保存了旧代码,所以如果有什么可以挽救的,我洗耳恭听

我现在有一个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;
}
#包括
#包括
使用名称空间std;
int main()
{
//声明变量
int n=0;
int计数器=0;
双倍得分=0.0;
双倍合计=0.0;
双平均值=0.0;
双倍总分=0.0;
双倍合计=0.0;
//提示用户输入工作分配数
cout>n;
计数器=0;
做
{ 
计数器++;
//提示用户为每个查询输入分数*/count up*/

你的代码有点乱,但我大致了解了你作业的要点

根据你的标题,你似乎在寻找用户输入的X个作业的总数。现在你只跟踪用户输入的最后分数和总数

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

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

scoreTotal += score;
totalTotal += total;  // Terrible name sorry
+=的意思是scoreTotal=score+scoreTotal,它将跟踪累计总和。最后,您需要做的就是scoreTotal/totalTotal来找到平均值。

最终代码如下所示:

#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;
}
#包括
#包括
使用名称空间std;
int main()
{
//声明变量
int n,计数器;
满分,总分,总分=0,总分=0,回答;
//提示用户输入工作分配数
cout>n;
计数器=0;
做
{ 
计数器++;
//提示用户为每个查询输入分数*/count up*/

无法直接解释作业是什么,因此我们不需要从代码中推断。我可以做得更好,我将复制并粘贴参数。编写一个程序,以百分比的形式计算N个课堂练习的总成绩。用户应输入N的值,然后输入N个分数和总数。计算总成绩百分比(所得总分除以可能总分的总和。)然后以百分比的形式输出。输入和输出示例如下所示。要输入多少个练习?练习1得到3分:练习2得到10分:练习2得到7分:练习2得到22分:12您的总分数是30分中的22分,或73.33%。请编辑您的问题,将其包括在内。
p+=i
这一部分的意义是什么?每次循环迭代,s、t和p的值都会被赋予新的值,因此上一次迭代的数据会丢失。是的,这是我们第二周的课,所以我们还没有接触数组。从技术上讲,我们只在教科书的第二章,但这类工作在第三章中介绍,我们没有去在课堂上深入讨论它,这就是为什么我这么努力的原因。你所写的与我之前的代码非常相似。我会尝试一下!所以我输入了你的建议,并将其与Neil Kirk建议的技巧结合起来,以找到平均值,但我正在使用的shell(cpp.sh)警告我scoreTotal和totalTotal未在该函数中初始化。我的谷歌搜索也无法解决该问题。//计算平均值scoreTotal+=score;totalTotal+=total;avg=((scoreTotal/totalTotal)*100)/n;'在循环之前,您需要声明float scoreTotal=0;并且float totalTotal=0;您不需要为此使用数组-它将使用不必要的空间。如果您声明基本类型的变量(int、float等)如果没有初始值,则在写入值之前读取该值是错误的。您不需要在每次循环迭代中重新计算
answer
,除非您打算对中间值执行某些操作。
#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;
}