histogram program gives strange output C++ -
i have been writing code produce horizontal histogram. program takes user input of range of numbers vector. asks user lowest value want histogram begin at, , how big want each bin be. example:
if lowestvalue = 1
, binsize = 20
, vector filled values {1, 2, 3, 20, 30, 40, 50}
print like:
(bin) (bars) (num)(percent) [ 1-21) #### 4 57% [21-41) ## 2 28% [41-61) ## 2 28%
here of code so:
void printhistogram(int lowestvalue, int binsize, vector<double> v) { int binfloor = lowestvalue, binceiling = 0; int numbins = amountofbins(binsize, (int)range(v)); (int = 0; i<=numbins; i++) { binceiling = binfloor+binsize; int amoinbin = amountinbin(v,binfloor, binsize); double perinbin = percentinbin(v, amoinbin); if (binfloor < 10) { cout << "[ " << binfloor << '-' << binceiling << ") " << setw(20) << left << formatbars(perinbin) << ' ' << amoinbin << ' '<< setprecision(4) << perinbin << '%' << endl; binfloor += binsize; } else { cout << '[' << binfloor << '-' << binceiling << ") " << setw(20) << left << formatbars(perinbin) << ' ' << amoinbin << ' '<< setprecision(4) << perinbin << '%' << endl; binfloor += binsize; } } }
and function counts how many terms in each bin:
int amountinbin(vector<double> v, int lowestbinvalue, int binsize) { int count = 0; (size_t i; i<v.size(); i++) { if (v[i] >= lowestbinvalue && v[i] < (lowestbinvalue+binsize)) count += 1; } return count; }
now issue:
for reason, not counting values between 20-40. @ least far can see testing. here image of run:
any appreciated.
the code in loop doesn't initialize i
, results @ best unpredictable.
Comments
Post a Comment