C++ column chart is reversed -
i'm stumped on why columns reversed. actual column graph (asterisks only) needs flipped horizontally. (the population in 1900 2000 , population in 2040 24000.)
here looks like:
population column chart 24000 ** 23000 ** 22000 ** ** 21000 ** ** 20000 ** ** 19000 ** ** 18000 ** ** ** 17000 ** ** ** 16000 ** ** ** 15000 ** ** ** 14000 ** ** ** ** 13000 ** ** ** ** 12000 ** ** ** ** 11000 ** ** ** ** 10000 ** ** ** ** 9000 ** ** ** ** ** 8000 ** ** ** ** ** 7000 ** ** ** ** ** 6000 ** ** ** ** ** 5000 ** ** ** ** ** ** 4000 ** ** ** ** ** ** ** 3000 ** ** ** ** ** ** ** 2000 ** ** ** ** ** ** ** ** 1000 ** ** ** ** ** ** ** ** 1900 1920 1940 1960 1980 2000 2020 2040
and here's code:
#include <fstream> #include <iostream> #include <iomanip> using namespace std; int main(){ int population_size = 8; //available changes. int year = 1900; int population[population_size]; bool havefile = true; const string infilename = "people.txt"; ifstream infile("people.txt"); //defines input file , opens cout << "population column chart" << endl; cout << endl; if (infile){ // cout << "open of " << infilename << " successful.\n"; int count = 0; while (!infile.eof()) { infile >> population[count]; count++; if (infile.eof()) break;} int min = 1000; //define minimum y-axis amount int max = 0; //initialize max value 0 (int i=0; i<count; i++){ //find maximum population if(population[i]> max); max = population[i]; } //this block creates y-axis legend , rows of stars (int num = max; num >= min; num = num - 1000){ cout << setw(5) << num << " "; (int year_counter = 0; year_counter <= count-1; year_counter++) if (num <= population[year_counter]) cout << setw(3) << "**" << " "; cout << endl; } //this block creates x-axis legend cout << " "; (int k = 1; k <= count; k++){ cout << setw(6) << year; year += 20; } } }
the problem in area of code:
for (int year_counter = 0; year_counter <= count-1; year_counter++) if (num <= population[year_counter]) cout << setw(3) << "**" << " ";
population[0] should value of 2000, 24000. there problem array being somehow reversed?
thanks in advance
if(population[i]> max); max = population[i];
should be
if(population[i]> max) max = population[i];
try , see.
ok, think see what's now. trying print populations various years. each column 6 characters wide ("\*\*"
set in setw(3)
, plus " "
). trouble is, print column if it's going "**"
; otherwise print nothing. columns "\*\*"
jammed left.
solution print column, each row, whether gets "**"
or not:
(int year_counter = 0; year_counter <= count - 1; year_counter++) if (num <= population[year_counter]) cout << setw(3) << "**" << " "; else //new cout << setw(3) << " " << " "; //new
Comments
Post a Comment