1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
lorasvet [3.4K]
3 years ago
12

Write a function, insertAt, that takes four parameters: an array of integers, the number of elements in the array, an integer (s

ay, 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 (Note that index must be between 0 and the number of elements in the array; that is, 0 <= index < the number of elements in the array.) Assume that the array is unsorted.
Chemistry
1 answer:
zimovet [89]3 years ago
6 0

Answer:

Implemented the program using C++ below

Explanation:

#include <iostream>

#include <iomanip>

using namespace std;

void printIt (int numbers[],int length);

int removeAt (int numbers[], int length, int index);

void insertAt (int numbers[], int length, int insertItem, int index);

int main()

{

int numbers[] = {4,23,65,34,82,37,12,17,24,36,82,51};

int length;

int index;

int insertItem;

cout<<"Removing an item from the list..."<<endl;

cout<<endl;

printIt(numbers,12);

removeAt(numbers,12,index);

insertAt(numbers,12,insertItem,index);

system ("PAUSE");

return 0;

}

void printIt (int numbers[],int length)

{

cout<<"The current array..."<<endl;

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

{

cout<<numbers[i]<<" ";

}

cout<<endl;

}

int removeAt (int numbers[], int length, int index)

{

int item;

cout<<endl;

cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;

cout<<"Enter the position of the item to be removed."<<endl;

cout<<"Enter 0 for the first item and so on: ";

cin>>item;

if (item > length)

{

cout<<endl;

cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;

cout<<endl;

cout<<"The current array..."<<endl;

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

{

cout<<numbers[i]<<" ";

}

cout<<endl;

cout<<endl;

cout<<"!!!! Index out of Range !!!!"<<endl;

cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;

cout<<"You entered position "<<item<<", which is OUT OF RANGE."<<endl;

cout<<"Enter the position of the item to be removed."<<endl;

cout<<"Enter 0 for the first item and so on: ";

cin>>item;

cout<<endl;

cout<<"After removing the item at position "<<item<<", array is..."<<endl;

cout<<endl;

cout<<"The current array..."<<endl;

}

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

{

if (i != item)

{

cout<<numbers[i]<<" ";

}

}

cout<<endl;

cout<<endl;

cout<<"************************************************************";

cout<<endl;

cout<<endl;

}

void insertAt (int numbers[], int length, int insertItem, int index)

{

int item;

cout<<"Inserting an item in the list..."<<endl;

cout<<endl;

cout<<"The current array..."<<endl;

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

{

if (i != item)

{

cout<<numbers[i]<<" ";

}

}

cout<<endl;

cout<<"There are 10 items(s) in the list (position 0 through 11)"<<endl;

cout<<"Enter item to be inserted and its position"<<endl;

cout<<"Position of the first element is 0,"<<endl;

cout<<"so if you want the #5 at the front type in: "<<endl;

cout<<"5 (space) 0 "<<endl;

cin>>insertItem;

cin>>index;

if (index > length)

{

cout<<endl;

cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;

cout<<endl;

cout<<"The current array..."<<endl;

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

{

if (i != item)

{

cout<<numbers[i]<<" ";

}

}

cout<<endl;

cout<<endl;

cout<<"!!!! Index out of Range !!!!"<<endl;

cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;

cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl;

cout<<endl;

cout<<"Enter item to be inserted and its position"<<endl;

cout<<"Position of the first element is 0,"<<endl;

cout<<"so if you want the #5 at the front type in: "<<endl;

cout<<"5 (space) 0 "<<endl;

cin>>insertItem;

cin>>index;

}

cout<<endl;

cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl;

cout<<endl;

cout<<"The current array..."<<endl;

numbers[insertItem-1] = insertItem;

cout<<numbers[insertItem-1]<<" ";

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

{

if (i != item)

{

cout<<numbers[i]<<" ";

}

}

cout<<endl;

}

You might be interested in
Why do you need to know the balanced chemical equation before calculating the amount of a compound needed to neutralize an acid
I am Lyosha [343]

Answer: To determine the mole ratio

Explanation:

A balanced chemical equation is needed ,because it helps to determine the mole ratio of acid and base.

The mole ratio ensures that the number of moles of H+ provided by the acid is equal to number of moles of OH- ion provided by the base, For neutralization to occur, this must be the case.

5 0
4 years ago
What is the law of inertia?
Pachacha [2.7K]
B is the law of inertia
3 0
3 years ago
Read 2 more answers
A flask holds 3.01 x 10^23 molecules of carbon dioxide. The container is holding
Andrew [12]
B) 0.50 moles of carbon dioxide.

4 0
4 years ago
What are the three main radioactive elements
DIA [1.3K]
Three main radioactive elements are:

Uranium

Thorium

Radium
6 0
3 years ago
A sample of helium gas occupies 355ml at 23°c. if the container the he is in is expanded to 1.50 l at constant pressure, what is
ss7ja [257]

Answer: The final temperature would be 1250.7 K.

Explanation: We are given a sample of helium gas, the initial conditions are:

V_{initial}=355mL=0.355L  (Conversion factor: 1L = 1000 mL)

T_{initial}=23\°C=296K (Conversion Factor: 1° C = 273 K)

The same gas is expanded at constant pressure, so the final conditions are:

V_{initial}=1.50L

T_{initial}=?K

To calculate the final temperature, we use Charles law, which states that the volume of the gas is directly proportional to the temperature at constant pressure.

V\propto T

\frac{V_{initial}}{T_{initial}}=\frac{V_{final}}{T_{final}}

Putting the values, in above equation, we get:

\frac{0.355L}{296K}=\frac{1.50L}{T_{final}}

T_f=1250.7K

5 0
3 years ago
Read 2 more answers
Other questions:
  • Enriched weapons-grade uranium consists of 80% uranium-235 (235.044 amu) and 20% uranium-238 (238.051 amu). what is the average
    5·1 answer
  • Which is not among the current strategies used to prevent lead poisoning?
    14·2 answers
  • Example of a metal, nonmetal, and metalloid<br> WILL GET BRAINLIEST!!
    13·2 answers
  • (PLS HELP will mark brainliest!) Compare and contrast the alkali metals and the alkaline earth metals. Discuss their physical an
    11·1 answer
  • 2. Calculate the mass of 5.35 mole of H2O2.
    13·1 answer
  • What should be the very last function performed in the lab before you leave?
    10·1 answer
  • How can line spectra be used to evaluate what elements are in an unknown sample?
    7·1 answer
  • In the clinical laboratory, testing to identify specific porphyrins is performed using Group of answer choices Fluorometric emis
    9·1 answer
  • The motion of a car on a position-time graph is represented with a horizontal line. What does this indicate about the car's moti
    12·1 answer
  • Can someone help me... I need to talk to someone
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!