简体   繁体   English

测试分数的平均值和没有指针的A,B,C,D,F等级的学生人数

[英]test score average and number of students with A,B,C,D,F grade without pointer

I am trying to make a C++ program counting the number of A, B, C, D, F grades without using Pointers. 我试图制作一个C ++程序,不使用Pointer来计数A,B,C,D,F等级的数量。 I thought this would be easy but still have a problem. 我以为这很容易,但是仍然有问题。 My code counts the number of C, D, F grade correctly but whenever I input A(90-100) and B(80-89) scores, It displays weird numbers such as 907517809. Why is this working like this? 我的代码正确地计算了C,D,F等级的数量,但是每当我输入A(90-100)和B(80-89)分数时,它都会显示奇怪的数字,例如907517809。为什么这样工作? it calculates average score right. 它计算平均得分正确。 This might be a basic question, but I'm curious.. Sorry in advance. 这可能是一个基本问题,但我很好奇。

#include <iostream>
using namespace std;

int main(){
int i,testscore,N;
int sum=0;
int Acount,Bcount,Ccount,Dcount,Fcount=0;

std::cout<<"How many test scores? " <<endl;
cin>> N;
    if(N<1){
    std::cout<<"Invalid input. try again"<<endl;
    }
    else if(N>25)
    {
    std::cout<<"1-25 only."<<endl;    
    }
    else if(N>0 && N<25){
    std::cout<<"Total number of test is: "<< N << endl;      
    }
for(i = 0; i < N; i++)
    {
        cout << "Enter the score of students " << i + 1 << ": "; 
        cin >>testscore;

        if(testscore >= 90 && testscore < 100){
        Acount++;
        }

        else if(testscore >= 80 && testscore < 90){
        Bcount++;
        }

        else if(testscore >= 70 && testscore < 80){
        Ccount++;
        }

        else if(testscore >= 60 && testscore < 70){
        Dcount++;
        }
        else if(testscore <60){
        Fcount++;
        }

        sum+=testscore;
    }


std::cout<<"The average test score is: "<<sum/N<<endl;
std::cout<<"The number of A grades: " <<Acount<<endl;
std::cout<<"The number of B grades: " <<Bcount<<endl;
std::cout<<"The number of C grades: " <<Ccount<<endl;
std::cout<<"The number of D grades: " <<Dcount<<endl;
std::cout<<"The number of F grades: " <<Fcount<<endl;

    return 0;
}

Because you've only initialised Fcount to zero. 因为您只将Fcount初始化为零。 You need to assign all the others too. 您还需要分配所有其他人。

int Acount=0,Bcount=0,Ccount=0,Dcount=0,Fcount=0;

You probably already know that without this assignment the variables will have random numbers. 您可能已经知道,没有此分配,变量将具有随机数。

You should be getting a warning about using an uninitialized value. 您应该收到有关使用未初始化值的警告。 It's good practice to compile in the strictest mode you can. 最好以最严格的模式进行编译。 Doing so will help avoid these trivial but time consuming errors. 这样做将有助于避免这些琐碎但耗时的错误。

求将 a/b 转换为 c/d 的步数,使得 0 <a<b<100000 and 0<c<d<100000, or none if there is no ways< div><div id="text_translate"><blockquote><p> <em>原来的问题。</em> 给定两个有效分数a/b和c/d 。 每个转换都将a和b加1 ,然后优化a/b 。 找出将a/b转换为c/d的步骤数,以便0&lt;a&lt;b&lt;100000和0&lt;c&lt;d&lt;100000 ,如果没有办法,则没有。</p></blockquote><pre> #include &lt;iostream&gt; #include &lt;math.h&gt; using namespace std; int gcd(int x, int y) { while(y) { int r=x%y; x=y; y=r; } return x; } int main() { int a, b, c, d; cin&gt;&gt;a&gt;&gt;b&gt;&gt;c&gt;&gt;d; int i=0; while (b&lt;d) { if (a*d&lt;b*c) { a++; b++; a/=gcd(a, b); b/=gcd(a, b); i++; } else if (a*d==b*c) { cout&lt;&lt;i; break; } else { cout&lt;&lt;0; break; } } }</pre><p> 有问题,即输入</p><pre>1 6 2 3</pre><p> 答案是5 ,但不是 output 这里.. 我需要帮助,感谢您的所有好评! </p></div></a<b<100000> - Find the number of steps transforming a/b into c/d so that 0<a<b<100000 and 0<c<d<100000, or none if there is no ways

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获得 10 名学生的平均成绩 c++ - get average grade for 10 students c++ C ++学生得分平均计算器数组问题 - C++ Students score average calculator array problem 在不使用 arrays 或 C++ 中的最小值/最大值的情况下计算测试等级平均值 - Calculating test grade averages without using arrays or min/max in C++ Visual Studio:直接进入语句a()-&gt; f(b(),c(),d())中的f()的热键/方法 - Visual Studio : Hotkey/way to step into f() in statement a()->f(b(),c(),d()) directly (c ++)函数计算平均收益(第一分/总分)? - (c++) Function calculating average returns (first score / total number of scores)? 求将 a/b 转换为 c/d 的步数,使得 0 <a<b<100000 and 0<c<d<100000, or none if there is no ways< div><div id="text_translate"><blockquote><p> <em>原来的问题。</em> 给定两个有效分数a/b和c/d 。 每个转换都将a和b加1 ,然后优化a/b 。 找出将a/b转换为c/d的步骤数,以便0&lt;a&lt;b&lt;100000和0&lt;c&lt;d&lt;100000 ,如果没有办法,则没有。</p></blockquote><pre> #include &lt;iostream&gt; #include &lt;math.h&gt; using namespace std; int gcd(int x, int y) { while(y) { int r=x%y; x=y; y=r; } return x; } int main() { int a, b, c, d; cin&gt;&gt;a&gt;&gt;b&gt;&gt;c&gt;&gt;d; int i=0; while (b&lt;d) { if (a*d&lt;b*c) { a++; b++; a/=gcd(a, b); b/=gcd(a, b); i++; } else if (a*d==b*c) { cout&lt;&lt;i; break; } else { cout&lt;&lt;0; break; } } }</pre><p> 有问题,即输入</p><pre>1 6 2 3</pre><p> 答案是5 ,但不是 output 这里.. 我需要帮助,感谢您的所有好评! </p></div></a<b<100000> - Find the number of steps transforming a/b into c/d so that 0<a<b<100000 and 0<c<d<100000, or none if there is no ways 计算不包括最后分数的平均值 - Calculate the average without including last score C ++中的平均指针位置 - Average pointer position in C++ 将字母等级转换为数字等级 - Translating a letter grade into a number grade function 没有将最低年级的学生返回到主 - function not returning lowest grade of students to main
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM