Hey guys,
I need to write a C++ program for school and I would like some help. We have not gone over pointers or classes, not sure if they would be needed, but I would like some help writing the program below:
C++ Population Bar:
Write a program that asks the user to enter the population of 4 cities and produce the bar graph
Here is an example of the program’s output. User input is shown as bold.
Please enter number of cities: 4
Enter the population of city 1 : 10000
Enter the population of city 2: 15000
Enter the population of city 3: 9000
Enter the population of city 4: 18000
POPULATION
(each * = 1000 people)
City 1 : **********
City 2 : ***************
City 3 : *********
City 4 : ******************
Input validation: Do not accept population less than 0.
Here is an example of program’s output if user input is less than zero. (Error message is in Red. (Assume that user enter 3 for number of cities in this case)
Enter the population of city 1 : -20
Enter the population of city 2 : 3000
Enter the population of city 3 : 4000
Enter the population of city 4 : 8000
Population can’t be negative. Please re-enter
Enter the population of city 1 : 2000
Enter the population of city 2 : 3000
Enter the population of city 3 : 4000
Enter the population of city 4 : 8000
POPULATION
(each * = 1000 people)
City 1 : **
City 2 : ***
City 3 : ****
City 4: ********
Hint: For this program, you need to use selection and repetition commands. Consider using a do-while loop. Also, there are many ways of writing this program. I will accept any valid implementation that users proper C++ constructs
(b) [5 Points Bonus] Modify your program above that asks the user to enter number of cities he/she would like to enter population. Then ask the user to enter the population for each city and produce the bar graph
Here is an example of program’s output. User input is shown bold.
Please enter number of cities: 4
Enter the population of city 1 : 10000
Enter the population of city 2: 15000
Enter the population of city 3: 9000
Enter the population of city 4: 18000
POPULATION (each * = 1000 people)
City 1 : **********
City 2 : ***************
City 3 : *********
City 4 : ******************
Input validation: Do not accept population less than 0.
Here is an example of program’s output if user input is less than zero. (Error message is in Red. (Assume that user enter 3 for number of cities in this case)
Enter the population of city 1 : -20
Enter the population of city 2 : 3000
Enter the population of city 3 : 4000
Population can’t be negative. Please re-enter
Enter the population of city 1 : 2000
Enter the population of city 2 : 3000
Enter the population of city 3 : 4000
POPULATION
(each * = 1000 people)
City 1 : **
City 2 : ***
City 3 : ****
Hint: For the bonus part, you will need to use dynamic memory allocation and arrays.
//part A program
#include <iostream>
using namespace std;
int main()
{
int population[4],k;
int i=0,n;
cout<<"enter number of
cities:"<<endl;
cin>>n;
do
{
cout<<"Enter the
population of city "<<i<<":"<<endl;
cin>>population[i];
if(population[i]<0)
cout<<"population cannot
be negative,Reenter\n";
else
i++;
} while (i<n);
for(int i=0;i<n;i++)
{
cout<<"\nCity
"<<i+1<<":";
k=population[i]/1000;
for(int j=0;j<k;j++)
cout<<"*";
}
return 0;
}
output obtained:
STDIN:
4
10000
-15000
1200
2000
3000
$g++ -o main *.cpp $main enter number of cities: Enter the population of city 0: Enter the population of city 1: population cannot be negative,Reenter Enter the population of city 1: Enter the population of city 2: Enter the population of city 3: City 1:********** City 2:* City 3:** City 4:***
---------------------------------------------------------------------------------------------------
//part b program
#include <iostream>
using namespace std;
int main()
{
int* population;
int k,n,i=0;
cout<<"Enter number of
cities:"<<endl;
cin>>n;
// population is dynamic array of size n;
population=new int[n];
// population array can be used as normal array
do
{
cout<<"Enter the
population of city "<<i+1<<":"<<endl;
cin>>population[i];
if(population[i]<0)
cout<<"population cannot
be negative,Reenter\n";
else
i++;
}
while (i<n);
for(int i=0;i<n;i++)
{
cout<<"\nCity
"<<i+1<<":";
k=population[i]/1000;
for(int j=0;j<k;j++)
cout<<"*";
}
return 0;
}
output:
stdin
5
10000
1200
-2000
3000
30000
7890
$g++ -o main *.cpp $main Enter number of cities: Enter the population of city 1: Enter the population of city 2: Enter the population of city 3: population cannot be negative,Reenter Enter the population of city 3: Enter the population of city 4: Enter the population of city 5: City 1:********** City 2:* City 3:*** City 4:****************************** City 5:*******
Hey guys, I need to write a C++ program for school and I would like some...