Delete element in array and shift array to the left

Please help me. I've spent a few hours trying to figure this out but I'm still very new at arrays and have had a hard time finding a tutorial on this specific topic. I need help with the deleteArray function in my code below. How can I remove the "5" and shift the values left of the five to the right.
It should look like:
0 1 2 3 4 6 7 8 9 10
11 12 13 14 15 16 17 18 19

I was able to change the "5" to a "0" but I haven't a clue how to remove it and then shift.

*UPDATE: I think I figured it out! Not a clue what it's doing but the 5 was located and removed. I updated the code below. Please let me know if something looks wrong or could be better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iomanip>
#include <iostream>

using namespace std;

void printArray(int arr[],int len)
{
    for(int i = 0; i < len; i++)
    {
        if (i>0 && i % 10 == 0)
            cout<<endl;
        cout<<setw(2)<<arr[i]<<" ";
    }
    cout<<endl;
}
void deleteArray(int original[], int origLen, int newArray[], int index){

    for(int i = 0; i < origLen-1; i++){
        newArray[i] = original[i];
    int index = 0;
        }for(int x = 0; x < origLen-1; x++){
            newArray[index] = 0;
        }
}
void biggerArray(int original[], int origLen, int newArray[], int newLength){
    for(int i = 0; i < origLen; i++){
        newArray[i] = original[i];
    }
}

int main()
{   int intArray[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
    int smallArray[19] = {};
    int bigArray[25] = {};

    cout<<"Original array: "<<endl;
    printArray(intArray,20);
    cout<<endl;

    cout<<"Smaller array:  "<<endl;
    deleteArray(intArray,20,smallArray,5);
    printArray(smallArray,19);
    cout<<endl;



    return 0;
}
Last edited on
- you should use std::vector instead of C-style arrays. Whoever is teaching you to use c-style arrays in C++ is a bad teacher.

- the answer to this particular question is best given in my book "programming for Beginners - with C++" www.amazon.com/Programming-Beginners-C-Kevin-Compton/dp/1511806982/

- this should fix your problem:
1
2
3
4
5
6
7
void deleteArray(int original[], int origLen, int newArray[], int index){

    for(int i = 0; i < index; i++)
        newArray[i] = original[i];    
    for(int i = index; i < origLen-1; i++)
        newArray[i] = original[i+1];        
}


Thank you, I will definitely look into the book.
I ended up using:
1
2
3
4
5
6
7
8
9
void deleteArray(int original[], int origLen, int newArray[], int index){

    for(int i = 0; i < origLen-1; i++){
        newArray[i] = original[i];
    int index = 0;
        }for(int x = index; x < origLen-1; x++){
            newArray[x] = original[x+1];
        }
}
What's "int index=0;" for? You know that it is not doing anything?
Last edited on
Topic archived. No new replies allowed.