int array_with_loop(vector<int> data)
{
int i;
data.push_back(6);
data.push_back(9);
data.push_back(2);
data.push_back(-7);
data.push_back(-4);
int result = data[0];
for ( i =0; i < data.size(); i++ )
{
if ( data[i] < result )
result = data[i];
}
return result;
}
Hand execute the following code on paper for array data, variables i and result, and then indicate the returned value of array_with_loop function .
data.push_back(6);
// array status: 6
data.push_back(9);
// array status: 6| 9
data.push_back(2);
// array status: 6| 9| 2
data.push_back(-7);
// array status: 6| 9| 2| -7
data.push_back(-4);
// array status: 6| 9 |2| -7| -4
int result = data[0];
result = 6
for ( i =0; i < data.size(); i++ )
{
if ( data[i] < result )
result = data[i];
}
i = 0
data[0] < result
6 < 6 false
i = 1
data[1] < result
9 < 6 false
i = 2
data[2] < result
2 < 6
result = data[2]
result = 2
i = 3
data[3] < result
-7 < 2 true
result = -7
i = 4
data[4] < result
-4 < -7 false
i =5
i > data.size() loop stops
so, variable i = 5
result = -7 ans
/* PLEASE UPVOTE */
Get Answers For Free
Most questions answered within 1 hours.