简体   繁体   English

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

[英](C++) Trying to finish up a quick program but I'm not sure where I went wrong?

The program description is below. 程序说明如下。 When I try to compile, I get errors saying 'cannot convert 'double' to 'double**' for argument . 当我尝试编译时,出现错误,说'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. 程序应显示分数和平均值的排序列表,并带有适当的标题。

NOTE: I am fairly new to using pointers in C++ and I do realize that the error in my code may be fairly easy for some to spot, so please do go easy. 注意:我对使用C ++中的指针还很陌生,并且我意识到我的代码中的错误对于某些人来说可能很容易发现,所以请放轻松。 Also, if there are any suggestions as to what I can improve on or where I might find additional references to pointers, that would be great! 另外,如果对我可以改进的地方或在哪里可以找到指向指针的其他引用有任何建议,那就太好了!

#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; 
}

Remove the * from your function parameters: 从函数参数中删除*

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

You want to pass in an array of double values, not an array of double* pointers. 您想传入一个double值数组,而不是一个double*指针数组。

And then, inside of arrSelectionSort() , get rid of the uses of * since you want to sort values, not sort pointers: 然后,在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; 
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在C ++中使用double。 不知道我要去哪里错了 - Trying to use doubles in C++. Not sure where I'm going wrong C++ 新手,我不确定我做错了什么 - New to C++, I'm not sure what I did wrong 我正在尝试从文本文件中读取并将它们分类为结构的不同成员,但我不知道哪里出了问题 - I'm trying to read from a text file and categorize them into different members of structure but i don't know where it went wrong 如何在程序退出之前确保所有c ++ boost线程完成? - How can I make sure all of my c++ boost threads finish before the program exits? C++ 游戏出现错误,我很确定指针有问题。 但我不能告诉 - Having an error with C++ game, I'm pretty sure it's something wrong with the pointers. but i cant tell 我正在尝试完成一个程序,但程序崩溃了,我找不到逻辑错误 - I'm trying to finish a program and the program crashes and I can't find the logic error 我正在尝试用 c++ 创建一个货币转换程序,我不知道如何继续 - I'm trying to create a currency conversion program with c++, I don't know how to continue C ++无限代码吊死了吗? 冷冻? 我不确定 - C++ Infinite Sum Code Hanging? Freezing? I'm not sure 您好,有关函数的程序(按引用传递/按值传递),我不完全确定我的程序出了什么问题 - Hello, the program on Functions (pass by reference/pass by value) and I'm not entirely sure what is wrong with my program 我是 C++ 的新手,我得到了这个错误帮助。 我很确定这是一个简单的解决方法,但我查找的答案没有意义 - I'm new to C++ and I got this error help. I'm pretty sure its an easy fix but the answers i looked up didn't make sense
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM