Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, here\'s another code lab question I have. If you can make the program sim

ID: 3666495 • Letter: H

Question

Hello, here's another code lab question I have. If you can make the program simple, I would appreciate it, thanks!

5.18: Population Bar Chart
Write a program that produces a bar chart showing the population growth of Prairieville, a small town in the Midwest, at 20-year intervals during the past 100 years. The program should read in the population figures for 1900, 1920, 1940, 1960, 1980, and 2000 from a file named People.txt. It is understood that the first value represents the population in 1900 (for example, 2,843), the second the population in 1920 and so on. For each year it should display the year, followed by at least one space followed by a bar consisting of one asterisk for each 1,000 people. (Theprogram should round UP so that a population of 1 would generate one asterisk -- as would 1000, while a population of 1,001 would generate two, etc.)

Prompts And Output Labels. The program is not interactive so there are no prompts. The first line of the output should be
PRAIRIEVILLE POPULATION GROWTH
and the second line of the output should be
(each * represents 1000 people)
after which the main output follows.

Input Validation. The program should validate each population count read. If the population is ever negative, the program should print themessage "BAD DATA" on a line by itself and not process any more data.

Instructor Notes:

Here are all of the necessary output strings to assist you with this assignment.

Explanation / Answer

Code:

import math
print 'PRAIRIEVILLE POPULATION GROWTH'
print '(each * represents 1000 people) '
year = 1900
with open('People.txt','r') as f:
for line in f:
for pop in line.split():
pop=pop.replace(',','')# to remove commas in number ex 2,345 to 2345
if int(pop)<0:
print str(year)+' population can't be positive , population read = '+str(pop)
else:
res = '*'*int(math.ceil(float(pop)/1000)) #creating * string length equla to population /1000 rounded to next intiger with math.ceil function
print str(year)+ " " + res # print the output
year = year+20

People.txt (input File) :

2,345 34,567 1,000 -987 8,990 9,087

output:

PRAIRIEVILLE POPULATION GROWTH

(each * represents 1000 people)

  

1900  ***

1920  ***********************************   

1940  *

1960  population can't be positive , population read = -987

1980  *********

2000  **********

C++ Code:

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

int main()
{
string pop="";
int temp;
int res;
int i,year=1900;
ifstream in("People.txt");
cout<<"PRAIRIEVILLE POPULATION GROWTH"<<endl;
cout<<"(each * represents 1000 people) ";
while(in>>pop)// read values from file
{
pop.erase(std::remove(pop.begin(), pop.end(), ','), pop.end());// remove commas in the numbers
temp = stoi(pop); // convert string to number
if(temp<0)
cout<<year<<" population can't be negetive"<<endl;
else
{
res = ceil((float)temp/1000);// find the number of * to print
cout<<year<<" ";
for(i=0;i<res;i++)
cout<<"*";//print * of length = res
cout<<endl;
}
year+=20;
}
}

People.txt (input File) :

2,345 34,567 1,000 -987 8,990 9,087

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote