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