Array Manipulator not working correctly?

I have to write an array manipulator with 6 functions, which are all called in main. I wrote most of the functions, but the ones I am stuck on is one where the user chooses from a menu to add an element at a specified index, and another function where the user chooses to remove an element. This is the code I have so far for the insertElement function:

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
 
int main()
{
// other cases;
           case 5:
                cout << "Enter a value to insert: ";
                cin >> insert;
                cout << "Enter a position to insert the value in: ";
                cin >> pos;
                pos--;
                if (pos < 0 || pos > size)
                {
                    cout << "Invalid Index " << endl;
                }
                else
                {
                    size = insertValue(arr, insert, pos, size);
                    displayArray(arr, size);
                }
                break;

        }
       choice = displayMenu();
        
    }
}

int insertValue(int arr[], int value, int pos, int size)
{
    if (size == 10)
        cout << "Array full" << endl;
    else
    {
        int i;
        for (i = size - 1; i >= pos; --i) {
            arr[i + 1] = arr[i];
        }
        arr[i] = value;
        ++size;

    }
    cout << endl;
    return size;
}

I think my function for adding an element is right, but when I run it with array values of 3 4 5, and tell the program to add the number 4 at index 1, it prints 4 4 4 5. how can I make the program print 3 4 4 5?
Thank you for all your help!
Last edited on
1
2
if (size == 10)
        cout << "Array full" << endl;

this is not a valid check for whether an array is full or not. In fact there is no check possible to determine if a C-style array is indeed full, one usually finds that out in the unpleasant way of an array element trying to access out-of-bounds memory:
http://stackoverflow.com/questions/19631873/c-how-do-i-check-if-an-array-is-full

what you can do however is to make sure that the position at which the new element is to be inserted does not try to access such out-of-bound memory, akin to the C-program here which you can try and modify to C++ if you find it useful:
http://www.codeforwin.in/2015/07/c-program-to-insert-element-in-array.html

ps. on this site you'll also find a C-program to delete element from C-style arrays
Thank you for your suggestion! I fixed the errors for the insert value function,, but now I am having trouble with the remove value function. This is my code:
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
// main: {
 case 5:
                cout << "Enter a position to remove from the array: ";
                cin >> newPos;
                newPos++;
                if (newPos < 0 || newPos > size)
                {
                    cout << "Invalid Index " << endl;
                }
                else
                {
                    size = removeValue(arr, newPos, size);
                    displayArray(arr, size);
                }
                break;

}
int removeValue(int arr[], int newPos, int size)
{
    int i = 0;
    for(i = newPos - 1; i < size - 1; i++)
        {
            arr[i] = arr[i+1];
        }

        size--;

        cout << endl;
        return size;
}


The program runs, but when told to remove the 2nd element in an array of 1 2 3 4 it prints 1 2 4. Is something wrong with my main code?
Your removeValue() seems fine, the problem might be in the displayArray():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int removeValue(int arr[], int newPos, int size)
{
    int i = 0;
    for(i = newPos - 1; i < size - 1; i++)
        {
            arr[i ] = arr[i+1];
        }

        size--;

        std::cout << std::endl;
        return size;
}

int main()
{
    int ar[] = {1, 2, 3, 4};
    removeValue(ar, 1, 4);
    for (size_t i = 0; i < 3; ++i)std::cout << ar[i] << " ";
}
Thank you for your help!
Last edited on
Topic archived. No new replies allowed.