平均值的C++数组循环
C++ Array loop for average
我目前正在尝试使用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;
}
提前感谢
请尝试此代码:IDEONE
#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;
}
请记住,平均值不应该是一个整数值,它会将数字四舍五入到底。记住,在循环中也可以将每个值输入到数组中,不要一个接一个地输入。您还在循环中一次又一次地声明"int sum=0",因此全局和在循环中不可见。我也删除了一些不需要的代码。您可以查看它,欢呼
#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;
}
在循环中去掉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]++;
}
固定代码:
#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;
}
相关文章:
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 使用for循环检查数组中的重复项
- 并行用于C++17中数组索引范围内的循环
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 程序在遍历所有值之前离开循环. 数组的
- 在C++中循环数组时访问结构属性
- 如何创建循环数组
- 查找 for 循环数组C++中存在最大值和最小值的值
- 有没有办法在 c++ 中创建循环数组
- OpenMP-每个线程的循环/数组界限
- 循环数组 - 从最后一个跳到第一个,反之亦然
- 循环数组时显示'stack smashing detected abc2 terminated Aborted'
- 使用循环数组实现矢量
- 为什么在循环数组时会得到奇怪的整数
- 循环数组-超出值
- 用于循环数组赋值的C++.正在返回垃圾邮件
- 在RapidJson中循环数组并获取对象元素
- 对于循环数组迭代,if循环测试索引是否为真/假,计算结果不正确
- c++循环/数组
- 有没有一种方法可以简化这个c++整数循环数组?