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

string apples[] = {\"Gala\", \"Rome\", \"Fuji\", \"Delicious\"}; int foundIt = f

ID: 3691930 • Letter: S

Question

string apples[] = {"Gala", "Rome", "Fuji", "Delicious"};

int foundIt = false, i;

const int MAX_APPLES = 4;

string inApple;

cout << "Enter apple type:";

cin >> inApple;

for(i = 0; i <= MAX_APPLES; i++)

{

(inApple == apples[i])

{foundIt = true;

}

}

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

2. Which variable is the flag?

3. Is the flag variable declared correctly?If not, what should you do to fix it?

4. is the comparison in the IF statement done correctly ? if not how can you fix it

Explanation / Answer

1) The loop should stop when i becomes MAX_APPLES. so the write method is

for(i = 0; i <MAX_APPLES; i++)

{

(inApple == apples[i])

{foundIt = true;

}

}

2) foundit is the flag variable.

3) the variable foundit should be of type bool. So the correct declaration is:

bool founit=false;int i;

4) No it is not done correctly. You cannot compare strings with ==. The correct way is:

if(inApple.compare(apples[i])==0)

{foundIt = true;

}