Question
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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started