# Question & Answer: C++ CodeRunner Problem:…..

C++ CodeRunner Problem:

Show transcribed image textWrite a function named insertlnArray that takes the Knowing parameters: listInput a char array cap: capacity of the array numItems: number of items currently in the array index: index at which to insert the new item newVal: value to insert into the array If the array is at capacity then the function should return -1. Otherwise, insert newVal at index and return 0 for success. int insertInArray(char listInput[], int cap, int^x numItems, int index, char newVal) For example, if the array is {‘9’, ‘2’, ‘8’, ‘3’} and it has the capacity to add a new value of 5 at index 2, then the resulting array should be {‘9’, ‘2’, ‘5’, ‘8’ ‘3’} and the function should return 0. The numItems variable will increment from A to 5. Answer: (penalty regime: 0, 5, 10, …%)

#include<stdio.h>
#include<stdlib.h>

int insertInArray(char listInput[], int cap, int *numItems, int index, char newVal) {

int i;
if (*numItems == cap)
return -1;

for (i = *numItems+1; i > index; i–){
listInput[i] = listInput[i-1];
}
listInput[index] = newVal;
*numItems = *numItems + 1;
return 0;
}

int main(){

char list[5];
int i;
int *num;
num = (int *)malloc(sizeof(int));

list[0] = ‘9’;
list[1] = ‘2’;
list[2] = ‘8’;
list[3] = ‘3’;
*num = 4;

if (insertInArray(list,5,num,2,’5′) == 0){
for (i = 0; i<*num; i++)
printf(“%c “,list[i]);
printf(“n”);
}
if (insertInArray(list,5,num,3,’6′) == 0){
for (i = 0; i<*num; i++)
printf(“%c “,list[i]);
printf(“n”);
}
else {
printf(“It is fulln”);
}

return 0;
}