如何在数组中抛出一个值

时间:2017-04-27 04:02:34

标签: c++ arrays function calculator

我正在创建潜水计算器程序。要求难度。程序然后要求法官1-7的7个分数,将它们存储在一个数组中,抛出最小和最大的值然后找到剩余的总和。然后将剩余部分乘以难度等级0.6。我的问题在于我的findLargest功能,好像我没有全部调用它。代码:

#include <iostream>

using namespace std;

int judgesScore[7]; //array name
float difficulty; //between 1.2 & 3.8
float finalScore; 



void collectInput() {
    int input;

    for (int i=0; i < 7; i++){
        input = -1;
        while (input < 0 || input > 10) {
            cout << "Enter the score of judge " << i+1 << ": ";
            cin >> input;   
        }
         judgesScore[i]=input;
    }
}//end collectInput

int findsmallest () {
    int smallest = 0;
    for (int i = 1; i < 7; i++){
        if (judgesScore[i] < judgesScore[smallest]){
            smallest = i;
        }

    }
    return smallest;
}//end smallest


int findlargest () {
    int largest = 0;
    for (int i = 1; i < 7; i++){
        if (judgesScore[i] < judgesScore[largest]){
            largest = i;
            cout << "the largest is: "<<largest;
        }

    }
    return largest;
}//end largest

int sumOfScore(){
    int smallest = findsmallest();
    int largest = findlargest();
    int sum = 0;
    for(int i =0; i <7; i++){
        if( i !=smallest && i !=largest){
            sum+= judgesScore[i];

        }
    }
    return sum;

}

int main(int argc, char *argv[]){


    while (!(difficulty >= 1.2 && difficulty <= 3.8) ){
        cout << "Please enter the level of difficulty from 1.2 - 3.8: ";
        cin  >> difficulty;


    }//end of while
        collectInput();
        cout << "the sum of scores is "<<sumOfScore() << endl;

        finalScore = (sumOfScore() * difficulty) * 0.6;
        cout 
             << "at a difficulty level of " << difficulty << "\n"
             << "Final Score: " << finalScore << "\n";

2 个答案:

答案 0 :(得分:0)

试试这个!您打印最大的索引应该在for循环之外,并进行一些修改。

int findlargest () {
    int j = 0, largest = judgesScore[0];
    for (int i = 1; i < 7; i++){
        if (judgesScore[i] < largest){
            largest = judgesScore[i];
            j = i;
        }
    }
    cout << "the largest is: "<< j;
    return j;
}//end largest

答案 1 :(得分:0)

你的findlargest函数与最大函数相同,所以结果最小==最大。请小心〜^ _ ^

int findlargest () {
int largest = 0;
for (int i = 1; i < 7; i++){
    if (judgesScore[i] > judgesScore[largest]){
        largest = i;
        cout << "the largest is: "<<largest;
    }

}
return largest;

} //最大的