Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/*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

/*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

#include

#include

#include

#include

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;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions