如何在數組中拋出一個值

[英]How to throw out a value in an array


I'm creating a diving calculator program. Asks for the level of difficulty. the program then asks for the 7 scores from judges 1-7, stores them in an array, throws out the smallest and largest values then finds the sum of the the remaining. The remaining is then multiplied by the level of difficulty and 0.6. My issue is with my findLargest function, it's as if i'm not calling it all. CODE:

我正在創建一個潛水計算器程序。要求難度。程序然后要求法官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 个解决方案

#1


0  

Try this! Your printing the index for the largest should be outside the for loop, and some modifications in comparison.

試試這個!您打印最大的索引應該在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

#2


0  

your findlargest function is the same with findsmallest function, so the result is smallest == largest.Please careful~ ^_^

你的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;

}//end largest


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/04/27/725c8369f0bd4f6a702f6ae596120a56.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com