Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何同时对2个数组进行排序?_Arrays_Sorting_Struct - Fatal编程技术网

Arrays 如何同时对2个数组进行排序?

Arrays 如何同时对2个数组进行排序?,arrays,sorting,struct,Arrays,Sorting,Struct,我有一个作业,其中我应该创建一个结构,其中有两个成员,一个ID和一个分数。我要为200名学生生成一个唯一的ID,每个学生的分数在1-100之间。在那之后,我将打印出它们的前排序、后排序(按ID排序)和后排序(按分数排序)版本 我的问题是如何将ID从最小到最大排序,但同时保留分配给它的分数?如果我对ID排序,分数保持不变。我需要给定的分数来匹配ID,即使在排序之后也是如此 struct students { int studentID; int studentScore; };

我有一个作业,其中我应该创建一个结构,其中有两个成员,一个ID和一个分数。我要为200名学生生成一个唯一的ID,每个学生的分数在1-100之间。在那之后,我将打印出它们的前排序、后排序(按ID排序)和后排序(按分数排序)版本

我的问题是如何将ID从最小到最大排序,但同时保留分配给它的分数?如果我对ID排序,分数保持不变。我需要给定的分数来匹配ID,即使在排序之后也是如此

struct students
{
    int studentID;
    int studentScore;
};

int main()
{

    // Local Variables
       students studentInfo[200];
       int counter = 0;

    for(int i=0; i<200;i++)
    {
    HERE:
        studentInfo[i].studentID = rand() % 500;  

        for(int k=0; k < counter; k++)
        {
            if(studentInfo[200].studentID == studentInfo[k].studentID)
            {
                goto HERE;
            }
        }

        counter++;
    }

    for(int i=0; i<200;i++)
    {
        studentInfo[i].studentScore = rand() % 100;       
    }

您有一个学生数组,称它为student会更好,因为它只包含一个学生的信息,并且您正在按照他们的studentID属性对他们进行排序。studentScore是students的另一个属性,因此可以说它保持不变,也就是说,当您按ID对其排序时,它不会影响分数,反之亦然。我认为您只需要std::swap函数?此外,此代码错误:studentInfo[200]。studentID索引200超出范围。看起来您只是在尝试生成随机ID,为什么不将它们的ID设置为索引?也不要对两个数组进行排序。对一个结构数组进行排序。