打ち消し線### 前提・実現したいこと
ここに質問の内容を詳しく書いてください。
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
正しい平均の出力ができない (2.102e 等になってしまう)
該当のソースコード
C++
1#include<iostream> 2#include<string> 3#include<iomanip> 4using namespace std; 5 6class Arrays { 7 friend ostream &operator<<(ostream&, const Arrays&); 8 friend istream &operator>>(istream&, Arrays&); 9public: 10 Arrays(); 11 Arrays operator+(const Arrays& rhs) const; 12 Arrays operator-(const Arrays& rhs) const; 13 Arrays &operator--(); 14 Arrays operator--(int); 15 bool operator>(const Arrays& rhs)const; 16 bool operator<=(const Arrays& rhs)const; 17private: 18 int num[10]; 19 float avg; 20}; 21 22Arrays &Arrays::operator--() 23{ 24 for 25 (int i = 0; i < 5; i++) 26 { 27 int t = num[9 - i]; 28 num[9 - i] = num[i]; 29 num[i] = t; 30 } 31 return *this; 32} 33 34Arrays Arrays::operator--(int) 35{ 36 Arrays temp = *this; 37 for 38 (int i = 0; i < 5; i++) 39 { 40 int t = num[9 - i]; 41 num[9 - i] = num[i]; 42 num[i] = t; 43 } 44 return temp; 45} 46 47Arrays Arrays::operator+(const Arrays& rhs) const 48{ 49 Arrays sum; 50 for (int i = 0; i<10; i++) 51 { 52 sum.num[i] = num[i] + rhs.num[i]; 53 } 54 return sum; 55} 56 57 58 59Arrays Arrays::operator-(const Arrays& rhs) const 60{ 61 Arrays dif; 62 for (int i = 0; i<10; i++) 63 { 64 dif.num[i] = num[i] - rhs.num[i]; 65 } 66 return dif; 67} 68 69Arrays::Arrays() 70{ 71 int large1 = num[0], large2 = num[0], sum = 0; 72 for (int i = 0; i < 10; i++) 73 { 74 if (num[i] > large1) 75 { 76 large1 = num[i]; 77 } 78 } 79 for (int i = 0; i < 10; i++) 80 { 81 if (num[i] < large1 && num[i] > large2) 82 { 83 large2 = num[i]; 84 } 85 sum += num[i]; 86 } 87 avg = (sum - large1 - large2) / (float)8; 88} 89 90 91 92ostream& operator<<(ostream& output, const Arrays& one) 93{ 94 for (int i = 0; i < 10; i++) 95 { 96 output << one.num[i] << ' '; 97 } 98 output << one.avg; 99 return output; 100} 101 102 103istream& operator>>(istream& input, Arrays& one) 104{ 105 for (int i = 0; i < 10; i++) 106 { 107 input >> one.num[i]; 108 } 109 one.avg; 110 return input; 111} 112 113bool Arrays::operator>(const Arrays& rhs)const 114{ 115 return avg > rhs.avg; 116} 117bool Arrays::operator<=(const Arrays& rhs)const 118{ 119 return avg <= rhs.avg; 120} 121 122int main() 123{ 124 Arrays first, second, sum, dif; 125 cout << "Pick 10 numbers for first the Array "; 126 cin >> first; 127 cout << "Pick 10 numbers for second the Array "; 128 cin >> second; 129 sum = first + second; 130 dif = first - second; 131 cout << endl; 132 cout << "The first Array is " << first << endl; 133 cout << "The second Array is " << second << endl; 134 cout << "The sum of arrays is " << sum << endl; 135 cout << "The difference of arrays is " << dif << endl; 136 cout <<"The reverse first Array: " << --first << endl; 137 cout << "The reverse second Array: "<< --second << endl; 138 cout << "The reverse sum of Array" << --sum << endl; 139 cout << "The reverse difference of Array" << --dif << endl; 140 if (first > second) 141 cout << "the average of the first array with the two largest values excluded from the average calculation is greater than the average of the second array with the two largest values excluded from the average calculation" << endl; 142 else 143 cout << " the average of the first array with the two largest values excluded from the average calculation is less than or equal to the average of the second array with the two largest values excluded from the average calculation" << endl; 144 145 146 return 0; 147} 148
試したこと
publicでlarge1,large2, sumを宣言
instream で平均計算
補足情報(FW/ツールのバージョンなど)
Visual Studio 2019
求められているのは一番目と二番目に大きい数値を抜いた数列の平均です
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。