Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this exercise, you use what you have learned about parallel arrays. Study the

ID: 3691935 • Letter: I

Question

In this exercise, you use what you have learned about parallel arrays. Study the followingcode, and then answer Questions 1 – 4. Note that this code may contain errors.

string cities[] = "Chicago", "Rome", "Paris", "London";

int populations[] = 2695598, 2760665, 2190777, 7805417;

const int MAX_CITIES = 4;

int foundIt;

int i, x;

string inCity;

cout << "Enter city name: ";

cin >> inCity;for(i = 0; i = MAX_CITIES; ++i)

{

if(inCity == cities[i])

{

foundIt = i;

}

}

cout << "Population for " << cities[foundIt] <<

" is " << populations[foundIt] << endl;

1. Are the arrays declared and initialized correctly?If not, how can you fix them?

2. Is the for loop written correctly?If not, how can you fix it?

3. As written, how many times will the for loop execute?

4. How would you describe the purpose of the statement foundIt = i; ?

Explanation / Answer

the correct working code is given below

------------------------------------------------------------------------------

1) the arrays are declared properly but are not initialised properly.

they can be declared properly using the following syntax

string cities[] = {"Chicago", "Rome", "Paris", "London"};
int populations[] = {2695598, 2760665, 2190777, 7805417};

2)the for loop is not written correctly... the condition is not given properly

for(i = 0; i < MAX_CITIES; ++i) is the correct way

3) as given the loop will execute infinite number of times

as the conditions are not meeting

4)the foundit=i will give the index of the city whose correct choice user has entered

The correct working code is given below.

#include<iostream>
using namespace std;
int main()
{
string cities[] = {"Chicago", "Rome", "Paris", "London"};
int populations[] = {2695598, 2760665, 2190777, 7805417};
const int MAX_CITIES = 4;
int foundIt;
int i, x;
string inCity;
cout << "Enter city name: ";
cin >> inCity;
for(i = 0; i < MAX_CITIES; ++i)
{
if(inCity == cities[i])
{
foundIt = i;
}
}
cout << "Population for " << cities[foundIt] <<" is " << populations[foundIt] << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote