Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please show all the steps as detail as possible.... I need help with this.... **

ID: 3802122 • Letter: P

Question

Please show all the steps as detail as possible.... I need help with this....

*****************************************************************

Need Help on C++ Programming. Can you show me and teach me how to make this programming.

*Make sure the programming is working and do not turn in a handwriting.

*Do not separate the file, use main.cpp that make me understand how to do this programming. Use "iostream"

*****************************************************************

Part 1 - LIST

Create an unsorted LIST class. Each list should be able to store 100 names.

#include <iostream>

#include <iostream>

using namespace std;

class TopSearchSort

{

int arr[100];

int n;

public:

//Function return number of elements in array

int getn()

{

return n;

}

void input()

{

//int n;

cout<<"Enter the number of elements :";

cin>>n;

  

cout<<" Enter the elements: ";

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

cin>>arr[i];

}

void output()

{

cout<<"The array elements are : ";

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

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

}

void delete_element()

{

int d,start=0;

cout<<"Enter the number to delete : ";

cin>>d;

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

{

if(arr[i]==d)

{

start=i;

break;

}

}

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

arr[i]=arr[i+1];

n=n-1;

}

  

/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */

// void merge(int arr[], int l, int m, int r)

void merge(int l, int m, int r)

{

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

/* create temp arrays */

int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */

for (i = 0; i < n1; i++)

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1+ j];

/* Merge the temp arrays back into arr[l..r]*/

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2)

{

if (L[i] <= R[j])

{

arr[k] = L[i];

i++;

}

else

{

arr[k] = R[j];

j++;

}

k++;

}

/* Copy the remaining elements of L[], if there are any */

while (i < n1)

{

arr[k] = L[i];

i++;

k++;

}

/* Copy the remaining elements of R[], if there are any */

while (j < n2)

{

arr[k] = R[j];

j++;

k++;

}

}

  

void mergeSort( int l, int r)

{

if (l < r)

{

int m = l+(r-l)/2; //Same as (l+r)/2 but avoids overflow for large l & h

mergeSort(l, m);

mergeSort(m+1, r);

// merge(arr, l, m, r);

merge(l,m,r);

}

}

void search()

{

int s;

cout<<"Enter the number to search :";

cin>>s;

mergeSort(0,getn()-1);

int start=0;

int end=n;

int mid=-1;

while(start<=end)

{

mid=(start+end)/2;

if(arr[mid]==s)

break;

if(arr[mid]<s)

{

start=mid+1;

}

else

end=mid-1;

}

cout<<"The element is present at location (-1 means element is not present) :"<<mid<<" ";

  

}

};

int main()

{

TopSearchSort s;

while(1)

{

cout<<" Enter your choice 1.Enter elements 2.Print array 3.Delete element 4.Search Element 5.Sort array ";

int c;

cin>>c;

if(c==1)

s.input();

else if(c==2)

s.output();

else if(c==3)

s.delete_element();

else if(c==4)

s.search();

else if(c==5)

s.mergeSort(0,s.getn()-1);

else

{cout<<"wrong choice ";

break;

}

}

return 0;

}

Explanation / Answer

There was couple of bugs in your program during insertion and searching which I fixed.I modified your program to support two more functionality.

Based on the your choice you have the following operation

Enter your choice
1.Insert Elements
2.Print Array
3.Delete Element
4.Search Element
5.Sort Array
6.Clear Array
7.Exit


Inserion:

            In this step ,user asked to input no of elements he wants to insert followed by the elements.Whenenver user wants to insert elements, the new elements get appened to the old array, It will not overwrite the array which was happening in youyr case.

Print Array:

                Print all the elements of the array.

Delete Element :

               delete the given element in the array.First we find the position of that element in that array.Then we shift all the elements after the position by one towards left.

Search Element:

          We first sort the array using merge sort and the apply the binary search to find the element, as binary search can only be applied to sorted array.I binary serach, we find the middle element, and comapres with our element, if it matches we are done.If given number is greater than middle element then serach in mid+1 to high .If given number is smaller than middle element then serach in low to mid-1.

Sort Array:

             We use merge sort to sort the given array.It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves.

Clear Array:

           It deletes all the elements of tha array.

Exit:

       exit from the program

Below is the list.cpp

Compile :c++ list.cpp -o list -std=c++11

execute: ./list

#include <iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

class TopSearchSort

{

int arr[100];

int n;

public:

//Function return number of elements in array

TopSearchSort()
{
n=0;
}

int getn()

{

return n;

}

void clear()
{

for(int i=0;i<n;i++)
    arr[i]=-1;
n=0;
}

void input()

{

int no;

cout<<"Enter the number of elements :";

cin>>no;

cout<<" Enter the elements: ";

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

cin>>arr[n+i];

n=n+no;
}

void output()

{

if(n==0)
cout<<" Array is empty. ";
else
{
cout<<"The array elements are : ";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}
}

void delete_element()

{

int d,start=0;

cout<<"Enter the number to delete : ";

cin>>d;

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

{

if(arr[i]==d)

{

start=i;

break;

}

}

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

arr[i]=arr[i+1];

n=n-1;

}

/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */

// void merge(int arr[], int l, int m, int r)

void merge(int l, int m, int r)

{

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

/* create temp arrays */

int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */

for (i = 0; i < n1; i++)

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1+ j];

/* Merge the temp arrays back into arr[l..r]*/

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2)

{

if (L[i] <= R[j])

{

arr[k] = L[i];

i++;

}

else

{

arr[k] = R[j];

j++;

}

k++;

}

/* Copy the remaining elements of L[], if there are any */

while (i < n1)

{

arr[k] = L[i];

i++;

k++;

}

/* Copy the remaining elements of R[], if there are any */

while (j < n2)

{

arr[k] = R[j];

j++;

k++;

}

}

void mergeSort( int l, int r)

{

if (l < r)

{

int m = l+(r-l)/2; //Same as (l+r)/2 but avoids overflow for large l & h

mergeSort(l, m);

mergeSort(m+1, r);

// merge(arr, l, m, r);

merge(l,m,r);

}

}

void search()

{

int s;

cout<<"Enter the number to search :";

cin>>s;

mergeSort(0,getn()-1);

int start=0;

int end=n;

int mid=-1;

while(start<=end)

{

mid=(start+end)/2;

if(arr[mid]==s)

break;

if(arr[mid]<s)

{

start=mid+1;

}

else

end=mid-1;

}

if(start> end)
cout<<"The Element is not present in the given array ";
else
cout<<"The element is present at location :"<<mid<<" ";

}

void exit_program()
{
cout<<"Exting from the program...";
exit(1);
}

};

int main()

{

TopSearchSort s;

while(1)

{

cout<<" Enter your choice 1.Insert Elements 2.Print Array 3.Delete Element 4.Search Element 5.Sort Array 6.Clear Array 7.Exit ";

int c;
cout<<"Choice: ";
cin>>c;

switch(c)
{
case 1:
         s.input();
         break;
case 2:
         s.output();
         break;
case 3:
         s.delete_element();
         break;
case 4:
         s.search();
         break;
case 5:
         s.mergeSort(0,s.getn()-1);
         break;
case 6:
         s.clear();
         break;

case 7:
         s.exit_program();
         break;
default:
         cout<<" Entered wrong choice.Please try again...";
         break;
}

}

return 0;

}

           

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote