C++;平均值的数组循环 我目前正在尝试使用C++。到目前为止,我已经学会了制作一个程序,允许我输入10个变量以及5个已经赋值的变量,然后找到这些数字的平均值。但是我不知道怎么做,我为数组做了一个for循环,但似乎没有找到平均值。以下代码有什么问题?这就是我到目前为止所做的: #include <iostream> using namespace std; int cole[10]; int sum = 0; int main() { int a = 10; int b = 10; int c = 10; int d = 10; int e = 35; cout << "Please input ten numbers, one at a time" << endl; cin >> cole[0]; cin >> cole[1]; cin >> cole[2]; cin >> cole[3]; cin >> cole[4]; cin >> cole[5]; cin >> cole[6]; cin >> cole[7]; cin >> cole[8]; cin >> cole[9]; cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl; for(int x = 0; x < 10; x++ ) { int sum = 0; cout << cole[x] << " "; sum += cole[x]; cole[x]++; } sum += cole[0]; cole[0]++; cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl; cout << "The average of these numbers is: "; sum = sum + a + b + c + d + e; cout << sum / 15; } #包括 使用名称空间std; int-cole[10]; 整数和=0; int main() { INTA=10; int b=10; int c=10; int d=10; int e=35; 库特-科尔[0]; cin>>科尔[1]; cin>>科尔[2]; cin>>科尔[3]; cin>>科尔[4]; cin>>科尔[5]; cin>>科尔[6]; cin>>科尔[7]; cin>>科尔[8]; cin>>科尔[9]; 不能
摆脱循环中的C++;平均值的数组循环 我目前正在尝试使用C++。到目前为止,我已经学会了制作一个程序,允许我输入10个变量以及5个已经赋值的变量,然后找到这些数字的平均值。但是我不知道怎么做,我为数组做了一个for循环,但似乎没有找到平均值。以下代码有什么问题?这就是我到目前为止所做的: #include <iostream> using namespace std; int cole[10]; int sum = 0; int main() { int a = 10; int b = 10; int c = 10; int d = 10; int e = 35; cout << "Please input ten numbers, one at a time" << endl; cin >> cole[0]; cin >> cole[1]; cin >> cole[2]; cin >> cole[3]; cin >> cole[4]; cin >> cole[5]; cin >> cole[6]; cin >> cole[7]; cin >> cole[8]; cin >> cole[9]; cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl; for(int x = 0; x < 10; x++ ) { int sum = 0; cout << cole[x] << " "; sum += cole[x]; cole[x]++; } sum += cole[0]; cole[0]++; cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl; cout << "The average of these numbers is: "; sum = sum + a + b + c + d + e; cout << sum / 15; } #包括 使用名称空间std; int-cole[10]; 整数和=0; int main() { INTA=10; int b=10; int c=10; int d=10; int e=35; 库特-科尔[0]; cin>>科尔[1]; cin>>科尔[2]; cin>>科尔[3]; cin>>科尔[4]; cin>>科尔[5]; cin>>科尔[6]; cin>>科尔[7]; cin>>科尔[8]; cin>>科尔[9]; 不能,c++,arrays,loops,average,C++,Arrays,Loops,Average,摆脱循环中的int sum=0和cole[x]++;。同样也要失去: sum += cole[0]; cole[0]++; 在循环之后 int sum = 0; 这需要在循环之外,您不断地重置总和。您声明了两次sum。请从for循环内部删除声明 for(int x = 0; x < 10; x++ ) { cout << cole[x] << " "; sum += cole[x]; cole[x]++; } for(i
int sum=0
和cole[x]++;
。同样也要失去:
sum += cole[0];
cole[0]++;
在循环之后
int sum = 0;
这需要在循环之外,您不断地重置总和。您声明了两次
sum
。请从for循环内部删除声明
for(int x = 0; x < 10; x++ )
{
cout << cole[x] << " ";
sum += cole[x];
cole[x]++;
}
for(int x=0;x<10;x++)
{
cout固定代码:
#include <iostream>
using namespace std;
int cole[10];
int sum = 0;
int main()
{
int a = 10;
int b = 10;
int c = 10;
int d = 10;
int e = 35;
cout << "Please input ten numbers, one at a time" << endl;
cin >> cole[0];
cin >> cole[1];
cin >> cole[2];
cin >> cole[3];
cin >> cole[4];
cin >> cole[5];
cin >> cole[6];
cin >> cole[7];
cin >> cole[8];
cin >> cole[9];
cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;
int sum = 0; //initialize sum outside the for loop
for(int x = 0; x < 10; x++ )
{
cout << cole[x] << " ";
sum += cole[x];
cole[x]++;
}
//sum += cole[0]; //this seems unnecessary
//cole[0]++;
cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
cout << "The average of these numbers is: ";
sum = sum + a + b + c + d + e;
cout << sum / 15;
}
#包括
使用名称空间std;
int-cole[10];
整数和=0;
int main()
{
INTA=10;
int b=10;
int c=10;
int d=10;
int e=35;
库特-科尔[0];
cin>>科尔[1];
cin>>科尔[2];
cin>>科尔[3];
cin>>科尔[4];
cin>>科尔[5];
cin>>科尔[6];
cin>>科尔[7];
cin>>科尔[8];
cin>>科尔[9];
cout请尝试以下代码:
#包括
使用名称空间std;
int-cole[10];
整数和=0;
int main()
{
INTA=10;
int b=10;
int c=10;
int d=10;
int e=35;
双平均值=0;
不能包含
使用名称空间std;
int coleCount=10;
int cole[coleCount];
整数和=0;
int main()
{
INTA=10;
int b=10;
int c=10;
int d=10;
int e=35;
库特·科尔[i];
}
不能将int-sum=0;
移出循环。此代码的作用:sum+=cole[0];cole[0]++;
?为什么要在求和后增加数组项?还包括cole[0]
两次。请开始使用调试器:)仍然存在sum
#include <iostream>
using namespace std;
int cole[10];
int sum = 0;
int main()
{
int a = 10;
int b = 10;
int c = 10;
int d = 10;
int e = 35;
double avg = 0;
cout << "Please input ten numbers, one at a time" << endl;
for (int i=0;i<10;i++) {
cin >> cole[i];
}
cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;
for(int x = 0; x < 10; x++ )
{
cout << cole[x] << " ";
sum += cole[x];
}
cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
cout << "The average of these numbers is: ";
sum = sum + a + b + c + d + e;
avg = sum / 15.0;
cout << avg;
}
#include <iostream>
using namespace std;
int coleCount = 10;
int cole[coleCount];
int sum = 0;
int main()
{
int a = 10;
int b = 10;
int c = 10;
int d = 10;
int e = 35;
cout << "Please input ten numbers, one at a time" << endl;
for(int i = 0; i < coleCount; i++)
{
cin >> cole[i];
}
cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;
for(int i = 0; i < coleCount; i++ )
{
cout << cole[i] << " ";
sum += cole[i];
//cole[x]++; why are you incrementing this?
}
//sum += cole[0]; why?
//cole[0]++; why?
cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
cout << "The average of these numbers is: ";
sum += (a + b + c + d + e);
cout << sum / 15;
}