Multiple operations on an array in a for loop

Can I do a for loop on an array and include multiple functions on it on the same line?

I know I can do this:

This will take the input from a user, assign a value to "months_entered" and then run the array through a loop to add 9.95 to each value in the array.

for (int i = 0; i < months_entered; i++) // Adds the base charge
month_array[i] += 9.95;



What if I wanted to perform multiple operations on the array? Like this:

for (int p = 0; p < months_entered; p++)


month_array[p] = (1200 * .73) + ((month_array[months_entered] - 1200) * .037) + ((month_array[months_entered] - 2000) * .77);

Pay attention to the before/after states. Usually you will wish to create a separate array to assign the modified values, which you can copy over the original array when you are done.

Sometimes the update operations are designed to modify the array as you go.

Decide which of these two cases apply and good luck!
In your second example, the RHS appears to be (a) constant and (b) likely to go outside array bounds.

Maybe you should show some actual code?
Topic archived. No new replies allowed.