C++循环,用于接收输入以填充数组

C++ for loops for taking in input to fill an array

本文关键字:输入 填充 数组 循环 用于 C++      更新时间:2023-10-16

我需要创建一个程序,它接受 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, 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;
}

这就是我目前所拥有的。我知道我需要制作一个 for 循环,但我该怎么做,以便在验证输入后,它会将 6 个输入分配给它在数组"记分表"中的位置?提前谢谢。

#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 << "nnSorted 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;
}