Question

Modify your program above that asks the user to enter number of cities he/she would like...

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 : ****

Here is what I have below: ( I don't know how to display output as red text and I need to enter Population can’t be negative. Please re-enter (in red))

#include <iostream>

using namespace std;

int gcd(int a, int b)

{

if (a == 0)

return b;

return gcd(b % a, a);

}

int ngcd(int nums[], int n)

{

int result = nums[0];

for (int i = 1; i < n; i++)

result = gcd(nums[i], result);

return result;

}

int main()

{

int *dynamicArray;

int size;

do

  

{

cout<<"Enter the number of cities ( >0)::";

cin>>size;

}while(size<=0);

  

dynamicArray = new int[size];

  

for(int i=0;i<size;i++)

  

{

do

  

{

cout<<"Enter the population of City "<<i+1<<" (>0)::";

cin>>dynamicArray[i];

  

}while(dynamicArray[i]<=0);

  

}

  

cout<<endl<<"Bar plot of the cities ::"<<endl;

int unit=ngcd(dynamicArray,size);

cout<<"\tone * = "<<unit<<endl;

  

for(int i=0;i<size;i++)

{

cout<<"City "<<i+1<<" :: ";

  

for(int j=0;j< dynamicArray[i]/unit;j++)

  

{

cout<<"*";

}

  

cout<<endl;

  

}

  

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is your required code :-

#include<iostream>

using namespace std;

#define NUM_CITIES 4

int main(){

int population[NUM_CITIES], temp;                       // taking population

bool negEntered;

while(1){

negEntered =false;

for(int i=0; i<NUM_CITIES; i++){

cout<<"Enter the population of city "<<(i+1)<<": ";             //taking the population from the user

cin>>population[i];      // taking values

if(population[i]<0)

negEntered = true;                      // condition fot if user entered less than 0 or negative value

}

if(!negEntered)

break;

cout<<"Population can’t be negative. Please re-enter Enter the population of city"<<endl;

}

for(int i=0; i<NUM_CITIES; i++){

cout<<"City "<<(i+1)<<": ";

temp = population[i];

while(temp>0){

cout<<"*";

temp = temp - 1000;

}

cout<<endl;

}

return 0;

}

Output :-

Add a comment
Know the answer?
Add Answer to:
Modify your program above that asks the user to enter number of cities he/she would like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...

    Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • C++ Project Modify the Date Class: Standards Your program must start with comments giving your name...

    C++ Project Modify the Date Class: Standards Your program must start with comments giving your name and the name of the assignment. Your program must use good variable names. All input must have a good prompt so that the user knows what to enter. All output must clearly describe what is output. Using the date class, make the following modifications: Make the thanksgiving function you wrote for project 1 into a method of the Date class. It receive the current...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT