Hey, so I have to delete all occurrences of a number in an array, without using vectors, strings or pointers.I understand that we can't really "Delete" the number since the array isn't dynamic.
What my thought process is that I loop through the array, if the array[i] matches the element we are trying to delete, then shift all of the numbers to the right of that, left one space.
This sounds a lot easier when I'm typing it out but I'm struggling right now with how to actually put it into work.
This is what I currently have.
n is the size
x is the number we are trying to delete
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// looping through the array
for (i = 0; i < n; i++)
{
//if we find the number
if (a[i] == x)
{
// start at that number and go right until end of array
for (int j= x; j <= n - 1; j++)
{
//shift those items to the left 1?
a[j] = a[j + 1];
}
}
}
|
The output is currently looking like:
array =[1,2,3,4,5] - delete the number 3 - [1,2,3,5]
array =[1,2,2,3,4] - delete the number 2 - [1,2,3,4]
All help is appreciated, once again I can't use anything "Fancy" Just pretty much loops.