Filling an array?

Hello, I'm having some trouble with filling an array in my code I'm suppose to write a function that fills an array that looks like this
void fillArray(int ar[], int size, int inc);
The function assumes the 0th element, ar[0] is already filled with some value, and fills the remaining slots, from ar[1] on, with the value of the preceding element plus 'inc'. For instance, when ar[0] was filled with 5, ar[1] should be 8, ar[2] should be 11 and so on.
I have written most of the code but this part for some reason is throwing me off, which honestly at first did not seem so difficult but i have tried everything to and still nothing. if anyone can help me with this fucntion, I would greatly appreciate it. Thank You.
That function is an one-liner, so I'm not sure what you're having problems with.
What have you tried?
I started with a for loop something like this
for (int i = 0; i < size; i++)
after that i tried something like ar[i] + inc. again im kind of new to c++, so i apologize for that. honestly im not too sure what to do after that or if im even on the right track.
Well yeah, now you just need to assign that to ar[i+1] and remember not to step out of bounds:
for (int i=0;i<size-1;i++)ar[i+1]=ar[i]+inc;

Since that is ugly and unnatural, it should be done the other way around:
for (int i=1;i<size;i++)ar[i]=ar[i-1]+inc;

That's a 1-to-1 translation of the task description to C++.

It could also be done like this, which might appeal more to some people (such as me):
for (int i=1;i<size;i++)ar[i]=ar[0]+i*inc;
Oh ok i see now thank you very much, that totally makes sense now thank you very much. You have no idea how much i appreciate this.
Topic archived. No new replies allowed.