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

Write a function named insertinArray that takes the parameters list as an intege

ID: 3746627 • Letter: W

Question

Write a function named insertinArray that takes the parameters list as an integer array, size for the capacity of the array, numltems has the number of items in the array, index which is the index where the new item should go, and newVal has the new value to be inserted into the array. If the array is at capacity then the function should return-1, otherwise return 0 for success int insertInArray(int list[ ], int size, int numItems, int index, int newval) For example, if the array has (9, 2, 8, 3) and the array has the capacity to add a new value, and the new value is '5' at index '2', then the resulting array should be (9, 2, 5, 8, 3) and the function should return O. You will also need to add one to the numltems variable.

Explanation / Answer

If you have any doubts, please give me comment...

#include<iostream>

using namespace std;

int insertArray(int list[], int size, int *numItems, int index, int newVal);

int main(){

int list[5] = {9,2,8,3};

int numItems = 4;

insertArray(list, 5, &numItems, 2, 5);

for(int i=0; i<numItems; i++)

cout<<list[i]<<" ";

cout<<endl;

return 0;

}

int insertArray(int list[], int size, int *numItems, int index, int newVal){

if(size<=*numItems)

return -1;

for(int i=(*numItems)-1; i>=index; i--){

list[i+1] = list[i];

}

list[index] = newVal;

(*numItems)++;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote