needs to be in C++ coding sample run: Enter the number of elements you want to e
ID: 3607622 • Letter: N
Question
needs to be in C++ coding
sample run:
Enter the number of elements you want to enter (Max 100): 5
Enter 5 numbers
2.5 6 -7 10.7 0.8
1. Insert an element
2. Remove an element
3. Print an element
4. Sort the array
3
5. Exit
Enter your option: 1
Enter the number: 3.1
Enter the position: 2
Element Inserted.
1. Insert an element
2. Remove an element
3. Print an element
4. Sort the array
5. Exit
Enter your option: 3
The array is:
2.5 6 3.1 -7 10.7 0.8
1. Insert an element
2. Remove an element
3. Print an element
4. Sort the array
5. Exit
Enter your option: 4
1. Insert an element
2. Remove an element
3. Print an element
4. Sort the array
5. Exit
Enter your option: 3
The array is:
-7 0.8 2.5 3.1 6 10.7
1. Insert an element
2. Remove an element
3. Print an element
4. Sort the array
5. Exit
Enter your option: 2
Enter the element: 0.8
Element deleted.
1. Insert an element
2. Remove an element
3. Print an element
4. Sort the array
5. Exit
Enter your option: 3
The array is:
-7 2.5 3.1 6 10.7
4
1. Insert an element
2. Remove an element
3. Print an element
4. Sort the array
5. Exit
Enter your option: 5
Goodbye
these tasks repcatedly. For this problem, you can assume that the array size will never drop below 1 and will never rise above 99 Please make sure you conform to the following requirements: 1. Create a global constant integer called CAPACITY and set it to 100 (5 points 2. Use the class examples and exercises to write functions to initialize the array by reading values from the user, and to print the array. These functions take the array and its current size as 3. Write a function called insert that takes the array, a number, a position and the current size 4. Write a function called remove that takes the array, a number and the current size of the rite a function called sort that takes the array and its current size as parameters and sorts parameters. You can use the code in the examples. (5 points) of the array as parameters. Insert the number at the given position. (15 points) array, and removes the first instance of the number from the array. (15 points) the array in ascending order. You need to use insertion sort, which is given below. (15 points) 6. In the main function, have the user initialize the array. Then, write a command line menu with the following options: » Print » Insert Remove » Sort » Exit Call the appropriate functions when the user chooses a particular option. Print and error message and continue if the user enters a wrong option. (15 points) 7. Please make sure your code is appropriately commented. (5 points) Algorithm for insertion sort loop i from 1 to Length(A)Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int CAPACITY=100;
void init(double array[],int size)
{
for(int i=0;i<size;i++)//Input from User
cin>>array[i];
}
void print(double array[],int size)
{
for(int i=0;i<size;i++)//Printing array elements
cout<<array[i]<<" ";
cout<<' ';
}
void insert(double array[],double num,int pos,int &size)//size if passed as Call by reference
{
for(int i=size;i>=(pos);i--)
{
array[i+1]=array[i];//shifting elements to the right by one place for making space for new value(num) to be inserted at pos
}
array[pos]=num;//inserted num at pos(0-based indexing)
size++;//Incrementing array size
}
void remove(double array[],double num,int &size)
{
for(int i=0;i<size;i++)
{
if(array[i]==num)//Number Found
{
for(int j=i;j<size-1;j++)
{
array[j]=array[j+1];
}
size--;//Decrementing array size
break;
}
}
}
/* Function to sort an array using insertion sort*/
void insertionSort(double arr[], int n)
{
int i, j;
double key;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
int main()
{
double num;
int pos;
int size;//Size of array
cout<<"Enter the number of element you want to enter(MAX 100) :";
cin>>size;
double array[size];
cout<<"Enter "<<size<<" numbers ";
init(array,size);
int ch;
do
{
cout<<"1. Insert an element 2.Remove an element 3. Print an element 4. Sort the array 5. Exit Enter your option :";
cin>>ch;
if(ch==1)
{
cout<<"Enter the number :";
cin>>num;
cout<<"Enter the position : ";
cin>>pos;
insert(array,num,pos,size);
cout<<"Element inserted ";
}
else if(ch==2)
{
cout<<"Enter the elemnet : ";cin>>num;
remove(array,num,size);
cout<<"Element Deleted ";
}
else if(ch==3)
{
cout<<"The array is: ";
print(array,size);
}
else if(ch==4)
{
insertionSort(array,size);
}
}while(ch!=5);
cout<<"Good Bye ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.