### Deleting all occurances of a number in an array 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

 ``1234567891011121314`` `````` // 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.
Last edited on x is a number, not an array index.
But then, on line 8, you initialize j to be x, and use it as an array index.
Perhaps you want to initialize j as i, or something of that nature. if the order of the array is not critical, you can have something like (pseudocode)

if (found value at x)
{
swap(array[x], array[end])
end--; //end is the index of the last active record.
}
this costs much less than shifting all the values every time. But if the array is sorted or something, you can't do this.

Last edited on ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include using namespace std; //========================================================== void remove( int A[], int &n, int value ) { int nextWrite = 0; for ( int nextRead = 0; nextRead < n; nextRead++ ) { if ( A[nextRead] != value ) A[nextWrite++] = A[nextRead]; } n = nextWrite; } //========================================================== void print( int A[], int n ) { for ( int i = 0; i < n; i++ ) cout << A[i] << " "; cout << '\n'; } //========================================================== int main() { int A[] = { 1, 2, 3, 3, 4, 5, 2, 6, 7, 2 }; int n = sizeof A / sizeof A; print( A, n ); remove( A, n, 2 ); print( A, n ); } //========================================================== ``````

 ```1 2 3 3 4 5 2 6 7 2 1 3 3 4 5 6 7``` @lastchance

thanks for your help! Just a question, what is nextwrite supposed to represent? Like I don't understand what it's doing in this case. I understand that nextRead is equivalent to 'i' but idk about next write. The code simply moves two indices forward - nextRead goes through each element in turn; nextWrite is the next available place into which to put an acceptable value.

The names of variables (i or nextRead) are irrelevant. I happen to think that the names here express more obviously (at least, to me) what the variables are intended to represent.
Last edited on