need help completing almost finished function.
my outputs are not all correct:
correct output:
input file with "AbracadaBra" and "3" "7" as low and high
i get correct output:
0 1 2 3 4 5 6 10 7 8 9
NOT correct output when:
input file contains "AbracadaBra" and "0" "10" as high and low
i get incorrect output.
should be :10 7 0 3 5 8 1 4 6 9 2
also:
Input file contains "AbracadaBra" and "0" "6" as high and low
i get incorrect output.
should be :0 3 5 1 4 6 2 7 8 9 10
my goal:
--------------------------------------------------------------------------
My code so far:
#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;
void readFromFile(string &S, string filename); void convertToLower(string &S); bool lessThan(const string &S, int first, int second, int pass); void insert(const string &S, vector &indices, int low, int high);
int main(int argc, char *argv[]) { string S; string filename = argv[1]; vector index; readFromFile(S,filename); convertToLower(S); if (argc == 4){ int low= atoi(argv[2]); int high= atoi(argv[3]); for(int i=0; i void readFromFile(string &S, string filename) { string buffer; ifstream ifile; ifile.open(filename);
while (getline(ifile, buffer, ' ')) { string Stemp; Stemp = buffer; S = S+Stemp; } } void convertToLower(string &S) { transform(S.begin(), S.end(), S.begin(), ::tolower); }
bool lessThan(const string &S, int first, int second , int pass) {
if(S[first] S[second]){ return false; } else return lessThan(S, first+1, second+1,0); }
void insert(const string &S, vector &indices, int low, int high) {
int length = (high), key =0 , j=0; for(int i= low+1; i
key = indices[i]; j=i-1; while(j>= low && lessThan(S,j,key,0)== false)// S[j] > key { indices[j+1] = indices[j]; j=j-1; } indices[j+1] =key; } for(int k=0; k
cout
makefile:
run: main.o g++ -Wall -pedantic -g -o run main.o
main.o: main.cpp g++ -Wall -pedantic -g -std=c++11 -c main.cpp
clean: rm -rf run main.o
Input parameters: 1. Constant string S by reference. 2. Vector of integers indices holding indices of suffixes of S 3. Integer low, the lowest index of the range. 4. Integer high, the highest index of the range Output: Use cout to print out the indices from the vector indices in this format: Insertion must sort suffixes of the input string S in alphabetical order The function will sort the suffixes of S by calling lessThan function (from Assignment 5) and swapping positions of the suffixes. Re-write Insertion sort algorithm to sort suffixes of a given string S. Similarly to QuickSort, your insertion function will take a string S (passed constant by reference), and a vector of integers with the starting positions of suffixes of S. The function will sort the suffixes of S in the range from low to high (inclusive indices of the range). Insertion will call lessThan function (from Assignment 5) and will swap positions of the suffixes instead of suffixes. Here is the header of this function: void insertion(const string &S, vectorint> &indices, int low, int high) Sample input: s "abracadabra", indices (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], low 3, high 7 insertion(S, indices, low, high); //will sort suffixes starting at positions 3, 4, 5, 6, and 7. Sample output: After insertion is called on the sample input, vector indices will have the following order of suffix positions: indices 10, 1, 2,7, 3, 5, 4, 6, 8, 9, 10). Only suffixes at indices 3, 4, 5, 6, and 7 are ordered alphabetically. Input parameters: 1. Constant string S by reference. 2. Vector of integers indices holding indices of suffixes of S 3. Integer low, the lowest index of the range. 4. Integer high, the highest index of the range Output: Use cout to print out the indices from the vector indices in this format: Insertion must sort suffixes of the input string S in alphabetical order The function will sort the suffixes of S by calling lessThan function (from Assignment 5) and swapping positions of the suffixes. Re-write Insertion sort algorithm to sort suffixes of a given string S. Similarly to QuickSort, your insertion function will take a string S (passed constant by reference), and a vector of integers with the starting positions of suffixes of S. The function will sort the suffixes of S in the range from low to high (inclusive indices of the range). Insertion will call lessThan function (from Assignment 5) and will swap positions of the suffixes instead of suffixes. Here is the header of this function: void insertion(const string &S, vectorint> &indices, int low, int high) Sample input: s "abracadabra", indices (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], low 3, high 7 insertion(S, indices, low, high); //will sort suffixes starting at positions 3, 4, 5, 6, and 7. Sample output: After insertion is called on the sample input, vector indices will have the following order of suffix positions: indices 10, 1, 2,7, 3, 5, 4, 6, 8, 9, 10). Only suffixes at indices 3, 4, 5, 6, and 7 are ordered alphabetically