Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to fix the for loop in the function Insert to correctly insert an el

ID: 3623569 • Letter: I

Question

I'm trying to fix the for loop in the function Insert to correctly insert an element into the array at position "position" before insertion the array looks like this:5 7 9 2
after the insertion element 6 at position 2, array looks like this: 5 7 6 9 2
here is my code:

#include<iostream>
using namespace std;

int Insert(int [], int, int);
const int SIZE= 10;

int main()
{
int array[SIZE], x= 0, position= 1, aSaize= 4;

//array[0]=1, array[1]= 3, array[2]= 5, array[3]= 7

for(int i= 0; i < aSaize; i++)
array[i]= 2*i+1

//insert value 0 in position 1 (x is 0, position is 1)
aSize= Insert(array, x, position, aSize);

//display values after the insertion
for(int i=0; i< aSize; i++)
cout<<array[i]<<" ";
cout<<endl;

return 0;
}

//this function inserts "element" in the given "position" in array "arr".
//it returns the new array size

int Insert(int arr[], int element, int position, int size)
{
for(int i= position; i < size; i++)
arr[i+1]= arr[i];
arr[position]= element;
++size;

return size;
}

Explanation / Answer

please rate - thanks

I think I've highlighted all the changes

#include<iostream>
using namespace std;

int Insert(int [], int,int, int);
const int SIZE= 10;

int main()
{
int array[SIZE], x= 0, position= 1, aSize= 4;

//array[0]=1, array[1]= 3, array[2]= 5, array[3]= 7

for(int i= 0; i < aSize; i++)
array[i]= 2*i+1;

//insert value 0 in position 1 (x is 0, position is 1)
aSize= Insert(array, x, position, aSize);

//display values after the insertion
for(int i=0; i< aSize; i++)
    cout<<array[i]<<" ";
cout<<endl;
system("pause");
return 0;
}

//this function inserts "element" in the given "position" in array "arr".
//it returns the new array size

int Insert(int arr[], int element, int position, int size)
{
for(int i= size ; i >position; i--)
    arr[i ]= arr[i-1];
arr[position]= element;
++size;

return size;
}