Question

Must be C++ 11

Write a function named insertinArray that takes the following parameters: list: an integer array index: index at which to insert the new item numitems: number of items currently in the array arrayCapacity: capacity of the array 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 O for success int insertInArray(int listl 1, int index, int numItems, int arrayCapacity, int 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 O. The numitems variable will increment from 4 to 5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER: Here I am giving you the code and output if you have any problem then you can comment or like my solution.

CODE:

#include <iostream>

using namespace std;

int insertInArray(int list[],int index, int *numItems,int arrayCapacity, int newVal)

{

if(index>arrayCapacity)

return -1;

else

{

for(int i=arrayCapacity; i>index; i--)

{

list[i]=list[i-1];

}

list[index]=newVal;

return 0;

}

}

int main()

{

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

int index, newVal;

cout<<"Enter an index to insert element :";

cin>>index;

int numItems=0;

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

{

if(list[i]!=NULL)

numItems++;

}

int arrayCapacity=4;

cout<<"Enter your value to insert :";

cin>>newVal;

cout<<"\nArray before Inserting the new value.";

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

{

if(list[i]!=NULL)

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

}

int rs=insertInArray(list,index,&numItems,arrayCapacity,newVal);

if(rs<0)

cout<<"You can not insert the value array is full or wrong index you entered.";

else{

cout<<"\nArray after Inserting the new value.";

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

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

}

}

OUTPUT:

Enter an index to insert element :2 Enter your value to insert :14 Array before Inserting the new value.9 8 3 Array after Inserting the new value.9 8 14 3

Add a comment
Know the answer?
Add Answer to:
Must be C++ 11 Write a function named insertinArray that takes the following parameters: list: an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function named insertBeforeKey that takes the parameters as follows list as an char array,...

    Write a function named insertBeforeKey that takes the parameters as follows list as an char array, capacity for the capacity of the array, numItems has the number of items in the array, key is an element of the array before which newVal is to inserted. Assume key is always present in the array. 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...

  • Define a function named insert that takes an integer atom and a sorted list as parameters....

    Define a function named insert that takes an integer atom and a sorted list as parameters. The function should return a list with the integer in the correct position in the sorted list. For example: insert [3, (2 4 6 8)] = (2 3 4 6 8) insert 9, ()] = (9) insert[3, (2 3 4 5)] = (23 3 4 5) Note that in the case of that last example, it does not matter which 3 comes first in...

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • Write a function named findTarget that takes three parameters: - numbers, an array of integers -...

    Write a function named findTarget that takes three parameters: - numbers, an array of integers - size, an integer representing the size of array - target, a number The function returns true if the target is inside array and false otherwise

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and...

    help ASAP 3. Write a string C++ function named UnsignedPartialSum() that takes two string parameters and an int parameter. If both string parameters represent binary numbers and the int parameter is equal to a positive number less than or equal to the length of the longest string parameter, the function should return a binary string whose length is equal to two times the length of the maximum length of the two string parameters whose value is equal to the sum...

  • Write a function named stats, that takes an array and the number of elements in the...

    Write a function named stats, that takes an array and the number of elements in the array as arguments. It must compute and print the minimum value in the array, maximum value in the array and the average value of all the values inside the array. Your function MUST be named stats Your function has two parameters in the following order: an array of type double The number of elements in the array, type int Your function should NOT return...

  • (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of...

    (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of elements in the array, an integer (say, insertItem), and an integer (say, index). The function should insert insertItem in the array provided at the position specified by index. If index is out of range, output the following: Position of the item to be inserted is out of range or if the index is negative: Position of the item to be inserted must be nonnegative...

  • Write a function named numPerfect that takes as parameters: an array of integer scores between zero...

    Write a function named numPerfect that takes as parameters: an array of integer scores between zero and 100 (inclusive) the size of the array and returns the number of perfect scores in the array. Also write a main function that creates and initializes an array of ints (in the appropriate range), calls the function with that array, and prints out the return value.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT