PS2. You do have "magic value" 10 on lines 8 and 12. What if you later change one, but forget to fix the other(s)?
Furthermore, you both state the size of array and have some initializers for it. What if their count differs?
lines 12-18 is all i added to this pre-existing code. My task is to complete it using a for loop. I omitted line 15 and the {} in the for loop and
now I'm prompted that "x" is undefined line 17
The big problem with the code in the OP is that x is being incremented inside the loop and also in the update section of the for construct for each iteration of the loop. That means that every other value is skipped.
The average can be calculated after the loop, although you won't get the wrong answer if you do it in the loop - you'll just be doing more work than is necessary.
Well, I was mistaken. To get the average, you must divide by the number of elements in the array, which would be 10. Within the loop, x can never be equal to 10.
Now your loop adds 11 values together, but your array has only 10 values in it.
Furthermore, the first iteration of the loop still divides with 0.
Move the calculation of the average to the outside of the loop. Move line 14 into line 16.
Fix the range of the loop. It is undefined behaviour to dereference nonexisting prices[10].
Of course the x is undefined outside of the loop. It is declared in the initialization part of the loop and thus its lifetime ends when the loop ends.
You do assume that the array has 10 elements and your loop should iterate 10 times. Similarly, you can divide with 10, rather than with x.