In: Computer Science
Assume the following declarations have been made:
-----------------------------------
char item[50];
char guess;
-----------------------------------
Furthermore, suppose that all of the elements of an item have
already been initialized, and that the user has already entered a
value for guess.
Write a code fragment to find and print out each index i for which
item[i] is the same as guess.
Be sure to specify any additional necessary variable declarations.
As an example, consider the case where the rst 10 elements of item
are (in order)
'a', 'b', 'c', 'd', 'a', 'b', 'b', 'c', 'f', 'a',
and the remaining elements are all 'y'. If guess were 'b', then
your code should print out the following indices:
1 5 6
Here is the completed code snippet for this problem. Assuming the language is C or C++. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
//looping from i=0 to i=49
for(int i=0;i<50;i++){
//comparing item at index i with guess
if(item[i]==guess){
//printing the index followed by a space
cout<<i<<" ";
//if the language is C, use: printf("%d ",i);
}
}
//printing newline character at the end.
cout<<"\n";
//if the language is C, use: printf("\n");