0

I am making a program that removes a number from the index that the user provides from the array, displays the new array, then asks the user to insert a number at whatever index they choose. The first part of this program works fine when it comes to removing an index, but I am having trouble with adding an index and number. For example, if the NEW array after the user deletes the number from index 5 is: 12 34 45 2 8 16 180 182 22, which is correct if you remember that arrays start at 0, then they request for example index 5 to be added again with the number 78, it gets messed up. It displays 12 34 45 2 78 8 16 180 182 22 (and then it also outputs the number -858993460 for some reason?) SO the problem basically is that it adds the new index and number one index BEFORE it is supposed to. Im sorry if this sounds so confusing but I have been stuck on it for hours. Thank you!

//This program demos basic arrays

#include <iostream>
using namespace std;

const int CAP = 10;

int main()
{
    int size;
    int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
    size = 10;
    int i, delIndex, addIndex, newInt = 0;

    cout << "Your list is: " << endl;
    for (i = 0; i < CAP; i++)
    {
        cout << list[i] << endl;
    } 

//Deleting an index

cout << "\nPlease enter index to delete from: ";
cin >> delIndex;

for (i = delIndex; i <= 10; i++)
{
    list[i] = list[i + 1];

}

cout << "The index position you specified has been deleted." << endl;
cout << "The new array is: " << endl;
for (i = 0; i < (size - 1); i++)
{
    cout << list[i] << endl;
}

//Adding an index

cout << "\nNow, please enter the index position to add to: " << endl;
cin >> addIndex;
cout << "\nEnter the number to add to the index: " << endl;
cin >> newInt;

for (i = size - 1; i >= addIndex - 1; i--)
{
    list[i + 1] = list[i];
}
        list[addIndex - 1] = newInt;
        size++;

    cout << "The number has been added at the specified index position." << 
endl;
    cout << "The new array is: " << endl;
    for (i = 0; i < size; i++)
    {
        cout << list[i] << endl;
    }



    return 0;
}
11
  • for (i = delIndex; i <= 10; i++) {list[i] = list[i + 1];} -- This loop writes to items that are out-of-bounds of the array. Second, arrays cannot be resized, so "removing elements" or "adding elements" is not what you're trying to do. Commented Mar 1, 2018 at 5:19
  • Then I am not sure how to word it. If you test my program, the user is able to request the the index to have a number removed. Those were my instructions, I didn't make this up. Commented Mar 1, 2018 at 5:20
  • It is not "working fine". You are just lucky it works. The array has 10 items, yet you are accessing items well beyond list[9]. You are accessing index 10 and 11, thus creating a buffer overrun and undefined behavior occurs. Commented Mar 1, 2018 at 5:21
  • Change for (i = delIndex; i <= 10; i++) to for (i = delIndex; i < 9; i++) Commented Mar 1, 2018 at 5:22
  • @Ishpreet -- for (i = delIndex; i < 10; i++) -- That still writes out of bounds on the last iteration. Commented Mar 1, 2018 at 5:23

2 Answers 2

1

The problem is with handling size variable in your code.

Following is corrected code. See it working here:

#include <iostream>
using namespace std;

const int CAP = 10;

int main()
{
    int size;
    int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
    size = CAP;
    int i, delIndex, addIndex, newInt = 0;

    cout << "Your list is: " << endl;
    for (i = 0; i < size; i++)
    {
        cout << list[i] << endl;
    } 

    //Deleting an index

    cout << "\nPlease enter index to delete from: ";
    cin >> delIndex;

    for (i = delIndex; i < size; i++)
    {
        list[i] = list[i + 1];
    }
    size--;
    cout << "The index position you specified has been deleted." << endl;
    cout << "The new array is: " << endl;
    for (i = 0; i < size; i++)
    {
        cout << list[i] << endl;
    }

    //Adding an index

    cout << "\nNow, please enter the index position to add to: " << endl;
    cin >> addIndex;
    cout << "\nEnter the number to add to the index: " << endl;
    cin >> newInt;

    for (i = size - 1; i >= addIndex; i--)
    {
        list[i + 1] = list[i];
    }
    list[addIndex] = newInt;
    size++;

    cout << "The number has been added at the specified index position." <<endl;
    cout << "The new array is: " << endl;
    for (i = 0; i < size; i++)
    {
        cout << list[i] << endl;
    }
    return 0;
}

Note: Since we are talking about index and not position in the array, so it is treated as 0 based and element is added at index addIndex, not at addIndex-1.

Sign up to request clarification or add additional context in comments.

3 Comments

This seems to have fixed my program but there is now a new error that is pretty minuscule that maybe you can help me with. When I add the number 675, for example, into the index spot number 3, it does input it in the correct position, however the number "10" which comes after the inputted number (675) repeats itself and appears twice. Do you know why this is?
@user8930130 Corrected now. The problem was with loop condition part. It should be i >= addIndex; not i > addIndex;. Now Corrected in answer.
This works perfectly. Wish everyone here was as helpful as you, and as to the point as you. Thank you!
1

There are some loopholes in your program, that you can improve.

  1. You are declaring a constant (CAP), but the array size is changing, so point doing that.

  2. You are printing the array multiple times, its better to use a function, and call it every time, when you need to print the entire array.

  3. You are using literals like 10, in some sections of the program. It is advisable to use the same variable like size or CAP instead of 10 to improve readability.

  4. You can even put insert & delete in functions so that they can be called multiple times.

This is a sample working code, you can take the idea of that. I haven't implemented Step 4, I hope you can do that easily,

LIVE CODE

Working Code

//This program demos basic arrays
#include <iostream>
using namespace std;
int CAP = 10;

void printArr(int list[]){
    for (int i = 0; i < CAP; i++)
        cout << list[i] << " ";
    cout<<endl;
}

int main()
{
    int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
    int i, delIndex, addIndex, newInt = 0;
    cout << "Your list is: " << endl;
    printArr(list);

    //Deleting an index
    cout << "\nPlease enter index(0 indexed based) to delete from: ";
    cin >> delIndex;
    for (i = delIndex; i < CAP - 1; i++)
        list[i] = list[i + 1];
    CAP--;
    cout << "The index position you specified has been deleted." << endl;
    cout << "The new array is: " << endl;
    printArr(list);

    //Adding an index
    cout << "\nNow, please enter the index position(0 indexed based) to add to: " << endl;
    cin >> addIndex;
    cout << "\nEnter the number to add to the index: " << endl;
    cin >> newInt;
    for (i = CAP; i > addIndex; i--)
        list[i] = list[i-1];        
    list[addIndex] = newInt;
    CAP++;
    cout << "The number has been added at the specified index position." << endl;
    cout << "The new array is: " << endl;
    printArr(list);
    return 0;
}

4 Comments

When I put this program into visual studio, I get the error message "const int CAP = 10, expression must be a modifiable value". Do you know why this is?
@Ishpreet This just replacing the element at the index addIndex - 1. Not adding. Have a close watch on the out put. Also you can see 22 two times at the end of output.
@cse Yeah I missed that, Updated the code as well as working link. Good catch
@user8930130 You cannot change the value of a constant Use variable instead. Refer this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.