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;
}
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 :-

Modify your program above that asks the user to enter number of cities he/she would like...
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 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 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; ...
Can you all fix this coding below, if so "ASAP" would be great? Write a program that prompts the user to input a string and outputs the string in uppercase letters using dynamic arrays. int main() { char *str; int strLen; int len; char ch; int i; cout << "Enter the size of the string: "; cin >> strLen; cout << endl; cin.get(ch); str = new char[strLen + 1]; cout << "Enter a...
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,...
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...
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[],...
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;...
Create a program with the main function and one additional function. The additional function asks for input from the user and returns the output back to the main function for display. This is for c++ and here is my code, could you guys please tell me what I am missing from the instructions. Thank you #include <iostream> using namespace std; int sum (int, int); int main() { int n1, n2, total; cout << "Enter two numbers...
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...