I don't know if posting my entire code is necessary but will do so in the event that it may be needed.
This sortWords function is sorting backwards and it is running infinitely. I am having trouble figuring out why - any pointers as to where to look and what may be the issue would be much appreciated.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
usingnamespace std;
struct Stuff{
string words;
int g;
};
//declare functions
void sortWords(Stuff Results[], int&);
int main() {
//sort words in array
sortWords(Results, size);
inf.close();
return 0;
}
//*************************sortWords*********************************************
// Name: sortWords
// Description: sort words and eliminate duplicates
// Parameters: Results, int size
// Return: none
//*******************************************************************************
void sortWords(Stuff Results[], int& size) {
// fill in with the type of elements
for (int nStartIndex = 0; nStartIndex < size - 1; nStartIndex++) {
// nSmallestIndex is the index of the smallest element
// we've encountered so far.
int nSmallestIndex = nStartIndex;
//Search thru evry element starting at nStartIndex + 1
for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < size; nCurrentIndex++) {
// If the current element is smaller than our previously found smallest
if (Results[nCurrentIndex].word < Results[nSmallestIndex].word)
//Store the index in nSmallestIndex
nSmallestIndex = nCurrentIndex;
if (nSmallestIndex != nCurrentIndex)
swap(Results[nSmallestIndex], Results[nStartIndex]);
cout << Results[nStartIndex].word << endl; // for testing
}// 2nd for loop
}//1st for loop delimiter
}//function delimiter
Well, I tried out your program with an input file that had 13 words in it and your program attempted to sort over 1000 strings in a very naive fashion.
I would suggest changing line 45 to:
sortWords(recResults, countW);
so that you're only sorting words you've extracted from the file and changing the sort function to something more like the following to minimize the number of elements you're swapping.
1 2 3 4 5 6 7 8 9 10 11 12
for (unsigned i=0 ; i < size-1 ; ++i)
{
// find smallest index of the elements left which are unsorted:
unsigned smallest = i ;
for (int j=i+1; j < size ; ++j )
if ( recResults[smallest].word > recResults[j].word )
smallest = j ;
// Make the current element the smallest of those we found.
if ( smallest != i )
swap( recResults[smallest], recResults[i] ) ;
}
Your code for counting letters in countL is guaranteed to be off by one. You should loop on something other than the eof condition.
void sortWords(Results recResults[], int& size) {
// fill in with the type of elements
for (int nStartIndex = 0; nStartIndex < size - 1; nStartIndex++) {
// nSmallestIndex is the index of the smallest element thus far.
int nSmallestIndex = nStartIndex;
//Search thru evry element starting at nStartIndex + 1
for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < size; nCurrentIndex++) {
// If the current element is smaller than our previously found smallest
if (recResults[nCurrentIndex].word < recResults[nSmallestIndex].word)
//Store the index in nSmallestIndex
nSmallestIndex = nCurrentIndex;
}// 2nd for loop
if (nSmallestIndex != nStartIndex) //<-------- changed this
swap(recResults[nSmallestIndex], recResults[nStartIndex]);
cout << "Start Print" << endl;
cout << recResults[nStartIndex].word << endl; // for testing
}//1st for loop delimiter
}//function delimiter