2016-11-01 26 views
0

創建了一個程序來提示用戶輸入測試分數並找到平均值,最高值和排序測試分數。我有問題,我似乎無法從我所做的功能中獲得最高的測試分數,當我創建排序測試分數功能時,程序崩潰。有人可以看看我的功能,看看我的問題在哪裏,我似乎可以得到平均測試分數。C++獲得最高測試分數並使用指針

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); 

應該

void arrayAscending(double *, int); 

要排序雙打的數組,而不是指針數組翻一番。

此外,

double *minElem; 

稍後用於沒有任何存儲器被分配它。再一次,你可能只需要

double minElem; 

而不是一個指針。

如果您不需要使用指針,那麼使用std::vector和來自標準庫的算法,如std::sort

+0

感謝您的幫助,你可以看看我更新的代碼運行它沒有任何錯誤,但數組不顯示@vsoftco –

+0

@MikeShasaco在我身邊很好地工作。從終端運行它,你會看到結果。見[這裏](http://coliru.stacked-crooked.com/a/ec36b87e770cb441)。 – vsoftco

+0

我看到最低,最高和平均值的結果,但我沒有看到陣列的結果升序 –