C++ C++;用于接收输入以填充数组的for循环
我需要做一个程序,接受6个浮点数,最终我必须在数组中从最小到最大排序,并删除最大和最小的数字C++ C++;用于接收输入以填充数组的for循环,c++,arrays,for-loop,C++,Arrays,For Loop,我需要做一个程序,接受6个浮点数,最终我必须在数组中从最小到最大排序,并删除最大和最小的数字 #include <iostream> using namespace std; int main(){ bool flag; float score1, score2, score3, score4, score5, score6; int size; float scoresheet [6] = {score1, score2, score3, score4, score5, sco
#include <iostream>
using namespace std;
int main(){
bool flag;
float score1, score2, score3, score4, score5, score6;
int size;
float scoresheet [6] = {score1, score2, score3, score4, score5, score6};
cout << "Pleaser enter your score for the gymnast: ";
cin >> score1;
while (cin.fail() || score1 > 10 || score1 < 0)
{
cout << "Invalid score!" << endl;
cout << "Pleaser enter your score for the gymnast: ";
cin >> score1;
}
#包括
使用名称空间std;
int main(){
布尔旗;
浮动计分1分,计分2分,计分3分,计分4分,计分5分,计分6分;
整数大小;
浮动记分表[6]={score1,score2,score3,score4,score5,score6};
cout>1分;
而(cin.fail()| |分数1>10 | |分数1<0)
{
不能包含
#包括
#包括
使用名称空间std;
int main(int argc,字符**argv){
array myArray;//创建和设置6个元素的数组
float number;//用于存储单个数字
数量;
myArray[i]=number;//将数字添加到数组中
}
如果你要单独声明(并使用)score1
,score2
等,为什么还要有记分表[6]<代码>?把你的验证循环放进for循环中。使用数组。我认为这是正确的方法,使数组“RykAppAdvices未初始化的分数初始化数组是不明确的行为。也使用<代码> <代码>,不是数组。它对你的健康有好处。”Rook:我会亲自向任何一个C++类的导师提出建议。这样的限制,告诉他他的课堂材料很烂。用C数组赢不了什么,也会失去很多。但是,是的,这闻起来像是家庭作业。编辑器允许你写完整的代码,请检查我的编辑。顺便问一下,你为什么在size()上使用decltype
?停止的目的是什么;
?你似乎从来没有真正使用过它。
#include <iostream>
#include <array>
#include <algorithm>
using namespace std;
int main(int argc, char**argv) {
array<float,6> myArray;//create and array of 6 elements
float number;//to store the individual numbers
cout << "Please type 6 numbers: ";
for(size_t i = 0; i < myArray.size(); ++i)
{
if(i==6){
break;
}
cin >> number;
myArray[i] = number;//adding the numbers to the array
}
cout << "\nUnsorted array:" << endl;
for(size_t i = 0; i < myArray.size(); ++i)
cout << myArray [i] << " ";
cout << "\n\nSorted Array:" << endl;
sort(myArray.begin(), myArray.end());
for(size_t i = 0; i < myArray.size(); ++i)
cout << myArray [i] << " ";
cout << endl;
//Smallest number
float smallest = 1000;
for(size_t i = 0; i < myArray.size(); ++i)
{
if(smallest > myArray[i])
smallest = myArray[i];
}
//Biggest number
float biggest = 0;
for(size_t i = 0; i < myArray.size(); ++i)
{
if(biggest < myArray[i])
biggest = myArray[i];
}
cout << "The Smallest number is: " << smallest << endl;
cout << "The Biggest number is: " << biggest << endl;
return 0;
}