2016-11-01 3 views
0

は、テストスコアを入力し、平均、最高、およびテストスコアをソートするようにユーザに促すプログラムを作成しました。私は私が作った関数から最高のテストスコアを得ることができない問題を抱えています。ソートテストスコア関数を作成するとプログラムがクラッシュします。誰かが私の機能を見て、私の問題がどこにあるかを見ることができます、私は平均的なテストのスコアを表示するようです。最高のテストスコアを得てポインタを使用する

double getAverage(double*, int); 
double gethighest(double*, int); 
double getlowest(double*, int); 
void arrayAscending(double*, int); 

int main() 
{ 
    // Variable declarations 
    double *testScores; // Dynamically allocate an array of test scores 
    double average, highest, lowest;  
    int numTests;  // Number of test scores 
    int count;   // Counter variable 

         // Get the number of tests 
    cout << "How many tests do you wish to process? "; 
    cin >> numTests; 

    // Verify input is not a negative number 
    while (numTests <= 0) 
    { 
     // Get input again. 
     cout << "Please enter a positive number: "; 
     cin >> numTests; 
    } 

    // Dynamically allocate an array large enough to hold the test scores 
    testScores = new double[numTests]; 

    // Get the specified number of test scores 
    cout << "Enter the test scores below.\n"; 
    for (count = 0; count < numTests; count++) 
    { 
     cout << "Test " << (count + 1) << ": "; 
     cin >> *(testScores + count); 

     // Verify input is not a negative number 
     while (*(testScores + count) < 0) 
     { 
      // Get input again. 
      cout << "Please enter a valid test score.\n"; 
      cin >> *(testScores + count); 
     } 
    } 

    // Calculate the average test score 
    average = getAverage(testScores, numTests); 
    highest = gethighest(testScores, numTests); 
    lowest = getlowest(testScores, numTests); 
    // Display the results. 

    arrayAscending(testScores, numTests); 

    cout << endl; 
    cout << "The average of those scores is: " << average << endl; 

    cout << "The higest of those scores is: " << highest << endl; 
    cout << "The lowest of those scores is: " << lowest << endl; 

    // Free dynamically allocated memory 
    delete[] testScores; 
    testScores = 0;  // Make testScores point to null 
    return 0; 
} 

//function getAverage - calculates the average of the test scores 
double getAverage(double* scores, int num) 
{ 
    double avg; 
    double total = 0.0; 
    for (int count = 0; count < num; count++) 
    { 
     total += scores[count]; 
    } 
    avg = total/num; 
    return avg; 
} 

double gethighest(double* scores, int num) 
{ 
    double highnum = 0.0; 

    for (int i = 0; i < num; i++) 
    { 
     if (scores[i] > highnum) 
      highnum = scores[i]; 

    } 
    return highnum; 
} 

double getlowest(double* scores, int num) 
{ 
    double lowestnum = 100; 

    for (int i = 0; i < num; i++) 
    { 
     if (scores[i] < lowestnum) 
      lowestnum = scores[i]; 
    } 
    return lowestnum; 
} 

void arrayAscending(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; 
    } 
} 

答えて

3
void arrayAscending(double *[], int); 

あなたがdoubleの配列ではなく、倍増へのポインタの配列をソートする

void arrayAscending(double *, int); 

でなければなりません。

また、

double *minElem; 

はそれのために割り当てられている任意のメモリずに後で使用されます。繰り返しますが、おそらくちょうど

double minElem; 

が必要です。

ポインタを使用する必要がない場合は、std::vectorstd::sortのような標準ライブラリのアルゴリズムを使用します。

+0

あなたの助けてくれてありがとう私の更新されたコードを見てもらえますがエラーはありませんでしたが配列は@vsoftcoに表示されません –

+0

@MikeShasaco私の側でうまく動作します。端末から実行すると結果が表示されます。 [ここ](http://coliru.stacked-crooked.com/a/ec36b87e770cb441)を参照してください。 – vsoftco

+0

最低、最高、平均の結果が表示されますが、arrayAscendingの結果は表示されません –

関連する問題