2016-05-16 12 views
-1

プログラムは、ユーザーが定義した数のテストスコアを保持するのに十分な大きさの配列を動的に割り当てる必要があります。すべてのスコアが入力されたら、配列を昇順でソートする関数に渡す必要があります。平均スコアを計算する別の関数を呼び出す必要があります。プログラムはスコアと平均のソートされたリストを適切な見出しで表示する必要があります。可能であれば、配列表記法ではなくポインタ表記法を使用してください。テストスコア - 入力検証

私が問題にしているのは、プログラムがテストスコアのために負の数を受け入れないようにすることです。

ここにコードがあります。

source.cpp

#include <iostream> 
#include <iomanip> 
#include <string> 
using namespace std; 
void arrSelectSort(float *, int); 
void showArrPtr(float *, int); 
void showAverage(float, int); 
int main() 
{ 
float *scores,    //To dynamically allocate an array 
    total = 0.0,   //Accumulator 
    average;    //To hold the averge scores 
int numScores;    //To hold the number of test scores 
          //Get the number of test scores. 
cout << "How many test scores would you like to process?"; 
cin >> numScores; 
//Dynamically allocate an array large enough to hold that many 
//test scores 
scores = new float[numScores]; 
if (scores == NULL) 
    return 0; 
//Get the test score for each test 
cout << "Enter the test scores below.\n"; 
for (int count = 0; count < numScores; count++) 
{ 
    cout << "Test score #" << (count + 1) << ": "; 
    cin >> scores[count]; 
    while (scores <= 0) 
    { 
     cout << "Zero or negative numbers not accepted.\n"; 
     cout << "Test Score #" << (count + 1) << ": "; 
     cin >> scores[count]; 
    } 
} 
//Calculate the total scores 
for (int count = 0; count < numScores; count++) 
{ 
    total += scores[count]; 
} 
//sort the elements of the array pointers 
arrSelectSort(scores, numScores); 
//Will display them in sorted order. 
cout << "The test scores in ascending order are: \n"; 
showArrPtr(scores, numScores); 
showAverage(total, numScores); 
//Free memory. 
delete[] scores; 
return 0; 
} 
void arrSelectSort(float *array, int size) 
{ 
int startScan, minIndex; 
float minElem; 
for (startScan = 0; startScan < (size - 1); startScan++) 
{ 
    minIndex = startScan; 
    minElem = array[startScan]; 
    for (int index = startScan + 1; index < size; index++) 
    { 
     if (array[index] < minElem) 
     { 
      minElem = array[index]; 
      minIndex = index; 
     } 
    } 
    array[minIndex] = array[startScan]; 
    array[startScan] = minElem; 
    } 
} 
void showArrPtr(float *array, int size) 
{ 
for (int count = 0; count< size; count++) 
    cout << array[count] << " "; 
cout << endl; 
} 
void showAverage(float total, int numScores) 
{ 
float average; 
//Calculate the average 
average = total/numScores; 
//Display the results. 
cout << fixed << showpoint << setprecision(2); 
cout << "Average Score: " << average << endl; 
system("pause"); 
} 

修正使用するsource.cppダブルの代わりに、フロート

#include <iostream> 
#include <iomanip> 

using namespace std; 
void arrSelectSort(double *, int); 
void showArrPtr(double *, int); 
double showAverage(double, int); 
int main() 
{ 
double *scores,    //To dynamically allocate an array 
    total = 0.0,   //Accumulator 
    average;    //To hold the averge scores 
int numScores;    //To hold the number of test scores 
          //Get the number of test scores. 
cout << "How many test scores would you like to process?"; 
cin >> numScores; 
//Dynamically allocate an array large enough to hold that many 
//test scores 
scores = new double[numScores]; 
if (scores == NULL) 
    return 0; 
//Get the test score for each test 
cout << "Enter the test scores below.\n"; 
for (int count = 0; count < numScores; count++) 
{ 
    cout << "Test score #" << (count + 1) << ": "; 
    cin >> scores[count]; 
    while (scores[count] <= 0) 
    { 
     cout << "Zero or negative numbers not accepted.\n"; 
     cout << "Test Score #" << (count + 1) << ": "; 
     cin >> scores[count]; 
    } 
} 
//Calculate the total scores 
for (int count = 0; count < numScores; count++) 
{ 
    total += scores[count]; 
} 
//sort the elements of the array pointers 
arrSelectSort(scores, numScores); 

cout << "The test scores in ascending order are: \n"; 
showArrPtr(scores, numScores); 
showAverage(total, numScores); 

delete[] scores; 
return 0; 
} 
void arrSelectSort(double *array, int size) 
{ 
int startScan, minIndex; 
double minElem; 
for (startScan = 0; startScan < (size - 1); startScan++) 
{ 
    minIndex = startScan; 
    minElem = array[startScan]; 
    for (int index = startScan + 1; index < size; index++) 
    { 
     if (array[index] < minElem) 
     { 
      minElem = array[index]; 
      minIndex = index; 
     } 
    } 
    array[minIndex] = array[startScan]; 
    array[startScan] = minElem; 
    } 
} 
void showArrPtr(double *array, int size) 
{ 
for (int count = 0; count< size; count++) 
    cout << array[count] << " "; 
cout << endl; 
} 
double showAverage(double total, int numScores) 
{ 
double average; 
//Calculate the average 
average = total/numScores; 
//Display the results. 
cout << fixed << showpoint << setprecision(2); 
cout << "Average Score: " << average << endl; 
system("pause"); 
} 

エラー - 重大度コード説明プロジェクトファイルの行の抑制状態 エラーC4716 'showAverage':必須ConsoleApplication9 c:\ users \ kenny \ desktop \ kenny_fepc1.cpp 85
重大度コード説明プロジェクトファイルの行の抑制状態 警告C4101 '平均':参照されていないローカル変数ConsoleApplication9 c:\ users \ kenny \ desktop \ kenny_fepc1.cpp 12

修正を支援してください。ありがとうございました。

while (scores <= 0) 

while (scores[count] <= 0) 

私は推測する、これは宿題である必要があり、あなたはCスタイルの配列を使用するように要求されています

+1

(スコア[回数] <= 0)'あなたのwhileループで。 –

答えて

0

あなたは、入力された値をチェックされていません。ただし、std::vectorを使用すると、動的サイズ(「手動」で扱う必要がない)が増え、ベクトルの並べ替えが簡単になるので、タスクが非常に簡単になります。あなたは、コードに変更を加えたいとき

ところで、私は

for (int count = 0; count < numScores; count++) { 
    scores[count] = -1; 
    while (scores[count] <= 0) { 
     cout << "Test Score #" << (count + 1) << "(has to be >0) : "; 
     cin >> scores[count]; 
    } 
} 

重複コードにループを変更することができますことをお勧めは、常にお尻の痛みです。できるだけ避けるようにしてください(わずか2本の小さな線であっても)。

showAveragedoubleを返すように宣言されていますが、returnがないため、他のエラーが表示されます。

void showAverage(double total, int numScores) 

または関数の最後にreturnステートメントを追加します:どちらか、それは何も返さないように宣言しますが、おそらくしばらく `をテストするためのもの

return average; 
+0

あなたの提案にもう一度while(スコア<= 0)があります。 –

+0

floatではなくdoubleを使用するようにコードを修正する必要があります。余分な基準を見ていない。ここに更新されたコードがあります。私は助けが必要な2つのエラーが発生しています。 – rimgartargaryen

+0

@rimgartargaryen私はπάνταῥεpointによって指摘された間違いを修正し、他のエラー – user463035818