Question

Using C++ Insert elements into a partially filled array. a) Create a partially filled array of...

Using C++

Insert elements into a partially filled array.

a) Create a partially filled array of CAPACITY 100.

b) Initialize the array with values 10,20,30,40,50,60,70,80,90,100

c) Create a print function for the array.

d) Create an insert function which inserts an integer into the array in sorted position.

e) Insert the numbers 5, 150 and 55.

f) Print the array before and after each insertion.

Output Example

10 20 30 40 50 60 70 80 90 100

5 10 20 30 40 50 60 70 80 90 100

5 10 20 30 40 50 60 70 80 90 100 150

5 10 20 30 40 50 55 60 70 80 90 100 150

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

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

#include<iostream>

using namespace std;

#define CAPACITY 100

void print_array(int arr[], int n);

void insert_array(int arr[], int &n, int val);

int main(){

int arr[CAPACITY] = {10,20,30,40,50,60,70,80,90,100};

int n = 10;

print_array(arr, n);

insert_array(arr, n, 5);

print_array(arr, n);

insert_array(arr, n, 150);

print_array(arr, n);

insert_array(arr, n, 55);

print_array(arr, n);

return 0;

}

void print_array(int arr[], int n){

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

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

}

cout<<endl;

}

void insert_array(int arr[], int &n, int val){

int pos = -1;

int i=0;

while(arr[i]<val && i<n){

pos = i;

i++;

}

for(i=n; i>pos; i--){

arr[i] = arr[i-1];

}

arr[pos+1] = val;

n++;

}

Add a comment
Know the answer?
Add Answer to:
Using C++ Insert elements into a partially filled array. a) Create a partially filled array of...
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
  • In java please :-) 12 values have been input into array yearlyValues. Output all 12 elements,...

    In java please :-) 12 values have been input into array yearlyValues. Output all 12 elements, with 4 per line. If the elements are 10 20 30 40 50 60 70 80 90 100 110 120, the output is: 10 20 30 40 50 60 70 80 90 100 110 120 Hints: • Use a for loop with increment i += 4, rather than ++i. • Inside the for loop, just print all four elements using four print statements. An...

  • using C language Create an array of doubles with 5 elements. In the array prompt the...

    using C language Create an array of doubles with 5 elements. In the array prompt the user to enter 5 temperature values (in degree Fahrenheit). Do this within main. Create a user defined function called convert2Cels. This function will not return any values to main with a return statement. It should have parameters that include the temperature array and the number of elements. The function should convert the values for Fahrenheit to Celsius and save them back into the same...

  • The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random...

    The ExceptionLab class provided: – Creates an array of 100 elements and fills it with random numbers from 1 to 100. – It asks the user for an index value between 0 and 99. – Prints the element at that position. – If a number > 99 is entered by the user, the class will abort with an ArrayIndexOutOfBoundsException • Modify the ExceptionLab: – Add a try-catch clause which intercepts the ArrayIndexOutOfBounds and prints the message: Index value cannot be...

  • 11. In the 2-3 tree given below (i.e., NOT a 2-3-4 tree), execute insert(28), insert(99), and...

    11. In the 2-3 tree given below (i.e., NOT a 2-3-4 tree), execute insert(28), insert(99), and insert(58), in that order, making sure to rebalance after each insertion. Draw the resulting 2-3 tree after executing these operations. 45 20 70 30 60 80 90 2(4(10 11) (25) (40) (50 55) (65) (71 75)(85) (92 96

  • PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw...

    PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...

  • In C Write a couple of functions to process arrays. Note that from the description of...

    In C Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an int array and size, and...

  • SQL Create an access database named student11. Create a table named student12 and insert the following...

    SQL Create an access database named student11. Create a table named student12 and insert the following records in it. ID          StudentName               Grade 1            John                                 90 2            Jim                                    70 3            Larry                               85 4            Sally                                 20 5            Sam                                   60 6            Sandra                            100 7            Runa                                 55 8            Simran                            35 9            Seema                              65 10         Ramu                               75 Create a query to find students whose grade are more than or equal to 80.

  • ==> Please In C Language <== Insert a value in a sorted array: Given an integer...

    ==> Please In C Language <== Insert a value in a sorted array: Given an integer array a[9] = { 2, 3, 5, 7, 11, 13, 17, 19 }, read in an integer k from the screen and insert k into the array so the new array remains sorted. Note that array a has size 9, but there are only 8 initial values. This ensures that no value will be lost after the insertion. For example, when k=12 , the...

  • Identify which of these four mass spectra indicate the presence of sulfur, chlorine, bromine, iondine, nitrogen....

    Identify which of these four mass spectra indicate the presence of sulfur, chlorine, bromine, iondine, nitrogen. suggest a molecular formula for each. 2otten breaks sh ass spec er be mass PROBLEM 12-7 Identify which of these four mass spectra indicate the presence of sulfur, chlorine bromine, iodine, or nitrogen. Suggest a molecular formula for each. AA gIve the lonica 100 77 80 156 158 (a) 60 40 20 Fragn 10 20 30 40 50 60 70 80 100 110 120...

  • Create an array named hotel with 100 elements in it. Place random values between 50 and...

    Create an array named hotel with 100 elements in it. Place random values between 50 and 150 in every item within the array.   Create a function (or do it in the main program) that counts how many values in the array are over 75 and display that number to the screen. C++ program thanks.

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