Question

In: Computer Science

n this lab, you use what you have learned about searching an array to find an...

n this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewritten C++ program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user to enter a city name; your program then searches the array for that city name. If it is not found, the program should print a message that informs the user the city name is not found in the list of valid cities in Michigan.

The file provided for this lab includes the input statements and the necessary variable declarations. You need to use a loop to examine all the items in the array and test for a match. You also need to set a flag if there is a match and then test the flag variable to determine if you should print the the Not a city in Michigan. message. Comments in the code tell you where to write your statements. You can use the previous Mail Order program as a guide.

Instructions

  1. Ensure the provided code file named MichiganCities.cpp is open.
  2. Study the prewritten code to make sure you understand it.
  3. Write a loop statement that examines the names of cities stored in the array.
  4. Write code that tests for a match.
  5. Write code that, when appropriate, prints the message Not a city in Michigan..
  6. Execute the program by clicking the Run button at the bottom of the screen. Use the following as input:
    Chicago
    Brooklyn
    Watervliet
    Acme

// MichiganCities.cpp - This program prints a message for invalid cities in Michigan.
// Input: Interactive
// Output: Error message or nothing

#include <iostream>
#include <string>
using namespace std;

int main()
{
// Declare variables
string inCity;   // name of city to look up in array
const int NUM_CITIES = 10;
// Initialized array of cities
string citiesInMichigan[] = {"Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"};
bool foundIt = false; // Flag variable
int x;        // Loop control variable

// Get user input
cout << "Enter name of city: ";
cin >> inCity;
  
// Write your loop here   
if inCity = citiesInMichigan then "A city in Michigan" if else then "Not a city in Michigan";
// Write your test statement here to see if there is
// a match. Set the flag to true if city is found.
  

          
          
// Test to see if city was not found to determine if
// "Not a city in Michigan" message should be printed.
  
  
return 0;  

} // End of main()

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// MichiganCities.cpp - This program prints a message for invalid cities in Michigan.
// Input: Interactive
// Output: Error message or nothing

#include <iostream>
#include <string>
using namespace std;

int main()
{
// Declare variables
string inCity; // name of city to look up in array
const int NUM_CITIES = 10;
// Initialized array of cities
string citiesInMichigan[] = {"Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"};
bool foundIt = false; // Flag variable
int x; // Loop control variable

// Get user input
cout << "Enter name of city: ";
cin >> inCity;
  
// Write your loop here   
//if inCity = citiesInMichigan then "A city in Michigan" if else then "Not a city in Michigan";
// Write your test statement here to see if there is
// a match. Set the flag to true if city is found.
for(x=0;x<NUM_CITIES;x++)
{
   if(citiesInMichigan[x].compare(inCity)==0)
   {
       foundIt=true;
       cout<<inCity<<" is A city in Michigan"<<endl;
   }
}
  

  
  
// Test to see if city was not found to determine if
// "Not a city in Michigan" message should be printed.
if(!foundIt)
{
       cout<<inCity<<" is not A city in Michigan"<<endl;
}
  
return 0;

} // End of main()

____________________________

Output:

_______________Could you plz rate me well.Thank You


Related Solutions

Searching an Array for an Exact Match in Java Summary In this lab, you use what...
Searching an Array for an Exact Match in Java Summary In this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewritten Java program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user of the program to enter a city name; your program then searches the array for that city name. If it is not found, the program should print...
Find an article about the misuse of statistics and share what you have learned from the...
Find an article about the misuse of statistics and share what you have learned from the article. Then, find an example of when statistics were misused/skewed and state the reason why you think the statistics were reported that way.
Use what you have learned about the time value of money to analyze each of the...
Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1:   Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $10,000 today.    Option B: Receive a $1600 gift each year for the next 10...
Use what you have learned about the time value of money to analyze each of the...
Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1:   Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $10,000 today.    Option B: Receive a $1600 gift each year for the next 10...
Use what you have learned about the time value of money to analyze each of the...
Use what you have learned about the time value of money to analyze each of the following decisions: Decision #2: Planning for Retirement Erich and Mallory are 22, newly married, and ready to embark on the journey of life.   They both plan to retire 45 years from today. Because their budget seems tight right now, they had been thinking that they would wait at least 10 years and then start investing $1800 per year to prepare for retirement.   Mallory just...
Use what you have learned about the time value of money to analyze each of the...
Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1:   Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $ 10,000 today.    Option B: Receive a $1400 gift each year for the next 10...
Use what you have learned about the time value of money to analyze each of the...
Use what you have learned about the time value of money to analyze each of the following decisions: Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $10,000 today.    Option B: Receive a $1600 gift each year for the next 10 years. The first $1600 would be received 1 year from today.   ...
Use what you have learned about the time value of money to analyze each of the...
Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1:   Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $ 10,000 today.    Option B: Receive a $1400 gift each year for the next...
Considering what you have learned in this lab and in class, write a hypothesis to explain...
Considering what you have learned in this lab and in class, write a hypothesis to explain why we observe a higher fraction of elliptical galaxies within a cluster and a higher fraction of spiral galaxies in the field. Think about how being in a crowded cluster versus alone might affect the morphology or appearance of a particular galaxy.
Step 1: Read the following questions, and use what you have learned about how the economy...
Step 1: Read the following questions, and use what you have learned about how the economy might impact your business industry, and to summarize your responses in 5 full pages. Consider the concepts and questions below when assessing the economy and the impact it may pose on your business: Describe your ideal capitalist economy. Will these conditions maximize your sustainability and profitability? What indicators would you use to measure goods and services? What happens when the quantity demand is impacted?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT