c++ - (C++) 试图完成一个快速程序,但我不确定哪里出错了?

标签 c++ arrays pointers function-pointers selection-sort

程序说明如下。当我尝试编译时,出现错误提示 'cannot convert 'double' to 'double**' for argument

Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings.

注意:我对在 C++ 中使用指针还很陌生,我确实意识到我的代码中的错误对于某些人来说可能相当容易发现,所以请放轻松。此外,如果对我可以改进的地方或在哪里可以找到对指针的其他引用有任何建议,那就太好了!

#include <iostream>
#include <iomanip>

using namespace std; 

//Function Prototypes
void arrSelectionSort( double *[], int );
double testAverage( double *[], int );

int main() { 

    double *testScores; //To dynamically allocate an array

    int numTest;    //To hold the number of test - size of array
    int count;      //Counter variable


    //User input
    cout <<"Please enter the number of test scores: ";
    cin >> numTest; 

    //Dynamically allocate an array for test scores 
    testScores = new double[numTest];

    //Obtain the individual test scores from user
    cout << "\nEnter each test score.\n";
    for ( count = 0; count < numTest; count++ ) { 

        cout << "Test " << ( count + 1 ) << ": " << endl; 
        cin >> testScores[count];

    }

    //Function call to sorting algorithm 
    arrSelectionSort( testScores, numTest );


    //Display sorted array
    cout << "Here are the sorted test scores.\n";
    for ( int x = 0; x < numTest; x++ ) { 

        cout << "\nTest " << (x+1) << ": " << testScores[x];

    } 


    //Display average of all test scores
    cout <<"\nAverage of all test scores is: " << testAverage( testScores, numTest );


    //Free up allocated memory
    delete  [] testScores;
    testScores = 0; 

    return 0;
}

void arrSelectionSort( double *arr[], int size ) { 

    int startScan, minIndex; 
    double *minElement; 

    for ( startScan = 0; startScan < ( size - 1 ); startScan++ ) { 

        minIndex = startScan; 
        minElement = arr[startScan]; 

        for ( int index = startScan + 1; index < size; index ++ ) { 

            if (*(arr[index]) < *minElement) {

                minElement = arr[index]; 
                minIndex = index;  
            }

        }

        arr[minIndex] = arr[startScan]; 
        arr[startScan] = minElement; 
    }
}

double testAverage( double *arr[], int size ) { 

    double average;
    double total; 

    //Accumulating Loop
    for ( int count = 0; count < size; count++ ) { 

        total += arr[count];
    }

    average = total/size; 

    return average; 
}

最佳答案

从函数参数中删除 *:

void arrSelectionSort( double arr[], int size );
double testAverage( double arr[], int size );

您想传入一个 double 值数组,而不是一个 double* 指针数组。

然后,在 arrSelectionSort() 中,去掉 * 的使用,因为你想对值进行排序,而不是对指针进行排序:

void arrSelectionSort( double arr[], int size ) { 

    int startScan, minIndex; 
    double minElement; 

    for ( startScan = 0; startScan < ( size - 1 ); startScan++ ) { 

        minIndex = startScan; 
        minElement = arr[startScan]; 

        for ( int index = startScan + 1; index < size; index ++ ) { 

            if (arr[index] < minElement) {

                minElement = arr[index]; 
                minIndex = index;  
            }

        }

        arr[minIndex] = arr[startScan]; 
        arr[startScan] = minElement; 
    }
}

关于c++ - (C++) 试图完成一个快速程序,但我不确定哪里出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53642517/

相关文章:

c++ - python::boost::对象分配

c++ - 错误 : pasting "operator" and "+" does not give a valid preprocessing token

c++ - 将系统命令的输出存储到 c 中的本地字符数组中

C++ double

java - 字符串数组的问题

c++ - 读取包含小数的文件时出现问题

javascript - 创建具有重复整数的随机 javaScript 数组后 - 避免并排使用相同的整数

c++ - 从字节到十六进制的令人困惑的基数转换

c++ - 向 char* 添加文本

C++ 标准 :string memory model