Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with the c++ HW. Will post the related files later /* FileName: sortingMain.cpp * Author: * Date: * * Description: Sort integers using

Need help with the c++ HW. Will post the related files later

/* FileName: sortingMain.cpp

* Author:

* Date:

*

* Description: Sort integers using selection and bubble sort

*/

#include

#include

#include "Sorting.h"

using namespace std;

// Global Variable for main

// Stores the values read in from a tex file

static int* values;

int readInValues(char* fileName);

/**

* @param argc - number of command line arguments

* @param argv - arguments

* @return - 0 for ok termination, -1 for error

*/

int main(int argc, char* argv[])

{

if (argc != 2) {

cout ";

return -1;

}

else {

// Get filename from the command line arguments

char *fileName = argv[1];

// Read in the values

int numValues = readInValues(fileName);

// Store the algorithm and sorting choice

int sortChoice;

int orderChoice;

// Get the algorithm choice

cout

cout

cout

cout

cout

cin >> sortChoice;

// Get the comparison choice

cout

cout

cout

cout

cout

cin >> orderChoice;

cout

// Based on user input call the appropriate functions

switch (sortChoice)

{

case 0:

if (orderChoice) {

sortArray(values, numValues, selectionSort, descending);

}

else {

sortArray(values, numValues, selectionSort, ascending);

}

break;

case 1:

if (orderChoice) {

sortArray(values, numValues, bubbleSort, descending);

}

else {

sortArray(values, numValues, bubbleSort, ascending);

}

break;

}

// Print out the results

cout

cout

// Print out the Values

for (int i = 0; i

cout

values++;

}

cout

}

return 0;

}

/**

* @arg fileName - filename to read in integer values from.

*

* @return - the number of elements read

*/

int readInValues(char* fileName)

{

int cValue; // Store the current value

int numValues; // Store number of values

// Open the text file

ifstream infile(fileName);

// Check to see if the file opened ok

if (!infile) {

cout

system("pause");

}

// Prompt

cout

// Get the first value to signify the number of values in the file

infile >> numValues;

// Allocate memory space

values = (int*)malloc(numValues * sizeof(int));

// Read in the values, incrementing the pointer

for (; infile >> cValue; values++) {

// Deference the current pointer location Store read in value

*values = cValue;

}

// Reset the pointer to orginal location

values -= numValues;

// Prompt

cout

// Return the number of values read

return numValues;

}

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

/** * FileName: Sorting.h * Author: * Date: * * Description: Functions for sorting. Bubble sort, Selection Sort */ #pragma once /** * This function sorts integers based on function pointers to * a desired sorting function and desired comparison function. * * @param values - pointer to array of integers * @param n - number of integers in the array * @param alg - function pointer to sorting algorithm * @param cmp - function pointer to comparision function * */ void sortArray(int* values, int n, void(*alg)(int*, int, bool(*cmp)(int, int)), bool(*)(int, int)); /** * This function sorts integers using the bubble sort method * based on a desired comparison function. * * @param values - pointer to array of integers * @param n - number of integers in the array * @param cmp - function pointer to comparision function * */ void bubbleSort(int* values, int n, bool(*cmp)(int, int)); /** * This function sorts integers using the selection sort method * based on a desired comparison function. * * @param values - pointer to array of integers * @param n - number of integers in the array * @param cmp - function pointer to comparision function * */ void selectionSort(int* values, int n, bool(*cmp)(int, int)); /** * This function swaps the values of two integers. * * @param a - pointer to an integer * @param b - pointer to an integer * */ void swap(int* a, int* b); /** * This function sorts integers using the selection sort method * based on a desired comparison function. * * @param values - pointer to array of integers * @param n - number of integers in the array * @param cmp - function pointer to comparision function * */ bool ascending(int a, int b); /** * This function compares two integers for descending order * * @param a - pointer to an integer * @param b - pointer to an integer * * @return true if a

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

/*

* FileName: Sorting.cpp

* Author:

* Date:

*

* Description: Implemented Functions for sorting. Bubble sort, Selection Sort

*/

#include "Sorting.h"

/**

* This function sorts integers based on function pointers to

* a desired sorting function and desired comparison function.

*

* @param values - pointer to array of integers

* @param n - number of integers in the array

* @param alg - function pointer to sorting algorithm

* @param cmp - function pointer to comparision function

*

*/

void sortArray(int* values, int n, void(*alg)(int*, int, bool(*cmp)(int, int)), bool(*cmp)(int, int))

{

// Finish

}

/**

* This function sorts integers using the bubble sort method

* based on a desired comparison function.

*

* @param values - pointer to array of integers

* @param n - number of integers in the array

* @param cmp - function pointer to comparision function

*

*/

void bubbleSort(int* values, int n, bool(*cmp)(int, int))

{

// Finish

}

/**

* This function sorts integers using the selection sort method

* based on a desired comparison function.

*

* @param values - pointer to array of integers

* @param n - number of integers in the array

* @param cmp - function pointer to comparision function

*

*/

void selectionSort(int* values, int n, bool(*cmp)(int, int))

{

// Finish

}

/**

* This function sorts integers using the selection sort method

* based on a desired comparison function.

*

* @param values - pointer to array of integers

* @param n - number of integers in the array

* @param cmp - function pointer to comparision function

*

*/

void swap(int* a, int* b)

{

// Finish

}

/**

Page 5

* This function sorts integers using the selection sort method

* based on a desired comparison function.

*

* @param values - pointer to array of integers

* @param n - number of integers in the array

* @param cmp - function pointer to comparision function

*

*/

bool ascending(int a, int b)

{

return 0;// Finish

}

/**

* This function compares two integers for descending order

*

* @param a - pointer to an integer

* @param b - pointer to an integer

*

* @return true if a

*/

bool descending(int a, int b)

{

return 0;

}

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

In this exercise, we will create a program that will sort integers using the selection sort method and the bubble sort methods. Integers will be read in from a provided text file, with the name of the text file passed as a command line argument. Integers will be stored in memory using a provided function that will allow the programmer access to the integers via a pointer. The end-user will be prompted for which algorithm will be used (Selection or Bubble) and then asked for the comparison operation (Ascending or Descending). By performing this exercise, knowledge of using pointers and function pointers will be gained. // Selection Sort a[0] to a n-1] is the array to sort / int i,j int iMin; advance the position through the entire array (could do j<-l>

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_2

Step: 3

blur-text-image_3

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

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions

Question

Describe the appropriate use of supplementary parts of a letter.

Answered: 1 week ago