Final: Write a program with functions: IN C++ 1-Show a menu in a loop with these
ID: 3732804 • Letter: F
Question
Final: Write a program with functions: IN C++
1-Show a menu in a loop with these following options to get the elements of an array:
a.Option 1: To enter manually: Ask user to enter the elements of an array. User can enter as many as they want until they enter “Q” or “q”to end.
i.Store the created array in a variable
ii.Print the created array with the name of the variable.
b.Option 2: To read from a file: You program should read a file to get the elements of the array. Prepare a text file with some integers.
i.Store the created array in a variable
ii.Print the created array with the name of the variable.
c.Option 3: To exit of this menu
2-Show a menu in a loop with these following options:
a.Option 1: Bubble sort: Choose this one to sort the first array (which entered manually). Print the array after sorting.
b.Option 2: Selection sort: Choose this one to sort the first array (which read from a file). Print the array after sorting.
c.Option 3: Exit of this menu
3-Show a menu in a loop with these followingoptions:
a.Option 1: Linear search: Ask user to enter an array (enter the first array) and a value to search in the array. Show if it found the value or not. If found how many of the value are in the array.
b.Option 2: Binary search: Ask user to enter an array (enter the secondarray) and a value to search in the array. Show if it found the value or not. If found how many of the value are in the array.
c.Option 3: Exit of this menu4-Write the sorted arrays in a file. (Ask user to enter the name of the file)
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#define MAX_SIZE 100
class my_class
{
public:
int array1[MAX_SIZE],array2[MAX_SIZE];
int linear_search(int size,int key)
{
int count=0;
for(int i=0;i<size;i++)
{
if(key==array1[i])
{
count++;
}
}
return count;
}
int binary_search(int l, int r, int key)
{
int count=0;
if (r >= l)
{
int mid = l + (r - l)/2;
if (array2[mid] == key)
{
return mid;
}
if (array2[mid] > key)
{
return binary_search(l, mid-1, key);
}
return binary_search(mid+1, r, key);
}
return -1;
}
void bubble_sort(int n)
{
int temp;
for (long i = 0; i < n-1; i++)
{
for (long j = 0; j < n-i-1; j++)
{
if (array1[j] > array1[j+1])
{
temp=array1[j];
array1[j]=array1[j+1];
array1[j+1]=temp;
}
}
}
}
void selection_sort(int n)
{
int i, j, min_idx,temp;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
{
if (array2[j] < array2[min_idx])
min_idx = j;
}
temp=array2[min_idx];
array2[min_idx]=array2[i];
array2[i]=temp;
}
}
};
int main()
{
int ch=-1;
int array_1_count=0,array_2_count=0;
my_class obj;
int i=-1,count=-1;
std::ifstream infile("./myfile.txt");
char *temp;
char more='y';
while(ch!=3)
{
cout<<" 1. Enter array elements Manually."<<endl;
cout<<" 2. Read array elements from File."<<endl;
cout<<" 3. Exit."<<endl;
cout<<" Enter your choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
more='y';
i=-1;
count=-1;
while(tolower(more)!='q')
{
cout<<" Enter the Array-1 Elements-"<<count+2<<":";
cin>>obj.array1[++count];
array_1_count++;
cout<<" Do You Want To Add More(Q/q for No):";
cin>>more;
}
cout<<" Your Data of Array-1"<<endl;
while(i<count)
{
cout<<" Element-"<<i+1<<":"<<obj.array1[++i]<<" ";
}
break;
case 2:
count=-1;
while(infile>>obj.array2[++count])
{
array_2_count++;
}
i=-1;
cout<<" Your Data of Array-2";
while(i<count-1)
{
cout<<" Element-"<<i+1<<":"<<obj.array2[++i]<<" ";
}
break;
default:
if(ch!=3)
{
cout<<"Invalid Choice...Please enter the provided choice number."<<endl;
}
else
{
cout<<" Exiting..."<<endl;
}
}
}
std::ofstream outfile("./myfile.txt");
ch=-1;
while(ch!=3)
{
cout<<" 1. Enter array elements Manually."<<endl;
cout<<" 2. Read array elements from File."<<endl;
cout<<" 3. Exit."<<endl;
cout<<" Enter your choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
if(array_1_count!=0)
{
obj.bubble_sort(array_1_count);
}
else
{
cout<<" No data In Array-1";
}
i=-1;
cout<<" Your Data of Array-1";
while(i<array_1_count-1)
{
cout<<" Element-"<<i+1<<":"<<obj.array1[++i]<<" ";
}
break;
case 2:
if(array_2_count!=0)
{
obj.selection_sort(array_2_count);
}
else
{
cout<<" No data In Array-2"<<endl;
}
i=-1;
cout<<" Your Data of Array-2";
while(i<array_2_count-1)
{
cout<<" Element-"<<i+1<<":"<<obj.array2[++i]<<" ";
}
break;
default:
if(ch!=3)
{
cout<<"Invalid Choice...Please enter the provided choice number."<<endl;
}
else
{
cout<<" Exiting..."<<endl;
}
}
}
ch=-1;
int key;
while(ch!=3)
{
cout<<" 1. Sort Array-1"<<endl;
cout<<" 2. Sort Array-2."<<endl;
cout<<" 3. Exit."<<endl;
cout<<" Enter your choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
if(array_1_count!=0)
{
cout<<" Enter Key To Serach In Array-1:";
cin>>key;
cout<<" "<<key<<" is occured "<<obj.linear_search(array_1_count,key)<<" times in Array-1";
}
else
{
cout<<" No data In Array-1";
}
break;
case 2:
if(array_2_count!=0)
{
cout<<" Enter Key To Serach In Array-2:";
cin>>key;
cout<<" "<<key<<" is occured at "<<obj.binary_search(0,array_1_count,key)<<" index in Array-2";
}
else
{
cout<<" No data In Array-2"<<endl;
}
break;
default:
if(ch!=3)
{
cout<<"Invalid Choice...Please enter the provided choice number."<<endl;
}
else
{
cout<<" Exiting..."<<endl;
}
}
}
ch=-1;
i=0;
while(ch!=2)
{
cout<<" 1. Store Data In File."<<endl;
cout<<" 2. Exit."<<endl;
cout<<" Enter your choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<" Storing Data... "<<endl<<"Done"<<endl;
while(i<array_2_count)
{
outfile<<obj.array2[i]<<endl;
i++;
}
break;
default:
if(ch!=3)
{
cout<<"Invalid Choice...Please enter the provided choice number."<<endl;
}
else
{
cout<<" Exiting..."<<endl;
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.