What is wrong with the following code? for(int i = 0; i <=phrase.size( ); ++i) {
ID: 3666874 • Letter: W
Question
What is wrong with the following code?for(int i = 0; i <=phrase.size( ); ++i) { cout << "Character at position " << i << " is: " << phrase[i] << endl; } What is wrong with the following code?
for(int i = 0; i <=phrase.size( ); ++i) { cout << "Character at position " << i << " is: " << phrase[i] << endl; }
for(int i = 0; i <=phrase.size( ); ++i) { cout << "Character at position " << i << " is: " << phrase[i] << endl; }
Explanation / Answer
The array counts from 0
phrase.size gives the number of elements
give i < phrase.size
because the array index is 0 to n-1
If possible give unsigned int i=0
Because sometimes it will throw an error
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.