Home>

If i put values ​​in a two-dimensional array of structures in c ++ and sort them in ascending order, the values ​​will be 0.

After accessing the structure with a pointer and entering the initial values, I sorted them in descending order of the total value, but for some reason all the values ​​in the two-dimensional array are 0 except the first number.

When I checked if the value was entered again after entering the initial value, the value was entered, so I feel that the two-dimensional array is strange when sorting.
Or is it just a value but it is displayed incorrectly?
I don't know so please let me know.

code
header
#pragma once
#include<iostream>
using namespace std;
// Main function
void HowToStruct_EX02 ();
// Function to put the initial value
void SetInit (struct empl * pe, int array [5] [6]);
// Function to display the value
void ShowStruct (struct empl * p, int x, int y);
// Sort function
void HowToStruct_Sort2 (struct empl * p, int n);
code
Cpp
// Overtime hours
struct zangyo {
    int hours [5] [6];
    float ave;
};
// Seeking overtime employees ------
struct empl {
    string name;
    string section;
    zangyo times;
};

void HowToStruct_EX02 () {
       // Monthly overtime hours
    int hours [5] [6] = {
    {23,89,50,10,30,75},
    {10,50,90,62,20,83},
    {40,55,36,44,80,15},
    {90,60,86,78,26,49},
    {58,81,76,11,66,99}
    };
    float average [5] = {0};
    //overtime
    struct zangyo times [5] = {
        {hours [0] [0], average [0]},
        {hours [1] [0], average [1]},
        {hours [2] [0], average [2]},
        {hours [3] [0], average [3]},
        {hours [4] [0], average [4]}
    };
    // Employee
    struct empl zangyo [5] = {
        // Name // Department // Monthly overtime
        {"Abe", "Human Resources", times [0]},
        {"Kato", "Accounting", times [1]},
        {"Sudo", "General Affairs", times [2]},
        {"Nishikawa", "Technology", times [3]},
        {"Tajima", "Sales", times [4]},
    };
    struct empl * pe;
    pe = zangyo;
    cout<<"Name |"<<"Department |"<<"January |"<<"February |"<<"March |"
      <<"April |"<<"May |"<<"June"<<endl;
    SetInit (pe, hours);
    ShowStruct (pe, 6, 5);
    cout<<"Arranged in descending order of overtime"<<endl;
    HowToStruct_Sort2 (pe, 5);
    ShowStruct (pe, 6, 5);
}
// Structure initialization
void SetInit (struct empl * pe, int array [5] [6]) {
    for (int i = 0;i<5;i ++)
    {
        int sum = 0;
        for (int j = 0;j<6;j ++)
        {
            pe->times.hours [i] [j] = array [i] [j];
            sum + = pe->times.hours [i] [j];
        }
        pe->times.ave = sum;
        pe ++;
    }
}
// Show the contents of the current structure
void ShowStruct (struct empl * pe, int x, int y) {
    for (int i = 0;i<y;i ++)
    {
        // Variable for storing total overtime
        cout<<pe->name<<"|";
        cout<<pe->section<<"|";
        for (int j = 0;j<x;j ++)
        {
            cout<<pe->times.hours [i] [j]<<"|";
        }
        cout<<pe->times.ave;
        cout<<endl;
        pe ++;
    }
}
// Rearrange in descending order of overtime -----------------------------  I feel that this is strange  
void HowToStruct_Sort2 (struct empl * pe, int n) {
    struct empl tmp;
    for (int i = 0;i<5;i ++)
    {
        for (int j = 0;j<5;j ++)
        {
            if ((pe + i)->times.ave>(pe + j)->times.ave) {
                tmp = * (pe + i);
                * (pe + i) = * (pe + j);
                * (pe + j) = tmp;
            }
        }
    }
}
  • Answer # 1

    At initializationzangyo [i]Overtime hours for (i = 0-4)zangyo [i] .times.hours [i] [j]It is stored in (j = 0 to 5).
    Since the sort order changes after sortingzangyo [i] .times.hours [i]Does not contain the expected value.


    Each individual only has to keep his or her overtime hours, so there is no need to make it two-dimensional.

    // Overtime hours
    struct zangyo {
    --int hours [5] [6];
    + int hours [6];
        float ave;
    };
    // Structure initialization
    void SetInit (struct empl * pe, int array [5] [6]) {
        for (int i = 0;i<5;i ++)
        {
            int sum = 0;
            for (int j = 0;j<6;j ++)
            {
    --pe->times.hours [i] [j] = array [i] [j];
    + pe->times.hours [j] = array [i] [j];
    --sum + = pe->times.hours [i] [j];
    + sum + = pe->times.hours [j];
            }
            pe->times.ave = sum;
            pe ++;
        }
    }
    // Show the contents of the current structure
    void ShowStruct (struct empl * pe, int x, int y) {
        for (int i = 0;i<y;i ++)
        {
            // Variable for storing total overtime
            cout<<pe->name<<"|";
            cout<<pe->section<<"|";
            for (int j = 0;j<x;j ++)
            {
    --cout<<pe->times.hours [i] [j]<<"|";
    + cout<<pe->times.hours [j]<<"|";
            }
            cout<<pe->times.ave;
            cout<<endl;
            pe ++;
        }
    }

    This area is also unnecessary.

    --float average [5] = {0};
        //overtime
    --struct zangyo times [5] = {
    -{hours [0] [0], average [0]},
    -{hours [1] [0], average [1]},
    -{hours [2] [0], average [2]},
    -{hours [3] [0], average [3]},
    -{hours [4] [0], average [4]}
    -};
        // Employee
        struct empl zangyo [5] = {
            // Name // Department // Monthly overtime
    -{"Abe", "Human Resources", times [0]},
    + {"Abe", "Human Resources",},
    -{"Kato", "Accounting", times [1]},
    + {"Kato", "Accounting",},
    -{"Sudo", "General Affairs", times [2]},
    + {"Sudo", "General Affairs",},
    -{"Nishikawa", "Technology", times [3]},
    + {"Nishikawa", "Technology",},
    -{"Tajima", "Sales", times [4]},
    + {"Tajima", "Sales",},
        };