c++で構造体の二次元配列に値を入れ昇順に並び替えたら値が0になる
ポインタを使って構造体にアクセスして初期値をいれたあと、その合計値の高い順に並び変えたのですが何故か2次元配列の中の値が最初の数字以外全部0になります。
初期値を入れた後に再度値が入っているかを確認した際に値が入っていたので並び替えのときに二次元配列がおかしくなっているような気がします。
それか、ただ単に値は入っているけど表示の仕方が誤っているかです。
わからないので教えてください。
C++
1コード 2header 3 4#pragma once 5#include <iostream> 6using namespace std; 7//メイン関数 8void HowToStruct_EX02(); 9 10//初期値を入れる関数 11void SetInit(struct empl* pe, int array[5][6]); 12//値を表示する関数 13void ShowStruct(struct empl* p, int x, int y); 14//並び替え関数 15void HowToStruct_Sort2(struct empl* p, int n);
C++
1コード 2Cpp 3 4//残業時間 5struct zangyo { 6 int hours[5][6]; 7 float ave; 8}; 9 10//残業社員を求める------ 11struct empl { 12 string name; 13 string section; 14 zangyo times; 15}; 16 17 18void HowToStruct_EX02() { 19 20 //月の残業時間 21 int hours[5][6] = { 22 {23,89,50,10,30,75}, 23 {10,50,90,62,20,83}, 24 {40,55,36,44,80,15}, 25 {90,60,86,78,26,49}, 26 {58,81,76,11,66,99} 27 }; 28 29 float average[5] = { 0 }; 30 31 //残業 32 struct zangyo times[5] = { 33 {hours[0][0],average[0]}, 34 {hours[1][0],average[1]}, 35 {hours[2][0],average[2]}, 36 {hours[3][0],average[3]}, 37 {hours[4][0],average[4]} 38 }; 39 40 //社員 41 struct empl zangyo[5] = { 42 //名前//部署//月の残業 43 {"阿部","人事",times[0]}, 44 {"加藤","経理",times[1]}, 45 {"須藤","総務",times[2]}, 46 {"西川","技術",times[3]}, 47 {"田島","営業",times[4]}, 48 }; 49 50 struct empl* pe; 51 pe = zangyo; 52 53 cout << "氏名|" << "部門|" << "1月|" << "2月|" << "3月|" 54 << "4月|" << "5月|" << "6月" << endl; 55 56 SetInit(pe, hours); 57 ShowStruct(pe, 6, 5); 58 59 cout << "残業が多い順に並べると" << endl; 60 61 HowToStruct_Sort2(pe, 5); 62 63 ShowStruct(pe, 6, 5); 64} 65 66//構造体の初期化 67void SetInit(struct empl* pe,int array[5][6]) { 68 for (int i = 0; i < 5; i++) 69 { 70 int sum = 0; 71 for (int j = 0; j < 6; j++) 72 { 73 pe->times.hours[i][j] = array[i][j]; 74 sum += pe->times.hours[i][j]; 75 } 76 pe->times.ave = sum; 77 pe++; 78 } 79} 80 81//現在の構造体の中身を表示 82void ShowStruct(struct empl* pe,int x,int y) { 83 84 for (int i = 0; i < y; i++) 85 { 86 //残業の合計を保存するための変数 87 cout << pe->name << "|"; 88 cout << pe->section << "|"; 89 for (int j = 0; j < x; j++) 90 { 91 cout << pe->times.hours[i][j] << " |"; 92 } 93 cout << pe->times.ave; 94 95 cout << endl; 96 pe++; 97 } 98} 99 100//残業が多い順に組み替える -----------------------------★★★★★ここがおかしい気がします★★★★★ 101void HowToStruct_Sort2(struct empl* pe, int n) { 102 103 struct empl tmp; 104 105 for (int i = 0; i < 5; i++) 106 { 107 for (int j = 0; j < 5; j++) 108 { 109 if ((pe + i)->times.ave >(pe + j)->times.ave) { 110 tmp = *(pe + i); 111 *(pe + i) = *(pe + j); 112 *(pe + j) = tmp; 113 } 114 } 115 } 116}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/19 14:42
2020/11/19 14:43
2020/11/19 15:02 編集
2020/11/19 15:11 編集
2020/11/19 15:14