When the following program is compiled, the output is if break float while break.
#include<iostream>
using namespace std;
string s[5]={"if","int","float","while","break"};
string & blast(int i){ return s[i];}
int main()
{
for (int i = 0; i < 5; i++ )
if( i % 3 == 1 )
blast( i ) = s[ 5-i ];
for (int i = 0; i < 5; i++ )
cout << s[ i ] << " ";
cout<<endl;
return 0;
}
Attempt:
blast[1] = s[4] = "break"
so, s[1] = "break"
Then blast[4] = s[4] = s[1] = "int"
but output doesn't agree with this.
I didn't understand this.. Please help me out.
string &blast (int i)
returns a reference to the string within the array to which you then assign a new string, e.g.blast(i) = s[5 - i];
is the same ass[i] = s[5 - i];
Additionally, if you space your code a little more, it will be a lot more readable (especially for older eyes), e.gfor (int i = 0; i < 5; i++)
andif (i % 3 == 1)
, etc.. – David C. Rankinstd::swap(blast(i), s[5-i]);
? – AconcaguaINCREMENT_BOTH(X, Y) ++X; ++Y;
) or against forgetting to place them on adding further expressions. – Aconcagua