如何创建一个数组来匹配学生的名字和他们的分数? #包括 #包括 #包括 无效*交换(整数arr[],整数大小); 无效*交换(整数arr[],整数大小){ int i,j,温度; 对于(inti{0};i“运算符>”/Cord>错误,因为您不能比较C++中的结构。如果更改,如果您的代码将(如[ARR[i]>ARR[j]) > < >(ARR[i],等级> ARR[j]。。我现在重编了所有内容。唯一的问题是,当我输入姓名和数字时,它只计算学生姓名和数字除以您输入的内容的大小。例如,假设我输入了3的大小,然后输入奥斯汀,60。它给我60/3,即20,但我不想要我的分数,我想要每个人的平均分数。我必须重写我的平均值函数以符合std::vector。有什么提示吗?它有点想在屏幕上格式化它,比如。名称:Austin 100,然后底部的单词average将所有分数结合起来,并按学生人数细分。 int a = test_scores[0].grade; string s = test_scores[0].name; student s; for (int i = 0; i < n; ++i) { std::cout << "Enter Student " << i+1 << " Name and Test Score: " << endl; std::cin >> s.name >> s.grade; test_scores[i] = s; // Don't use test_scores.push_back(s) here, if you do so, // you would increase its size by 1 and add the element s at the end of the vector. }

如何创建一个数组来匹配学生的名字和他们的分数? #包括 #包括 #包括 无效*交换(整数arr[],整数大小); 无效*交换(整数arr[],整数大小){ int i,j,温度; 对于(inti{0};i“运算符>”/Cord>错误,因为您不能比较C++中的结构。如果更改,如果您的代码将(如[ARR[i]>ARR[j]) > < >(ARR[i],等级> ARR[j]。。我现在重编了所有内容。唯一的问题是,当我输入姓名和数字时,它只计算学生姓名和数字除以您输入的内容的大小。例如,假设我输入了3的大小,然后输入奥斯汀,60。它给我60/3,即20,但我不想要我的分数,我想要每个人的平均分数。我必须重写我的平均值函数以符合std::vector。有什么提示吗?它有点想在屏幕上格式化它,比如。名称:Austin 100,然后底部的单词average将所有分数结合起来,并按学生人数细分。 int a = test_scores[0].grade; string s = test_scores[0].name; student s; for (int i = 0; i < n; ++i) { std::cout << "Enter Student " << i+1 << " Name and Test Score: " << endl; std::cin >> s.name >> s.grade; test_scores[i] = s; // Don't use test_scores.push_back(s) here, if you do so, // you would increase its size by 1 and add the element s at the end of the vector. },c++,C++,此问题的可能结构可以这样声明: #include <iostream> #include <array> #include <algorithm> void *swap(int arr[], int size); void *swap(int arr[], int size) { int i,j,temp; for(int i{0}; i < size; i++) { for(int j{i+1}; j <

此问题的可能结构可以这样声明:

#include <iostream>
#include <array>
#include <algorithm>

void *swap(int arr[], int size);
void *swap(int arr[], int size) {
    int i,j,temp;
    
    for(int i{0}; i < size; i++) {
        for(int j{i+1}; j < size; j++) {
            if(arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }   
        }
    }
    std::cout <<"The numbers in ascending order are: ";
    for(int i{0}; i < size; i++)
        std::cout << arr[i] << " ";
}

void average_score(int arr[], int size);
void average_score(int arr[], int size) {
    int average {0};
    
    for(int i{0}; i < size; i++) {
        average+=arr[i];
    }
    
    std::cout << "\nThe average is: " << average / size << std::endl;
}

int main() {
    int *test_scores = nullptr;
    int n;
    std::cout <<"Enter Size: ";
    std::cin >> n;
    
    test_scores = new int[n];
    
    int i = 0;
    int student_scores;
    
    while(i < n) {
        std::cout << "Enter Student " << i+1 << " Test Score: ";
        std::cin >> student_scores;
        
        test_scores[i] = student_scores;
        i++;
    }
    
    swap(test_scores,3);
    average_score(test_scores,3);
    
}
在主程序中,您可以更改向量的test_scores数组,这更有用,因为它的大小可以修改

struct student {
    int grade;
    string name;
};

出现此错误是因为您试图将
int
添加到
student
向量中。“无匹配函数调用”表示编译器不知道如何将整数转换为student。要解决此问题,我建议您询问名称,而不是只询问分数,因此您可以添加以下代码:

int a = test_scores[0].grade;
string s = test_scores[0].name;
student;
对于(int i=0;i
在average_score函数中,您应该更改
int arr[]
向量&arr的参数
average+=arr[i]
average+=arr[i].score
&arr
中的
非常重要,因为它将参数作为参考传递,因此其元素不会复制到函数中,这是非常不高效的


您的
swap
函数应该是
void
而不是
void*
。此外,尝试使用
算法库的排序函数。您实现的排序代码在向量长度上具有二次成本,如果有很多学生,这是非常无效的。

只需将这些数据保存在
cla中即可ss
struct
并用它们组成一个数组。使用指针而不是数组下标——这并不意味着你能想到的一切都是指针。你所要做的就是不用
array[i]
使用
*(array+i)
。仅此而已。您的
swap
函数不应尝试返回任何内容,但它被声明为返回
void*
。这不仅是错误的,您还存在未定义的行为,因为您不返回任何内容。建议使用
std::vector
而不是数组。例如:
std::vector
std::vector
可以在运行时更改大小,数组不能。你是对的,我提出了一个数组作为示例,因为问题的标题,但向量确实是一个更好的选择。现在已经更改了。@PereC嘿,PereC我尝试实现你的代码,但我仍然有点困惑。我做了std::vector测试(n)并创建了结构,但当我尝试通过执行test_scores.push_back(student_scores)将student_scores添加到test_grades.push_back(student_scores)时,会出现一个错误,提示“没有匹配的函数调用std::vector::push_back(&int)).我对这些东西还是新手,所以你能把代码写出来吗?我给了你一个解释作为另一个答案,我希望它能帮助你。是的,我的代码仍然有一些问题,我想我有点自掘坟墓了,有没有什么办法我可以把所有的东西都寄给你,这样你就可以帮我解决问题?比如电子邮件或通过StackOverflow.这个作业明天就要交了,我有点担心我可能完成不了-在我做了所有事情并改变了一些东西之后,现在它告诉我我的交换函数显示错误:“operator>”不匹配,以及交换函数中的其他错误。我甚至将参数更改为std::vector&arr,int size。这与我的inc有关修改和ARR[i]和ARR[j]。您得到<代码> >“运算符>”/Cord>错误,因为您不能比较C++中的结构。如果更改<代码>,如果您的代码将(如[ARR[i]>ARR[j])<代码> > < >(ARR[i],等级> ARR[j]。
。我现在重编了所有内容。唯一的问题是,当我输入姓名和数字时,它只计算学生姓名和数字除以您输入的内容的大小。例如,假设我输入了3的大小,然后输入奥斯汀,60。它给我60/3,即20,但我不想要我的分数,我想要每个人的平均分数。我必须重写我的平均值函数以符合std::vector。有什么提示吗?它有点想在屏幕上格式化它,比如。名称:Austin 100,然后底部的单词average将所有分数结合起来,并按学生人数细分。
int a = test_scores[0].grade;
string s = test_scores[0].name;
student s;
for (int i = 0; i < n; ++i) {
    std::cout << "Enter Student " << i+1 << " Name and Test Score: " << endl;
    std::cin >> s.name >> s.grade;
    
    test_scores[i] = s; // Don't use test_scores.push_back(s) here, if you do so,
    // you would increase its size by 1 and add the element s at the end of the vector.
}