Answer to Question #201452 in C++ for shubo

Question #201452
The definition of the function quadrupler is given again below.
void quadrupler(int& n)
{
n = 4 * n;
}
what (if anything) is wrong with the following loop that uses the function quadrupler?
int b[5] = {5, 6, 7, 8, 9};
for (int i = 1; i <= 5; i++)
{
quadrupler(b[i]);
}
1
Expert's answer
2021-06-04T06:07:52-0400

There is UB in the program due to access to array element out of array bounds.

// trying to access b[5] whereas size of array b is 5. 
// The last element you can work with is b[4].
for (int i = 1; i <= 5; i++)

need to be fixed to:

for (int i = 1; i <= 4; i++) 

or even (start with 0):

for (int i = 0; i <= 4; i++).

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog