Question

In: Computer Science

Searching an Array for an Exact Match C++ This question is on MindTap Cengage Summary In...

Searching an Array for an Exact Match C++

This question is on MindTap Cengage

Summary

In 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

Ensure the provided code file named MichiganCities.cppis open.

Study the prewritten code to make sure you understand it.

Write a loop statement that examines the names of cities stored in the array.

Write code that tests for a match.

Write code that, when appropriate, prints the message Not a city in Michigan.

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

  

// 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

Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation.

Please provide your feedback
Thanks and Happy learning!

// 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;


    for (x = 0; x < NUM_CITIES; ++x)
    {
        //Check whether the city name is there in the array or not
        if (!citiesInMichigan[x].compare(inCity))
        {
            //If the city name is present then set the found flag to true.
            foundIt = true;
            break;
        }
    }

    //Check to see whether the city was found
    if (!foundIt)
    {
        //If city was not found in th list, then print "Not a city in Michigan" message
        cout << inCity << " is not a city in Michigan. " << std::endl;
    }

    return 0;

} // End of main()

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...
Parallel Arrays This question is in MindTap Cengage Summary In this lab, you use what you...
Parallel Arrays This question is in MindTap Cengage Summary In this lab, you use what you have learned about parallel arrays to complete a partially completed C++ program. The program should: Either print the name and price for a coffee add-in from the Jumpin’ Jive Coffee Shop Or it should print the message Sorry, we do not carry that. Read the problem description carefully before you begin. The file provided for this lab includes the necessary variable declarations and input...
In C, write a program that initializes a 3D array (the exact size doesn't matter) that...
In C, write a program that initializes a 3D array (the exact size doesn't matter) that contain what ever arbitrary integers and print out the array including the elements and use different functions so that the main only contains variables and function calls.
Propose an algorithm in C to match numbers / tokens (words) from one array to another...
Propose an algorithm in C to match numbers / tokens (words) from one array to another array and pull out the matching numbers.
C++ Please read the question carefully and match the output example given at the end! Question...
C++ Please read the question carefully and match the output example given at the end! Question In this project you will implement operator overloading for a two dimensional Vector class. Begin with the declaration of the Vector class on the final page. There are three operators that must be overloaded insertion (​<<​), addition (​+​), and subtraction (​-​). insertion (​<<​): The insertion operator is used to send the values of a vector to an ostream object, if a vector has two...
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...
Question 3: Exact Differential Equation i. Test to see if the following equation is exact, if...
Question 3: Exact Differential Equation i. Test to see if the following equation is exact, if exact solve for the general solution (??/??) +(2? ????+?^3?^?)/(?^2cos?+3?^2?^?)=0 ii. Solve (?+????)??+(?????−2?)??=0 iii. Solve (?^2+?)??+(?^?−?)??=0 iv. (2?−3?)??+(2?−3?)??=0 I am sorry I could not send a photo. Whenever I try from the app it posts another question on the same page. I tried doing them individually and the only thing that happened was that the same question got posted 4 times. I would appreciate your...
Write a function hitRate(A, B) to return the percentage of elements in array A that match...
Write a function hitRate(A, B) to return the percentage of elements in array A that match the elements with the same index in array B. A and B are 1darrays or 2darrays of the same shape. Sample: if X = np.array([1, 3, 5, 8]); Y = np.array([2, 1, 3, 8]), then hitRate(X, Y) returns 0.25. in python program
C++ QUESTION: I had originally made the program with a 8by2 array of "moves" for "knights...
C++ QUESTION: I had originally made the program with a 8by2 array of "moves" for "knights tour". Now that I am trying to make each (row) and (column) individual arrays, I am not able to get the proper output. I'm sure it is a simple error in my for loops, but I haven't been able to make it work. Can you tell me what I need to change ? // September 11, 2019 // Program sets all of an 8...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT