Question

Write a static method insertIntoArray which inserts a value into a partially full array. The method...

Write a static method insertIntoArray which inserts a value into a partially full array. The method should take four parameters: an array of integers, a count of how many integer items are stored in the array, a given index where an element should be inserted, and a new integer item to insert. Assume that the integer items are stored at indexes 0 through count-1 (or no items stored if count is 0). If there is not room for one more item or if the index is not between 0 and count (inclusive), return without modifying the array. Otherwise, move the integer items starting at the index one index forward, and place the new item at the given index. You may assume that all of the elements starting at count have the value of 0, but stored elements may also have the value 0. The method should not return any value. (Use int method)

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

#include<iostream>

#include<fstream>

using namespace std;

static void insertIntoArray(int myArray[] , int &n, int index,int key){

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

myArray[i] = myArray[i-1];

myArray[index] = key;

++n;

return;

}

int main(int argc, char ** argv)

{

int val, size = 100,n = 9 , key = 33, index = 3;;

int arr[100]= {2,4,5,6,10,22,11,55,66};

cout<<"Before Insert"<<endl;

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

cout<<arr[i] <<" ";

insertIntoArray(arr,n,index,key);

cout<<"\nAfter Insert"<<endl;

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

cout<<arr[i] <<" ";

return 0;

}

===============================================================
See OUTPUT


Thanks, As language was not mentioned I did in C++. PLEASE COMMENT if you need in any other language


JAVA




import java.util.Scanner;

public class ArrayInsert {

static int insertIntoArray(int myArray[], int n, int index, int key) {

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

myArray[i] = myArray[i - 1];

myArray[index] = key;

++n;

return n;

}

public static void main(String args[]) {

int val, size = 100,n = 9 , key = 33, index = 3;;

int arr[]= new int[]{2,4,5,6,10,22,11,55,66,10000};

System.out.println("Before Insert");

for(int i=0 ;i<n-1 ; ++i)

System.out.print(arr[i] + " ");

n = insertIntoArray(arr,n,index,key);

System.out.println("\nAfter Insert");

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

System.out.print(arr[i] + " ");

}

}

====================

Add a comment
Know the answer?
Add Answer to:
Write a static method insertIntoArray which inserts a value into a partially full array. The method...
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
  • (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...

  • Given the Interface Code write a java class that implements this interface and show the working...

    Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...

  • complete in C++ code Lists Many programming languages have some kind list data structure, which allows...

    complete in C++ code Lists Many programming languages have some kind list data structure, which allows for insertion and deletion of elements, essentially an array that changes size. C++ has vectors, Java has the ArrayList, Python has lists (which is what they're called generically anyway). But how do they really work? All of these actually use arrays, which naturally have immutable lengths. Your Task Your task is to write a function, insert, which inserts a new item into an existing...

  • USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test...

    USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...

  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int...

    Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int virtualArrayLength; // the number of elements in the dynamic array // Throws an IndexOutOfBoundsException if i is not a valid index // for adding to the dynamic array, otherwise inserts s at index i. // Elements can be added from index 0 to this.size(). public void add(int i, String s) { // If there is no room for s in data, create a new...

  • JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{    private static final...

    JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{    private static final int DEFAULT_CAPACITY = 2;   //default initial capacity / minimum capacity    private T[] data;   //underlying array    // ADD MORE PRIVATE MEMBERS HERE IF NEEDED!       @SuppressWarnings("unchecked")    public SmartArray(){        //constructor        //initial capacity of the array should be DEFAULT_CAPACITY    }    @SuppressWarnings("unchecked")    public SmartArray(int initialCapacity){        // constructor        // set the initial capacity of...

  • C# public int IndexOf(T element) { for (var i = 0; i < Count; i++) {...

    C# public int IndexOf(T element) { for (var i = 0; i < Count; i++) { if (data[i].Equals(element)) return i; } return -1; } You must complete the Vector<T> implementation by providing the following functionality: void Insert(int index, T item) Inserts a new element into the data structure at the specified index. This will involve four parts (Note a part may be more than a single line of code): o If Count already equals Capacity (eg the currently allocated space...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

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