I am writing a sorting algorithm. At the moment, I am trying to fill up an array with sorted elements. This is kind of a bubble sort method, I believe. Basically, what I'm doing is I'm ranking lines based on score with highest score at bestmatch[0], etc. For each line I run stage3().
So, essentially, I take the Score (score for each line) and I compare it with what is in the array, then add this in based on its rank comparatively. It's not working. My print statement, which prints for each input line, is just printing the score of that individual line (provided it is non-zero). Could I please get some help?
void
stage3(double Score, line_t * linePtr) {
int j = 0;
line_t line;
size_t maxSz = MAX_LINELEN;
int scorecmp(int j, double Score, line_t * linePtr);
if (Score != 0 ) {
if (j < TOP_SCORING_MAX) {
scorecmp(j, Score, linePtr);
j++;
} /* fill up array */
else {
/* compare with last element
if greater than last element, check against
every element, moving it down while the thing
is bigger
when it is less than element, put it in that gap
*/
}
}
}
This is second function
int
scorecmp(int j, double Score, line_t * linePtr) {
line_t bestmatch[TOP_SCORING_MAX];
line_t line;
if (j == 0) {
bestmatch[j].score = Score;
bestmatch[j].index = linePtr->index;
bestmatch[j].buf = linePtr->buf;
}
else if (line.score > bestmatch[j-1].score) {
bestmatch[j].score = bestmatch[j-1].score;
bestmatch[j].index = bestmatch[j-1].index;
bestmatch[j].buf = bestmatch[j-1].buf;
bestmatch[j-1].score = Score;
bestmatch[j-1].index = linePtr->score;
bestmatch[j-1].buf = linePtr->buf;
}
else if (line.score <= bestmatch[j-1].score) {
bestmatch[j].score = Score;
bestmatch[j].index = linePtr->index;
bestmatch[j].buf = linePtr->buf;
}
printf("best match = %f\n",bestmatch[0].score);
return 0;
}
When I've finished this, I then need to compare any additional lines to the bottom scoring one in the array. If it is larger, I then need to compare it with every position in array until it finds its place.
Thank you
Here is the definition of line_t
typedef struct line_t
{
char* buf;
int lineLength;
int wordCount;
int index;
double score;
} line_t;
bestMatcharray inscorecmpis destroyed with each invoke, you know that, right? As written only one of those elements will ever had any actual data that is determinate. If the aroma of an XY Problem could be any more pungent than it is on this code, I'm pressed to see how.bestMatchas I described it? It wasn't meant as an insult, and the possibility of an XY problem is very common, particularly in beginners. A brief but accurate description of the X problem you're trying to solve, and how you think your Y implementation addresses it tremendously helps in finding problems in code, but usually more important, problems in thought-process that bore said code problems. Sample data , both input and expected/desired output, is tremendously helpful as well. Compilable code is, of course, always welcome.