2016-04-05 6 views
0

次のことを行うように設計されたC++プログラムに対して、次のコードを用意しました。最大値を見つける2つの関数(findLargestと呼ぶ) (それはfindSmallestと呼ぶ)配列内にある。コードは問題なくコンパイルされますが、出力が正しく表示されません。「-9.25596e + 61」と表示される一連の繰り返し番号が表示されます。この問題のトラブルシューティングを手伝っていただければ幸いです。ありがとうございました。C++:出力に関する問題

#include <iostream> 
 
using namespace std; 
 

 
double findLargest(const double LIST[], int); 
 
double findSmallest(const double LIST[], int); 
 
void printArray(const double LIST[], int); 
 

 

 
int main() 
 
{ 
 
\t cout.setf(ios::fixed); 
 
\t cout.setf(ios::showpoint); 
 
\t cout.precision(2); 
 

 
\t const int MAX = 10; 
 

 
\t double list[MAX] = { 32, 54, 67.5, 29, -34.5, 80, 115, 44.5, 100, 65 }; // elements for the array 
 
\t printArray(list, MAX); 
 

 
\t cout << "\n\nThe largest number is: " << findLargest(list, MAX); 
 
\t cout << "\n\nThe smallest number is: " << findSmallest(list, MAX) << "\n\n"; 
 

 
\t system("pause"); 
 
\t return 0; 
 
} 
 

 
double findLargest(const double LIST[], int size)    //function to evaluate the largest elements and output it out 
 
{ 
 
\t double largest = LIST[0]; 
 
\t for (int a = 0; a < size; a++) 
 
\t { 
 
\t \t if (LIST[a] > largest) 
 
\t \t { 
 
\t \t \t largest = LIST[a]; 
 
\t \t } 
 
\t } 
 
\t return largest; 
 
} 
 

 

 
double findSmallest(const double LIST[], int size)  // function to evaluate the smallest and output it out 
 
{ 
 
\t double smallest = LIST[0]; 
 
\t for (int a = 0; a < size; a++) 
 
\t { 
 
\t \t if (LIST[a] < smallest) 
 
\t \t { 
 
\t \t \t smallest = LIST[a]; 
 
\t \t } 
 
\t } 
 
\t return smallest; 
 
} 
 

 
void printArray(const double LIST[], int size)     // function to print array 
 
{ 
 
\t cout << "\n\nThe size of the element is: " << size << "\n\n"; 
 
\t for (int a = 0; a < size; a++) 
 
\t { 
 
\t \t cout << setw(9) << LIST[a]; 
 
\t \t if ((a + 1) % 8 == 0) 
 
\t \t \t cout << endl; 
 
\t } 
 
}

+0

これをデバッガで実行しようとしましたか、おそらくgdbを試してみましたか?チュートリアルをオンラインでご覧ください。また、将来デバッグするのにも役立ちます。 – Sarcoma

+0

あなたのコードをコピー&ペーストしました。それ以外は 'setw()'が宣言されていなかったので、コードを実行する前に '#include 'する必要がありました。しかし、私がそのファイルをインクルードした後、プログラムは必要なだけ実行されました。あなたのプログラムのどこにでも 'iomanip'を入れていますか?もしそうでなければ、それはあなたの問題かもしれません。 – Fearnbuster

答えて

0

私の知る限り見ることができるように、あなたの問題は、あなたがファイルをインクルードする必要があるということです。

<iomanip> 

の行を追加します。

#include <iomanip> 

にしますあなたのコードの始めとそれが何かを助けるかどうかを確認してください。

+0

これは私の問題を修正しました。ありがとうございました! –

+0

@JustinHartleyはい、問題の人はいません。デバッガを使用していますか?もしそうでなければ、あなたはすべきです。私のデバッガーがすぐに問題を検出しました。 iomanipを含めずにコードを実行することさえできませんでした。 – Fearnbuster

関連する問題