/*Final: Write a program with functions: C++ Write a program that shows a menu i
ID: 3737041 • Letter: #
Question
/*Final: Write a program with functions: C++
Write a program that shows a menu in a loop with these following options to get the elements of an array of integers:
Item 1: To enter manually: Ask the user for the size of the array and then ask to enter the elements of it.
1-1- Store the created array in a variable
1-2- Print the created array
1-3- Use Bubble Sort to sort this array.
1-4- Print the array after sorting and inform the user that you use Bubble Sort to sort the array.
1-5- Ask the user to enter a value to search (Linear search) in this array. Show if the value is found in the array or not.
If it is found, how many of that value is in the array. (In case of repeated value)
1-6- Write the sorted arrays in a file. (Ask the user to enter the name of the file)
Item 2: To read from a file: Your program should read a file to get the elements of the array of integers
(Use “Partially-Filled Arrays”). Prepare a text file with some integers.
2-1- Store the created array in a variable
2-2- Print the created array.
2-3- Use Selection Sort to sort this second array.
2-4- Print the array after sorting and inform the user that you use Selection Sort to sort the array.
2-5- Ask the user to enter a value to search (Binary search) in the array. Show if the value is found in the array or not.
If it is found, how many of that value is in the array. (In case of repeated value)
2-6- Write the sorted arrays in a file. (Ask the user to enter the name of the file)
Item 3: To exit of this menu
*/
This is my code so far. I have a problem to do the selection sort for the vector array 2. Please help. Thank you.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//Function prototypes
int linearSearch(int [], int, int);
void bubbleSort(int [], int);
void swapBubbleSort(int &, int &);
void PrintOut(int [], int);
void selectionSort(int [], int);
void swapSelectionSort(int &, int &);
//Main Function
int main()
{
int count = 1;
short int choice = 0;
//Start program. Print out platform
cout << "******************************** ";
cout << "* GET THE ELEMENTS OF AN ARRAY * ";
cout << "******************************** ";
cout << "-------------------------------- ";
//Display menu
while (choice != 3) {
cout << "Menu: Enter '1' for Option 1, '2' for Option 2, '3' for Option 3 ";
cout << "1. Option 1: To enter manually ";
cout << "2. Option 2: To read from a file ";
cout << "3. Option 3: Exit ";
cout << "Please choose your one of the following options below!(Enter 1, 2, or 3) ";
cin >> choice;
//Program running for each option chosen by users
switch (choice) {
//case 1: User manually input elements for the array
case 1:
{
int size;
//User input the size of the array (1-1)
cout << "Please enter how many elements do you want to input! ";
cin >> size;
int array1[size];
int i=0;
//User input elements for the array
for (i = 0; i < size; i++)
{
cout << "Please enter an element of the array! ";
cout << "Element-" << count++ << ": ";
cin >> array1[i];
}
//Program Output for unsorted array (1-2)
cout << "Here is your array: ";
PrintOut(array1, size);
//Bubble Sort (1-3)
bubbleSort(array1, size);
//Program Output for sorter array (bubbleSort) (1-4)
cout << "The array after sorting (bubbleSort) in ascending order: ";
for (auto element : array1)
cout << element << " ";
cout << endl;
//User input value they want to look for (1-5)
int value;
cout << "Enter the value of element that you would like to seach in the array: ";
cin >> value;
//Store result of linear search into a variable
int results = linearSearch(array1, size, value);
if (results != -1)
{
cout << "The number of elements found: ";
cout << linearSearch(array1, size, value) << endl;
}
else
{
//result = -1 mean no results is found
cout << "No results are found in the array!";
}
string name;
//Save the array into a txt file with input name from users (1-6)
cout << "Please enter name for the file you want to store the array: ";
cin >> name;
ofstream myfile (name);
if (myfile.is_open()) {
for (int i = 0; i < size; i++)
{
myfile << array1[i] << " ";
}
myfile.close();
}
else
{
cout << "Unable to open the file! " << endl;
}
cout << endl;
}
break; //end case 1
//case 2: Read elements from file name (input by user) for array elements
case 2:
{
ifstream Inputfile;
string filename;
int current_number = 0;
vector<int> array2;
//User input the file name to read (2-1)
cout << "What is the file name? ";
cin >> filename;
//Open file
Inputfile.open(filename);
if (Inputfile) //Check if file open successfully
{
cout << "Successful Open! ";
while (Inputfile >> current_number) {
array2.push_back(current_number);
}
Inputfile.close();
// Display the numbers read: (2-2)
cout << "The elements are: ";
for (int count = 0; count < array2.size(); count++)
{
cout << array2[count] << " ";
}
cout << endl;
//Selection Sort (2-3)
/*
selectionSort(array2, array2.size());
cout << "The sorted values: ";
for (auto element1 : array2)
cout << element1 << " ";
cout << endl;
*/
}
else
{
cout << "Error while openning! ";
}
}
break;
// end case 2
case 3:
{
cout << "Exit" << endl;
}
break;
default:
{
cout << "Please start the program again and choose one of the option! ";
}
break;
}
return 0;
}
}//end main function
//Stored array in Print Out (1-1)
void PrintOut(int array1[], int size)
{
for (int index = 0; index < size; index++)
cout << array1[index] << " ";
cout << endl;
}// end void PrintOut
void SecondPrintOut(int array2[], int size)
{
for (int index = 0; index < size; index++)
cout << array2[index] << " ";
cout << endl;
}// end void SecondPrintOut
//bubbleSort the array in ascending order (1-3)
void bubbleSort(int array1[], int size)
{
int maxElement;
int index;
for (maxElement = size -1; maxElement > 0; maxElement--)
{
for (index = 0; index < maxElement; index++)
{
if (array1[index] > array1[index + 1])
{
swap(array1[index], array1[index + 1]);
}
}
}
}//end void bubbleSort.
// The swap function swaps a and b in memory.
void swapBubbleSort(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}//end void swap
//Linear search (1-5)
int linearSearch(int array1[], int size, int value)
{
int count = 0;
for (int index = 0; index < size; index++) {
if (array1[index] == value)
{
//count how many found, return the count
count++;
}
}
return count;
}//end linear search
//Selection Sort the array (2-4)
void selectionSort(int array2[], int size)
{
int minIndex; int minValue;
for (int start = 0; start < (size -1); start++)
{
minIndex = start;
minValue = array2[start];
for (int index = start + 1; index < size; index++)
{
if (array2[index] < minValue)
{
minValue = array2[index];
minIndex = index;
}
}
swap(array2[minIndex], array2[start]);
}
}
// The swap function swaps a and b in memory.
void swapSelectionSort(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
Explanation / Answer
ANS:-
Given that,
PROGRAM:-
#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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.