这是我正在处理的问题:
还可以通过对矢量元素求和来输出总重量。
还输出向量元素的平均值。
还输出最大矢量元素。 到目前为止,这是我的代码
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int NEW_WEIGHT = 5;
vector<float> inputWeights(NEW_WEIGHT);
int i = 0;
float sumWeight = 0.0;
float AverageWeight = 1.0;
int maxWeight = 0;
int temp = 0;
for (i = 0; i < NEW_WEIGHT; i++){
cout << "Enter weight "<< i+1<< ": ";
cout << inputWeights[i]<< endl;
cin>> temp;
inputWeights.push_back (temp);
}
cout << "\nYou entered: ";
for (i =0; i < NEW_WEIGHT- 1; i++) {
cout << inputWeights.at(i)<< " ";
}
cout<< inputWeights.at(inputWeights.size() - 1) << endl;
for (i =0; i < NEW_WEIGHT; i++){
sumWeight += inputWeights.at(i);
}
cout <<"Total weight: "<< sumWeight<< endl;
AverageWeight = sumWeight / inputWeights.size();
cout <<"Average weight: "<< AverageWeight<< endl;
maxWeight= inputWeights.at(0);
for (i =0; i < NEW_WEIGHT- 1; i++){
if (inputWeights.at(i) > maxWeight){
maxWeight = inputWeights.at(i);
}
}
cout<< "Max weight: "<< maxWeight << endl;
return 0;
}
当我运行此代码时,无论我使用什么输入(对于cin&gt;&gt;(...)),我都将零作为输出,我不知道为什么。我可以帮忙吗。
更新
通过摆脱 cout&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; inputWeights [1] - ;&LT; ENDL;
并在程序开头调整向量inputWeights; 。但输出仍然不是它们应该是的。相反,只有前2个输入值使其成为输出。有什么理由吗?谢谢
更新这是正确或正确的代码。希望将来有人帮助。
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int NEW_WEIGHT = 5;
vector <float> inputWeights;
int i = 0;
float sumWeight = 0.0;
float AverageWeight = 1.0;
float maxWeight = 0.0;
float temp = 0.0;
for (i = 0; i < NEW_WEIGHT; i++){
cout << "Enter weight "<< i+1<< ": "<< endl;
cin>> temp;
inputWeights.push_back (temp);
}
cout << "\nYou entered: ";
for (i =0; i < NEW_WEIGHT- 1; i++){
cout << inputWeights.at(i)<< " ";
}
cout<< inputWeights.at(inputWeights.size() - 1) << endl;
for (i =0; i < NEW_WEIGHT; i++){
sumWeight += inputWeights.at(i);
}
cout <<"Total weight: "<< sumWeight<< endl;
AverageWeight = sumWeight / inputWeights.size();
cout <<"Average weight: "<< AverageWeight<< endl;
maxWeight= inputWeights.at(0);
for (i =0; i < NEW_WEIGHT- 1; i++){
if (inputWeights.at(i) > maxWeight){
maxWeight = inputWeights.at(i);
}
}
cout<< "Max weight: "<< maxWeight << endl;
return 0;
}
答案 0 :(得分:2)
您正在制作尺寸为5的vector
:
const int NEW_WEIGHT = 5;
vector<float> inputWeights(NEW_WEIGHT);
// == 0, 0, 0, 0, 0
然后,在您的输入循环中,您需要在结尾添加新值:
inputWeights.push_back (42);
// == 0, 0, 0, 0, 0, 42
然后你输出前五个始终为零的元素。
您需要选择一件事或另一件事:在程序开始时设置矢量的大小,或者只要输入push_back
就增长矢量。两者都是有效的选择。
您可以通过采用现代C ++(如C ++ 11及更高版本)习惯来清理代码并修复问题。您不需要再使用for(int i = 0; i < something; i++)
填写代码。这是一种更简单的方法。
// Size fixed in advance:
vector<float> weights(NUM_WEIGHTS);
for (auto& weight : weights) { // note it's `auto&`
cout << "\nEnter next weight: ";
cin >> weight; // if it was plain `auto` you'd overwrite a copy of an element of `weight`
}
// Size decided by input:
vector<float> weights; // starts empty this time
cout << "Enter weights. Enter negative value to stop." << endl;
float in;
while (cin >> in) {
if(in < 0) {
break;
}
weights.push_back(in);
}
在任何一种情况下,您都可以使用其他基于范围的for
来播放填充的矢量:
cout << "You entered: ";
for (const auto& weight : weights) {
cout << weight << " ";
}
如果您在输入过程中调整了矢量大小,您还需要从输入循环中删除cout << inputWeights[i] << endl;
行 - 正如您所写的那样,您正在阅读尚不存在的元素,并且可能会得到一个数组索引超出范围的异常。
答案 1 :(得分:1)
当您创建定义输入权重时,您将使用默认值将5个项目放入其中。
vector<float> inputWeights(NEW_WEIGHT);
将其更改为
vector<float> inputWeights;
在代码中删除此行或将其注释掉
cout << inputWeights[i]<< endl;
答案 2 :(得分:0)
这是您正在寻找的程序要求。
#include <vector>
#include <iostream>
int main() {
std::vector<double> weights;
double currentWeight = 0.0;
const unsigned numberOfWeights = 5;
std::cout << "Enter " << numberOfWeights << " weights" << std::endl;
unsigned i = 0;
for ( ; i < numberOfWeights; ++i ) {
std::cin >> currentWeight;
weights.push_back( currentWeight );
}
std::cout << "These are the weights that you entered: " << std::endl;
for ( i = 0; i < weights.size(); ++i ) {
std::cout << weights[i] << " ";
}
std::cout << std::endl;
double totalWeight = 0.0;
std::cout << "The total of all weights is: ";
for ( i = 0; i < weights.size(); ++i ) {
totalWeight += weights[i];
}
std::cout << totalWeight << std::endl;
std::cout << "The average of all the weights is: " << (totalWeight / numberOfWeights) << std::endl;
std::cout << "The max weight is: ";
double max = weights[0];
for ( i = 0; i < weights.size(); ++i ) {
if ( weights[i] > max ) {
max = weights[i];
}
}
std::cout << max << std::endl;
return 0;
}
将所有0视为输出的问题的罪魁祸首来自这两行代码:
const int NEW_WEIGHT = 5;
vector<float> inputWeights(NEW_WEIGHT);
与执行此操作相同:
vector<float> inputWeights{ 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
然后使用NEW_WEIGHT
循环最多5个元素,这样在遍历容器时更容易使用inputWeights.size()
。
编辑 - 简明版
#include <vector>
#include <iostream>
int main() {
std::vector<double> weights;
double currentWeight = 0.0;
const unsigned numberOfWeights = 5;
unsigned i = 0;
std::cout << "Enter " << numberOfWeights << " weights" << std::endl;
for ( ; i < numberOfWeights; ++i ) {
std::cin >> currentWeight;
weights.push_back( currentWeight );
}
double totalWeight = 0.0;
double max = weights[0];
std::cout << "These are the weights that you entered: " << std::endl;
for ( i = 0; i < weights.size(); ++i ) {
std::cout << weights[i] << " "; // Print Each Weight
totalWeight += weights[i]; // Sum The Weights
// Look For Max Weight
if ( weights[i] > max ) {
max = weights[i];
}
}
std::cout << std::endl;
std::cout << "The total of all weights is: " << totalWeight << std::endl;
std::cout << "The average of all the weights is: " << (totalWeight / numberOfWeights) << std::endl;
std::cout << "The max weight is: " << max << std::endl;
return 0;
}