2016-09-25 15 views
-1

こんにちは、私はC++を初めて使っています。単純平均計算機C++が正しく動作しない

私は5つの整数を取る簡単なプログラムを作成しようとしています(審査員のスコアとして)。最大と最小のスコアは破棄され、残りの中央の3つのスコアから平均を計算する必要があります。

私はこれがかなりシンプルだと思っていましたが、何らかの理由で私のプログラムはいつも私よりもやや大きな答えを私に与えてくれました。

また入力が整数でなければならない間に、答えが実数であることを述べるべきです。

以下は私がこれまでに持っているものですが、それはかなり完成していますが、何人かの隠れた児童生徒のエラーが数時間見つけられていません。

私はおそらくこれを行うための他のより効率的な方法のトーンがあることを知っていますが、これは私がちょうどC + +を開始したので私が考えることができる最高でした。

ご協力いただければ幸いです! getSmallestルーチンで

#include <iostream> 
using namespace std; 

int getSmallest(int first, int second, int third, int fourth, int fifth); 

int getLargest(int first, int second, int third, int fourth, int fifth); 

double calculateAverage(int largest, int smallest, int sum); 

int main() 
{ 
    int first, second, third, fourth, fifth; 
    int smallest, largest, sum; 
    //double ave; 

    //read input of 5 scores from judges 
    cin >> first; 
    cin >> second; 
    cin >> third; 
    cin >> fourth; 
    cin >> fifth; 

    smallest = getSmallest (first, second, third, fourth, fifth); 
    largest = getLargest (first, second, third, fourth, fifth); 
    sum = (first + second + third + fourth + fifth); 
    //ave = calculateAverage(largest, smallest, sum); 

    //cout << ave << endl; 

    cout << "The average is " << (double)calculateAverage(largest, smallest, sum) << endl; 

    return 0; 

} 

int getSmallest(int first, int second, int third, int fourth, int fifth) 
{ 
    int smallest = 0; 

    if (first <= smallest) 
    { 
     smallest = first; 
    } 
    if (second <= smallest) 
    { 
     smallest = second; 
    } 
    if (third <= smallest) 
    { 
     smallest = third; 
    } 
    if (fourth <= smallest) 
    { 
     smallest = fourth; 
    } 
    if (fifth <= smallest) 
    { 
     smallest = fifth; 
    } 

    return smallest; 
} 

int getLargest(int first, int second, int third, int fourth, int fifth) 
{ 
    int largest = 0; 

    if (first >= largest) 
    { 
     largest = first; 
    } 
    if (second >= largest) 
    { 
     largest = second; 
    } 
    if (third >= largest) 
    { 
     largest = third; 
    } 
    if (fourth >= largest) 
    { 
     largest = fourth; 
    } 
    if (fifth >= largest) 
    { 
     largest = fifth; 
    } 

    return largest; 
} 

double calculateAverage(int largest, int smallest, int sum) 
{ 
    return (((double)sum) - ((double)largest + (double)smallest))/3.0; 
} 
+0

のために行きますか?入力、期待出力、実際の出力は何ですか? – user463035818

+1

このような問題を解決する適切なツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –

答えて

2

、あなたは

int smallest = INT_MAX; 

またはsmallestが0何でもあなたが入力されます設定する必要があります。

EDIT(INT_MAXが利用できるようにするための<climits>含める):それは動作しますが、効率的ではありません。最初の条件は常にtrueになりますので、あなたは一つのテスト(その場合のINT_MAXのための必要はありません)を救うことができる:

int getSmallest(int first, int second, int third, int fourth, int fifth) 
{ 
    int smallest = first; 

    if (second <= smallest) 
    { 
     smallest = second; 
    } 

同じ最適化を「隠れた生徒エラー」とは何getLargest

int getLargest(int first, int second, int third, int fourth, int fifth) 
{ 
    int largest = first; 

    if (second >= largest) 
    { 
     largest = second; 
    }