[cs13001] Practice Exam Question

Mikhail Nesterenko mikhail at cs.kent.edu
Tue Mar 5 23:31:08 EST 2013


> I am having trouble with question 7 on the practice exam. Can you tell me
> what is wrong with this code? I can't get it to work properly.
> 
> for(int i=0; i<SIZE; i++)
>  if(myarray[i] == number)
>	cout << "FOUND";
>  else
>	cout << "NOT FOUND";
> 

This code output FOUND or NOT FOUND tem times. Instead, it should
output FOUND or NOT FOUND once. You need an extra variable to keep
track whether the number is found or not. For example:


   bool located=false;
   for(int i=0; i < SIZE; i++)
      if(myarray[i] == number)
         located = true;
   if(located)
      cout << "FOUND";
   else
      cout << "NOT FOUND";

Thanks,
-- 
Mikhail


More information about the cs13001 mailing list