Question: C++ Please look at my code and help make changes needed to get it to compile. Please see the instructions im trying to accomplish posted
C++
Please look at my code and help make changes needed to get it to compile.
Please see the instructions im trying to accomplish posted at this link: https://imgur.com/a/Tv3gL
Im compiling it PuTTy (linux)
im having issues with the functions "readintoArray()" , getmin(), getMax(), and not sure if im using recursion properly in the recursiveSort() or printArrayRec()
Thanks for your help!!
=============CODE==================
#include
#include
#include
using namespace std;
//reads the file and fills out the array
void readintoArray(string fileName, string words[], int numOfWords)
{
ifstream input_file(fileName);
for (int i = 0; i < numOfWords; i++)
{
getline(input_file, words[i]);
}
input_file.close();
}
//gets and returns the min value of the array
int *getMin(int array[], int arr_size)
{
int min = array[0];
for (int i = 1; i < arr_size; i++)
{
if (arr_size[&i] < min)
{
min = arr_size[&i];
}
}
return &min;
}
//gets and returns the max value of the array
int *getMax(int array[], int arr_size)
{
int max = array[1];
for (int i = 1; i < arr_size; i++)
{
if (arr_size[&i] > max)
{
max = arr_size[&i];
}
}
return &max;
}
// normal sort function
int normalSort(int arr[] , string sortOrder, int numberOfLines)
{
int i, j;
int n = numberOfLines;
cout << "normal" << endl;
if(sortOrder == "ascending" )
{
cout << "ascending";
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) //ensure this sorts in ASC order
swap(arr[j], arr[j + 1]);
}
else if(sortOrder == "descending")
{
cout << "descending";
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] < arr[j + 1]) //this may need work , ensure it sorts this in DESC order
swap(arr[j], arr[j + 1]);
}
else
{
cout << "Error: invalid input in normal sort function" << endl;
}
}
// recursive sort function
int recursiveSort(int arr[] , string sortOrder, int numberOfLines)
{
int n = numberOfLines;
cout << "recursive" << endl;
if (sortOrder == "ascending")
{
cout << "ascending";
for (int i = 0; i < n - 1; i++)
if (arr[i] > arr[i + 1])
swap(arr[i], arr[i + 1]);
recursiveSort(arr, sortOrder, (n - 1));
}
else if(sortOrder == "descending")
{
cout << "descending";
for (int i = 0; i < n - 1; i++)
if (arr[i] < arr[i + 1])
swap(arr[i], arr[i + 1]);
recursiveSort(arr, sortOrder, (n - 1));
}
else
{
cout << "Error: invalid input in rescursive sort function"< } } void swap(int &first,int &second) { int temp = first; first = second; // i have no clue if this is what i need second = temp; } //Prints the array normally void printArrayNorm(char array[], char array_size) { cout << "After: "; for (int i = 0; i < array_size; i++) { //WORK HERE cout << array[i] << " "; } cout << endl; } //Recursively prints the array void printArrayRec(char array[], char array_size) { if (array_size == 0) { return; } cout << *array; printArrayRec(array + 1, array_size - 1); } void printPreArray(char array_size , char array[]) { cout << "Before: "; for (int i = 0; i < array_size; i++) { cout << array[i] << " "; } cout << endl; } void InitialDecider(int new_Arr[], string cond, string sortOrder, int numberOfLines) { if (cond == "normal") { normalSort(new_Arr, sortOrder, numberOfLines); //goes to normalsort with the array and asc dec selection } else if (cond == "recursive") { recursiveSort(new_Arr, sortOrder, numberOfLines); //goes to rec sort with the array and asc dec selection } else { cout << "Invalid entry for argument 3"; } } int main(int argc, char* argv[]) { string inputFilename = argv[1]; //NAME OF INPUT FILE int numberOfLines = atoi(argv[2]); // number of lines string sortOrder = argv[3]; // ascending or descending string sortType = argv[4]; // normal or recursive char new_Arr[numberOfLines]; // new array with dynamic size readintoArray(inputFilename, new_Arr, numberOfLines); //first function , reads file into array InitialDecider(new_Arr, sortType, sortOrder, numberOfLines); printPreArray(numberOfLines, new_Arr); //Prints out the array prior to sorting == OUTPUT 4 return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
