Finish the implementation of the following program Keep two

Finish the implementation of the following program. Keep two things in mind:

Run the demo to get idea how the program should run

Add menu function accordingly

The student should implement and test the following functions:

void fill_array(int arr[], int size);
// precondition: The arr has actual size that is greater than or equal to size
// postcondition: arr[0], ..., arr[size-1] is filled from keyboard

void print_array(int arr[], int size);
// precondition: The arr has actual size that is greater than or equal to size
// postcondition: arr[0], ..., arr[size-1] is printed to screeen with 5 elements per line

int linear_search(int arr[], int size, int key);
// precondition: arr has given size
// postcondition: The index of first occurrence of key in arr is returned. If the key cannot be found in arr, -1 is returned

void select_sort(int arr[], int size);
// precondition: arr has given size
// postcondition: the elements in arr are rearranged from least to largest

void insert_sort(int arr[], int size);
// precondition: arr has given size
// postcondition: the elements in arr are rearranged from least to largest

void bubble_sort(int arr[], int size);
// precondition: arr has given size
// postcondition: the elements in arr are rearranged from least to largest

Of course, a menu function is needed. (you should know how to write the menu after run the demo). The main function will look like following


int main() {
    int choice;
    int a[9];
    do{
        menu();
        cout << \"Enter your choice: \";
        cin >> choice;
        switch(choice)
        {
            case 1:
            {
                fill_array(a, 9);
                cout << \"Enter the key you want to search: \";
                int key;
                cin >> key;
                int index = linear_search(a, 9, key);
                if(index == -1)
                    cout << \"The key \" << key << \" is not in array\ \";
                else
                    cout << \"The key \" << key << \" is #\" << (index + 1) << \" element in array\ \";
                break;
            }
            case 2:
            {
                fill_array(a, 9);
                select_sort(a, 9);
                cout << \"After sort, the array is:\ \";
                print_array(a, 9);
                break;
            }
            case 3:
            {
                fill_array(a, 9);
                insert_sort(a, 9);
                cout << \"After sort, the array is:\ \";
                print_array(a, 9);
                break;
            }
            case 4:
            {
                fill_array(a, 9);
                bubble_sort(a, 9);
                cout << \"After sort, the array is:\ \";
                print_array(a, 9);
                break;
            }
            case 5:
            {
                cout << \"Thank you for using the array functions\ \";
                break;
            }
            default:
            {
                cout << \"Wrong choice. Please choose from menu: \";
                break;
            }
        }
    }while(choice != 5);
   
    return 0;
}

Solution

//code has been tested on gcc compiler

//start of code
#include <iostream>

using namespace std;

void fill_array(int arr[], int size){ //function to fill elements in array
cout<<\"enter elements of array :\"<<endl;
for(int i=0;i<size;i++)
cin>>arr[i];
  
}
// precondition: The arr has actual size that is greater than or equal to size
// postcondition: arr[0], ..., arr[size-1] is filled from keyboard

void print_array(int arr[], int size){ //function to print elements of array
for(int i=0;i<size;i++)
cout<<arr[i];
}
// precondition: The arr has actual size that is greater than or equal to size
// postcondition: arr[0], ..., arr[size-1] is printed to screeen with 5 elements per line

int linear_search(int arr[], int size, int key){ //function to perform linear search
for(int i=0;i<size;i++)
{
if(arr[i]==key)
return i;
}
return -1;
}
// precondition: arr has given size
// postcondition: The index of first occurrence of key in arr is returned. If the key cannot be found in arr, -1 is returned

void select_sort(int arr[], int size){ //function to perform selection sort
int min,index;
int temp;
for(int i=0;i<size-1;i++)
{
min=arr[i];
index=i;
for(int j=i+1;j<size;j++)
{
if(min>arr[j])
{
min=arr[j];
index=j;
}
}

temp=arr[i];
arr[i]=arr[index];
arr[index]=temp;
}

}
// precondition: arr has given size
// postcondition: the elements in arr are rearranged from least to largest

void insert_sort(int arr[], int size){ //function to perform insertion sort
int temp;
for (int i = 1; i < size; i++)
{
for (int j = i; j >= 1; j--)
{
if (arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
else
break;
}
}
}
// precondition: arr has given size
// postcondition: the elements in arr are rearranged from least to largest

void bubble_sort(int arr[], int size){ //function to perform bubble sort
int temp;
for(int i=1;i<size;++i)
{
for(int j=0;j<(size-i);++j)
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
  
}
// precondition: arr has given size
// postcondition: the elements in arr are rearranged from least to largest
//Of course, a menu function is needed. (you should know how to write the menu after run the demo). The main function will look like following
void menu(){ //menu function
cout<<\"Hi User!!,We are presenting following list of array functions :\"<<endl;
cout<<\"Choice number\"<<\" \"<<\"Array Function\"<<endl;
cout<<\"1 \"<<\" \"<<\"Linear Search\"<<endl;
cout<<\"2 \"<<\" \"<<\"Selection Sort\"<<endl;
cout<<\"3 \"<<\" \"<<\"Insertion Sort\"<<endl;
cout<<\"4 \"<<\" \"<<\"Bubble Sort\"<<endl;
cout<<\"5 \"<<\" \"<<\"I dont want to use any function\"<<endl;
}

int main() {
int choice;
int a[9];
do{
menu();
cout << \"Enter your choice: \";
cin >> choice;
switch(choice)
{
case 1:
{
fill_array(a, 9);
cout << \"Enter the key you want to search: \";
int key;
cin >> key;
int index = linear_search(a, 9, key);
if(index == -1)
cout << \"The key \" << key << \" is not in array\ \";
else
cout << \"The key \" << key << \" is #\" << (index + 1) << \" element in array\ \";
break;
}
case 2:
{
fill_array(a, 9);
select_sort(a, 9);
cout << \"After sort, the array is:\ \";
print_array(a, 9);
break;
}
case 3:
{
fill_array(a, 9);
insert_sort(a, 9);
cout << \"After sort, the array is:\ \";
print_array(a, 9);
break;
}
case 4:
{
fill_array(a, 9);
bubble_sort(a, 9);
cout << \"After sort, the array is:\ \";
print_array(a, 9);
break;
}
case 5:
{
cout << \"Thank you for using the array functions\ \";
break;
}
default:
{
cout << \"Wrong choice. Please choose from menu: \";
break;
}
}
}while(choice != 5);

return 0;
}

//end of code

*********OUTPUT*********
Hi User!!,We are presenting following list of array functions :   
Choice number Array Function
1 Linear Search   
2 Selection Sort
3 Insertion Sort
4 Bubble Sort
5 I dont want to use any function   
Enter your choice: 1
enter elements of array :
1   
5   
7   
4   
8   
9   
3   
4   
6
Enter the key you want to search: 4
The key 4 is #4 element in array   

*********OUTPUT*********

In case of any doubt,please let me know,Thanks.

Finish the implementation of the following program. Keep two things in mind: Run the demo to get idea how the program should run Add menu function accordingly T
Finish the implementation of the following program. Keep two things in mind: Run the demo to get idea how the program should run Add menu function accordingly T
Finish the implementation of the following program. Keep two things in mind: Run the demo to get idea how the program should run Add menu function accordingly T
Finish the implementation of the following program. Keep two things in mind: Run the demo to get idea how the program should run Add menu function accordingly T
Finish the implementation of the following program. Keep two things in mind: Run the demo to get idea how the program should run Add menu function accordingly T

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site