当前位置: 首页 > 知识库问答 >
问题:

这段代码有问题吗?我不能让它工作。请帮帮我

颛孙喜
2023-03-14

其目的是对用户输入的分数进行排序、显示和平均。我还没弄明白。如果你已经解决了这个问题,请帮忙。到目前为止,我已经尝试了这段代码,但它不起作用。

#include<iostream>
using namespace std;

void sortArr(int [], int);
void getAvg(const int [], int);

int main()
{
    int *scorePtr = nullptr;
    int size;

    cout << "Enter the number of test scores: ";
    cin >> size;
    while(size < 0)
    {
        cout << "Invalid entry. Enter the number of scores: ";
        cin >> size;
    }
    if(size == 0)
    {
        cout << "No scores. Terminating program...";
        return 0;
    }

    scorePtr = new int[size];

    cout << "Enter the scores earned on each test: ";
    for (int index = 0; index < size; index++)
    {
        cin >> scorePtr[index];
        while(scorePtr[index] < 0)
        {
            cout << "Entry not valid. Enter score #" << (index + 1) << ": ";
            cin >> scorePtr[index];
        }
    }

    sortArr(scorePtr, size);
    getAvg(scorePtr, size);

    return 0;
}

void sortArr(int *arr[], int SIZE)
{
    int startScan, minIndex;
    int *minElem;

    for(startScan = 0; startScan < (SIZE - 1); startScan++)
    {
        minIndex = startScan;
        minElem = arr[startScan];
        for(int index = startScan + 1; index < SIZE; index++)
        {
            if(*(arr[index]) < *minElem)
            {
                minElem = arr[index];
                minIndex = index;
            }
        }
        arr[minIndex] = arr[startScan];
        arr[startScan] = minElem;
    }
    cout << "Here are the scores you entered, sorted in ascending order: " << endl;
    for(int count = 0; count < SIZE; count++)
        cout << *(arr[count]) << " ";
    cout << endl;
}

void getAvg(const int arr[], int Size)
{
    double total = 0.0;
    double avg = 0.0;
    for (int index = 0; index < Size; index++)
    {
        total += arr[index];
    }
    avg = (total/Size);
    cout << "Your average score is: " << avg << endl;
}

我从这段代码中得到的只是一个编译器错误。有人有什么建议吗?谢谢你。

共有1个答案

何昆
2023-03-14

您将sortarr定义为将指向int的指针数组作为其参数,但您传递给它的是一个实际的int数组。听起来后者才是您真正想要的,因此您需要重写sortarr以便在int数组上正常工作,首先要将定义更改为

void sortArr(int arr[], int SIZE) {
...
}

没有*。这也意味着该函数中的大部分*取消引用将被删除。

 类似资料:
  • 我正在为我的discord机器人制作一个管理cog,我的代码无法识别“ctx”。PyCharm建议用“self”代替“ctx”,我不知道“self”是做什么的。从PyCharm所说的,还有数以百万计的其他东西,我必须写下它是什么。PyCharm无法识别帮会、发送、作者和频道,它还说是一个无法访问的代码。请注意,如果这似乎是一个非常愚蠢的问题,我是一个初学者,两周前就开始了。 至于代码:

  • 我写了这个和我从谷歌和其他来源看到的一些部分,但我不能理解公共静态int[]直方图中的else部分 这个新Arr[计数]如何;作品有人可以向我解释,请

  • 我必须创建程序,这样我就可以输入3个字母加上像“Jan1999”这样的一年,并显示日历。

  • 这个问题已经回答了所以基本上,我只是写下了一个代码,显示100以下的所有素数。这是代码: 继续启动他们的代码,同事们:D

  • 我有这个错误:值错误:检查输入时的错误:预期dense_1_input有形状(6,),但得到了形状(1,)数组,但我的keras模型的输入层model.add(密集(单位=5,kernel_initializer='统一',激活='relu',input_dim=6))所以(6,)维度和输入是输入=np.array([HeadX, HeadY, TailX, TailY, AppleX, Appl

  • ~$npm安装-g yo npm错误!达尔文14.3。0 NPM ERR!"节点"/usr/本地/bin/npm""安装"-g""yo" npm错误!节点v0。12.5 npm错误!npm v2。14 npm错误!路径/usr/local/lib/node_模块/yo npm错误!代码EACCES NPM ERR!errno-13 npm错误!错误:EACCES,rmdir'/usr/local/