I have a homework assignment where we're supposed to create a list of integers, and allow the user to INSERT an integer at a given position in the list. The list should essentially move all integers over one position in the array, then insert the integer that the user input at the index they chose.
So let's say I have an array of {1, 2, 3, 4, 5} and I want to put 9 in index 1, the array should then be {1, 9, 2, 3, 4, 5}.
I thought I figured out the solution with this function:
void *INSERT(int count, int userNum, int index, int array[]){ //Accepts count of array, users number, users index, and array
int tempIndex = index;
index--;
for(int counter = 0; counter < count; counter )
{
array[tempIndex] = array[tempIndex-1];
tempIndex ;
}
array[index] = userNum;
}
Here is what I have in the main function:
int index = 0; //Hold users index selection
int userNum = 0; //Hold users number
int n = 10;
int a[n] = {1, 2, 3, 4, 5};
int *ptr = a;
cout << "Enter a number to insert: ";
cin >> userNum;
cout << "Enter an index: ";
cin >> index;
INSERT(n, userNum, index, ptr);//call insert function (Accepts count of array, users number, users index, and array)
int listNum = 1;
for(int i = 0; i < n; i ) {
cout << listNum << ". " << a[i] << "\n";
listNum ;
}
However, this is the output after printing the array:
1. 1
2. 9
3. 2
4. 2
5. 2
6. 2
7. 2
8. 2
9. 2
10. 2
I'm not sure where I'm going wrong here that could be causing this output. If I remove the -1 from the INSERT functions for loop like this(commented it out to make it more clear on what's being changed):
void *INSERT(int c, int n, int i, int a[]){ //Accepts count of array, users number, users index, and array
int x = i;
i--;
for(int r = 0; r < c; r )
{
a[x] = a[x/*-1*/];
x ;
}
a[i] = n;
}
I get the following output with the above code, which is what I'd expect but not what I need. I can post the entire code if needed as well, but I think this explains where the problem is. Thanks in advance for the help
1. 1
2. 9
3. 3
4. 4
5. 5
6. 0
7. 0
8. 0
9. 0
10. 0
CodePudding user response:
Based on your code, just change your insert function to this -
Here we first shift the values to next index in the array and then perform insertion of respective element
void *INSERT(int c, int n, int i, int a[]){ //Accepts count of array, users number, users index, and array
for(int r = c-1; r >= i; r--)
{
a[r 1] = a[r];
}
a[i] = n;
}
CodePudding user response:
This answer may be somewhat controversial because instead of directly solving your assignment for you I'm just telling you how to debug it so you can solve it yourself.
So fixed your code for you:
//Accepts count of array, users number, users index, and array
void *INSERT(
int array_size,
int number_to_insert,
int insert_at,
int numbers[]){
int moving_index = insert_at;
insert_at--; // Why are you doing this?
for(int r = 0; r < array_size; r )
{
// moving_index was initialized as insert_at
// What happens when insert_at is 0?
// What happens when moving_index >= array_size?
// Who knows...
numbers[moving_index] = numbers[moving_index-1];
// You're incrementing moving_index.
moving_index ;
// What would numbers[moving_index-1] be now?
}
numbers[insert_at] = number_to_insert;
}
Once you answer my questions, I'm sure you'll be able to figure it out.
