基本平均程序中的意外数组行为
Unexpected array behavior in basic averaging program
看来我总是来这里问一些愚蠢的问题,但就这样吧。截至目前,我正在上第一门 compsci 课程,我们正在学习 C++。我之前对 c 有一个非常基本的介绍,所以我以为我会超越我目前的任务。现在我这样做只是为了炫耀,我觉得如果我不实践我以前的概念,它们最终会消失。不管怎样,问题来了!我应该写一些代码,让用户输入他们的首字母,以及一系列考试。现在这应该完成三件事:平均考试,打印出输入的考试,并打印出他们的姓名首字母。好吧,本来很简单的作业,被你搞得一团糟
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string uInitials;
float avgExam = 0, tExam = 0;
int aExams[10] = {'0'};
int i, nExam = 0, cExam;
cout << "Enter your three initials!";
cin >> uInitials;
do
{
cout << "Enter your exam(s) to be averaged. Enter 0 when complete!\n";
cin >> cExam;
aExams[nExam] = cExam; //I used this before nExam was incremented, in order to get nExam while it was '0' That way the first exam score would be properly saved in the first space
nExam++;
tExam += cExam; //This is just to add all the exams up to later calculate the average
}
while(cExam != 0);
avgExam = tExam/(nExam - 1); //subtracted '1' from nExams to remove the sentinel value from calculations.
cout << "The average for initials: " << uInitials << " is: " << avgExam << endl;
cout << "This average was obtained using the following scores that were entered: \n";
for(i = 0; i < (nExam+1); i++)
{
cout << aExams[i] << endl; //Used a for loop to prevent redundancy
}
return 0;
}
前面是我的代码,问题是当我打印出已输入的考试列表时,我遇到了输出错误,其中添加了两个“0”。此外,我觉得我让整个 do{}while() 循环变得一团糟,所以我也想对其进行改进。如果有人能帮助这个可怜的、无知的初学者,我将不胜感激。感谢您的宝贵时间!
它在代码末尾打印出两个 0 的问题是您编写 for 循环的方式造成的。
而不是:
for(i = 0; i < (nExam+1); i++)
{
cout << aExams[i] << endl; //Used a for loop to prevent redundancy
}
使用:
for (i = 1; i < (nExam); i++)
{
cout << aExams[i - 1] << endl; //Used a for loop to prevent redundancy
}
我可以给的一些建议是例如在第 5 行没有必要
将 0 放在 ' ' 之间,甚至不需要使用赋值 = 运算符。
您可以像这样初始化数组:
int aExams[10]{0};
这会将所有元素初始化为 0,但不能用于其他值。
例如,如果您编写,您将不会拥有所有值为 1 的元素
int aExams[10]{1};
如果您打算初始化数组中所有元素的值不是 0,则可以使用 fill_n();函数。
fill_n(aExams, 10, 1);
第一个参数是数组的名称,第二个参数是您要用第三个参数初始化的元素,第三个是您希望所有元素都具有的值。
不要像第 6 行那样将未初始化的变量与 cExam 和 i 变量一起保留。像 cExam=0 一样初始化它; (复制分配初始化)或 cExam(0); (直接初始化)。后者调用 int 内置类型的构造函数。
我在你的 do-while 循环中看到的一个缺点是你不能确保用户将输入 10 个以下的考试,如果用户试图在一个只能容纳的数组中输入 15 个考试,就会发生不好的事情10.
只需将 while 更改为更像这样的内容:
while( cExam != 0 && (nExam<10) );
也可以把do-while循环的前两行写在循环外。
只需要一次告诉用户停止循环 he/she 需要输入 0。没有必要在每次迭代时都告诉他们这一点,而且如果将这两行放在一起,您将获得良好的性能优势在循环外。
看这里我会怎么写代码,有问题可以问我
看来我总是来这里问一些愚蠢的问题,但就这样吧。截至目前,我正在上第一门 compsci 课程,我们正在学习 C++。我之前对 c 有一个非常基本的介绍,所以我以为我会超越我目前的任务。现在我这样做只是为了炫耀,我觉得如果我不实践我以前的概念,它们最终会消失。不管怎样,问题来了!我应该写一些代码,让用户输入他们的首字母,以及一系列考试。现在这应该完成三件事:平均考试,打印出输入的考试,并打印出他们的姓名首字母。好吧,本来很简单的作业,被你搞得一团糟
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string uInitials;
float avgExam = 0, tExam = 0;
int aExams[10] = {'0'};
int i, nExam = 0, cExam;
cout << "Enter your three initials!";
cin >> uInitials;
do
{
cout << "Enter your exam(s) to be averaged. Enter 0 when complete!\n";
cin >> cExam;
aExams[nExam] = cExam; //I used this before nExam was incremented, in order to get nExam while it was '0' That way the first exam score would be properly saved in the first space
nExam++;
tExam += cExam; //This is just to add all the exams up to later calculate the average
}
while(cExam != 0);
avgExam = tExam/(nExam - 1); //subtracted '1' from nExams to remove the sentinel value from calculations.
cout << "The average for initials: " << uInitials << " is: " << avgExam << endl;
cout << "This average was obtained using the following scores that were entered: \n";
for(i = 0; i < (nExam+1); i++)
{
cout << aExams[i] << endl; //Used a for loop to prevent redundancy
}
return 0;
}
前面是我的代码,问题是当我打印出已输入的考试列表时,我遇到了输出错误,其中添加了两个“0”。此外,我觉得我让整个 do{}while() 循环变得一团糟,所以我也想对其进行改进。如果有人能帮助这个可怜的、无知的初学者,我将不胜感激。感谢您的宝贵时间!
它在代码末尾打印出两个 0 的问题是您编写 for 循环的方式造成的。
而不是:
for(i = 0; i < (nExam+1); i++)
{
cout << aExams[i] << endl; //Used a for loop to prevent redundancy
}
使用:
for (i = 1; i < (nExam); i++)
{
cout << aExams[i - 1] << endl; //Used a for loop to prevent redundancy
}
我可以给的一些建议是例如在第 5 行没有必要 将 0 放在 ' ' 之间,甚至不需要使用赋值 = 运算符。 您可以像这样初始化数组: int aExams[10]{0}; 这会将所有元素初始化为 0,但不能用于其他值。 例如,如果您编写,您将不会拥有所有值为 1 的元素 int aExams[10]{1}; 如果您打算初始化数组中所有元素的值不是 0,则可以使用 fill_n();函数。
fill_n(aExams, 10, 1); 第一个参数是数组的名称,第二个参数是您要用第三个参数初始化的元素,第三个是您希望所有元素都具有的值。
不要像第 6 行那样将未初始化的变量与 cExam 和 i 变量一起保留。像 cExam=0 一样初始化它; (复制分配初始化)或 cExam(0); (直接初始化)。后者调用 int 内置类型的构造函数。
我在你的 do-while 循环中看到的一个缺点是你不能确保用户将输入 10 个以下的考试,如果用户试图在一个只能容纳的数组中输入 15 个考试,就会发生不好的事情10.
只需将 while 更改为更像这样的内容: while( cExam != 0 && (nExam<10) );
也可以把do-while循环的前两行写在循环外。 只需要一次告诉用户停止循环 he/she 需要输入 0。没有必要在每次迭代时都告诉他们这一点,而且如果将这两行放在一起,您将获得良好的性能优势在循环外。
看这里我会怎么写代码,有问题可以问我