This function crashes the program whenever cPos(current item position) = 0. I cant figure out why either. i checked to see if it was accessing memory outside of the array but its not. what the for loop is suppose to do is shift everything until a[cPos -1] to the right. for example:
a[CAPACITY] = [20, 30, 40, 0, 0, 0]
When cPos = 2 and after the for loop the array should look like this after inserting 50
a[CAPACITY] = [20, 30, 50, 40]
But when cPos is 0 the program crashes. Would you guys happen to know what i did wrong?
In else part your doing used++; and again in for loop it will initialized int i = used; it seems like program will never end and will crash...
please use, that may be useful to you. staticint i = used;
For which values of used does it crash?
What does the size() function return? Does it just return used?
I would see a crash when used == CAPACITY - 1 then in the first iteration of the for loop you would write into a[CAPACITY] which is not correct. If it is the case, you need to rewrite the for loop as: for(int i = used - 1; i >= cPos; i--).
If it is not - maybe there's a problem with class member initialization, but you'd have to post the rest of your code for us to see where is the problem.
Also this kind of problems should be easily fixed using a debugger and step-by-step execution. Did you try that?
This code snippet does not crash just because cPos is zero, that's not the reason.
I wonder what is the meaning of the two variables cPos and used? Because you're using both as indices to access data in your array a, which i find quite confusing. Did you intend that?
Also consider the situation if used is equal to the maximum capacity and cPos is 30. In this case the if statement evaluates to true and it will try to access a[used], although used is out of range (which is 0...used-1).
Btw. i don't see how making i static will solve the problem.
First a design issue: In my opinion it would be more elegant to replace the the typedefs and CAPACITY with template parameters.
Also, if used is equal to CAPACITY-1, you'll try to access a[used+1] = a[CAPACITY] in the for loop, this might be the reason why your program crashes. Other than that i cant find anything wrong in the code.
If this doesn't help i suggest to post the portion of the code where you create an instance of the sequence class and call the methods, the error might be hidden somewhere there.