teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

誤字の修正

2021/01/24 11:57

投稿

tamago_yaki
tamago_yaki

スコア2

title CHANGED
File without changes
body CHANGED
@@ -66,9 +66,9 @@
66
66
  #include "random_vector.h"
67
67
  #include <cstdlib>
68
68
  #include <vector>
69
- // TODO: add any include you might require
70
69
 
71
70
 
71
+
72
72
  RandomVector::RandomVector(int size, double max_val) {
73
73
 
74
74
  int double_size = size;
@@ -124,10 +124,80 @@
124
124
 
125
125
  void RandomVector::printHistogram(int bins){
126
126
 
127
- double separation = 1 / bins;
127
+ //double separation = 1 / bins;
128
+
129
+ double max_num_hist=0;
130
+ for (auto &elem: vec){
128
- std::cout << min_num << " ";
131
+ if ( max_num_hist < elem){
132
+ max_num_hist = elem;
133
+ }
134
+ }
135
+
129
136
 
137
+ double min_num_hist=1;
138
+ for (auto &elem: vec){
139
+ if ( min_num_hist > elem){
140
+ min_num_hist = elem;
141
+ }
142
+ }
143
+
144
+ //std::cout << "max" << max_num_hist << " ";
145
+ //std::cout << "min" << min_num_hist << " ";
130
146
 
147
+ double separation = (max_num_hist - min_num_hist) / bins;
148
+ //std::cout << "min" << separation << " ";
149
+
150
+ std::vector<int> vect_his;
151
+ for (auto i = 1; i <= bins; ++i){
152
+ vect_his.push_back(0);
153
+ }
154
+
155
+ for (auto &elem: vec){
156
+
157
+ for (auto i = 0; i < bins; ++i){
158
+ if (i==0){
159
+ if ( min_num_hist+separation*(i+1) >= elem){
160
+ vect_his.at(i) += 1;
161
+ }
162
+ }
163
+
164
+ else if ( min_num_hist+separation*(i+1) >= elem && min_num_hist+separation*(i) < elem )
165
+ {
166
+ vect_his.at(i) += 1;
167
+ }
168
+ }
169
+
170
+ }
171
+
172
+ double max_num_freq=0;
173
+ for (auto &elem: vect_his){
174
+ if ( max_num_freq < elem){
175
+ max_num_freq = elem;
176
+ }
177
+ }
178
+ std::cout << std::endl;
179
+ for (auto i = max_num_freq; i > 0; --i){
180
+ for (auto j = 0; j < bins; ++j){
181
+ if (i <= vect_his.at(j) ){
182
+ std::cout << "*** ";
183
+ }
184
+ else{
185
+ std::cout << " ";
186
+ }
187
+ }
188
+ std::cout << std::endl;
189
+ }
190
+
191
+
192
+ std::cout << "elements: ";
193
+ for (auto &elem: vect_his){
194
+ std::cout << elem << " ";
195
+ //std::cout << vect_his.at(2) << " ";
196
+ }
197
+ std::cout << std::endl;
198
+
199
+
200
+
131
201
  }
132
202
  ```
133
203
 
@@ -153,10 +223,79 @@
153
223
 
154
224
  void RandomVector::printHistogram(int bins){
155
225
 
156
- double separation = 1 / bins;
226
+ //double separation = 1 / bins;
227
+
228
+ double max_num_hist=0;
229
+ for (auto &elem: vec){
157
- std::cout << min_num<< " ";
230
+ if ( max_num_hist < elem){
231
+ max_num_hist = elem;
232
+ }
233
+ }
234
+
158
235
 
236
+ double min_num_hist=1;
237
+ for (auto &elem: vec){
238
+ if ( min_num_hist > elem){
239
+ min_num_hist = elem;
240
+ }
241
+ }
242
+
243
+ //std::cout << "max" << max_num_hist << " ";
244
+ //std::cout << "min" << min_num_hist << " ";
159
245
 
246
+ double separation = (max_num_hist - min_num_hist) / bins;
247
+ //std::cout << "min" << separation << " ";
248
+
249
+ std::vector<int> vect_his;
250
+ for (auto i = 1; i <= bins; ++i){
251
+ vect_his.push_back(0);
252
+ }
253
+
254
+ for (auto &elem: vec){
255
+
256
+ for (auto i = 0; i < bins; ++i){
257
+ if (i==0){
258
+ if ( min_num_hist+separation*(i+1) >= elem){
259
+ vect_his.at(i) += 1;
260
+ }
261
+ }
262
+
263
+ else if ( min_num_hist+separation*(i+1) >= elem && min_num_hist+separation*(i) < elem )
264
+ {
265
+ vect_his.at(i) += 1;
266
+ }
267
+ }
268
+
269
+ }
270
+
271
+ double max_num_freq=0;
272
+ for (auto &elem: vect_his){
273
+ if ( max_num_freq < elem){
274
+ max_num_freq = elem;
275
+ }
276
+ }
277
+ std::cout << std::endl;
278
+ for (auto i = max_num_freq; i > 0; --i){
279
+ for (auto j = 0; j < bins; ++j){
280
+ if (i <= vect_his.at(j) ){
281
+ std::cout << "*** ";
282
+ }
283
+ else{
284
+ std::cout << " ";
285
+ }
286
+ }
287
+ std::cout << std::endl;
288
+ }
289
+
290
+
291
+ std::cout << "elements: ";
292
+ for (auto &elem: vect_his){
293
+ std::cout << elem << " ";
294
+ //std::cout << vect_his.at(2) << " ";
295
+ }
296
+ std::cout << std::endl;
297
+
298
+
160
299
  }
161
300
  ```
162
301
  の中で、他の関数の値、min_num, max_num (最小値と最大値) の値を使いたいとき、どのように宣言すればよいでしょうか。。

1

誤字の修正

2021/01/24 11:57

投稿

tamago_yaki
tamago_yaki

スコア2

title CHANGED
File without changes
body CHANGED
@@ -159,8 +159,9 @@
159
159
 
160
160
  }
161
161
  ```
162
+ の中で、他の関数の値、min_num, max_num (最小値と最大値) の値を使いたいとき、どのように宣言すればよいでしょうか。。
162
- の中で、他の関数の値、min_num, max_num の値を使いたいとき、どのように宣言すればよいでしょうか。私の結果のアウトプットは、printHistgram の中で、もう一度最小値と最大値を計算しているため、大変非効率だと感じております。
163
+ 私の結果のアウトプットは、printHistgram の中で、もう一度最小値と最大値を計算しているため、大変非効率だと感じております。
163
- また、引数は変えられないという条件があり、printHistgram内で、min,max を呼び出したいです。
164
+ また、引数は変えられないという条件があり、printHistgram内で、min(),max() を呼び出したいです。
164
165
 
165
166
  何か、計算が繰り返しにならない、いい方法はないでしょうか。。
166
167
  よろしくお願いいたします。