C++ : display a bar chart of array data using * -
i making program in c++11 represents yearly rainfall statistics. have completed program stuck in last stage. need design/display bar chart using *, display data array. example: uk monthly rainfall amount (mm) in 2015 are: 154.3, 79.2, 95.6, 46.3, 109.6, 55.1, 109.5, 107.4, 54.0, 72.2, 176.0, 230.0 each month consecutively.
i want bar chart this:
0 - 50 : * 50 - 80 : **** 80 - 110 : **** 110 - 140 : 140 - more : ***
this code wrote gives me incorrect output
void rainfall::outputbarchart() const { cout<< "bar chart rainfall amount:"<< endl; const size_t frequencysize= 5; array<unsigned int, frequencysize > frequency = { }; for(int mark : amount) ++frequency[mark / 50]; for(size_t count = 0; count < frequencysize; ++count) { if (count == 0) cout << " 0-49: "; else cout<< count * 50 << "-"<< (count * 50) + 30 << ": "; (unsigned int stars = 0; stars < frequency[count]; ++stars) cout<< '*'; cout << endl; } }
it gives me following bar chart in output
0 - 49 : * 50 - 80 : ***** 100 - 130 : *** 150 - 180 : ** 200 - 230 : **
try matrix hold "chart". this:
char chart[][] = char[12][4]; // fill "chart" blanks! (auto idx=0; idx<12; ++idx) { if (rain[idx] <= 50) { chart[idx][3] = '*'; } else if (rain[idx] > 50 && rain[idx] <= 80) { chart[idx][3] = '*'; chart[idx][2] = '*'; chart[idx][1] = '*'; chart[idx][0] = '*'; } //etc cover cases } // print matrix (auto idxcol=0; idxcol<4; ++idxcol) { (auto idxln=0; idxln<12; ++idxln) std::cout << chart[idxln][idxcol]; std::cout << std::endl; }
it can optimized i'm sure can that.
Comments
Post a Comment