0

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;
10
  • Isn't this the exact same code as in your previous stackoverflow.com/questions/26030063/…? Commented Sep 25, 2014 at 8:24
  • @Jongware Pretty much. There are a couple of very small changes. The question is different though. Is that allowed? Commented Sep 25, 2014 at 8:29
  • 1
    I'm utterly lost what "array" you're "filling with sorted elements". You're bestMatch array in scorecmp is 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. Commented Sep 25, 2014 at 8:47
  • @WhozCraig I'd appreciate it if rather than insult me, you'd offer more guidance as to what I'm doing wrong. I admit, I made a stupid mistake (even as a beginner), but, in all fairness, your comment achieves about as much as my code does. Commented Sep 25, 2014 at 9:12
  • Were you aware of the scope-lifetime issue with bestMatch as 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. Commented Sep 25, 2014 at 9:18

1 Answer 1

1

In your code, bestmatch[] is a local array. so, it's expired when each step is finished.
According to your algorithm, bestmatch[] should be keeped.

For solving this problem, you have two methods.

Simple method is that you define bestmatch[] to global variable.
Prefered method is that you define bestmath[] in specific function like stage3() or pervious caller function and pass bestmath[] to scorecmp().

Sign up to request clarification or add additional context in comments.

4 Comments

Ohh! That seems so obvious now! I'll give it ago and let you know what happens
This is almost definitely a stupid mistake on my part. I just redefined stage3 as: stage3(double Score, line_t * linePtr, line_t bestmatch), and I did similar for scorecmp(). I have defined bestmatch in main, which is where I call stage3(), and my function call is this: stage3(Score,&line,bestmatch[TOP_SCORING_MAX]) - I am receiving an error saying "subscripted value is neither array nor pointer". What am I doing wrong?
Yep, that's what I've done (= Is there something wrong with my calling of the function?
I think each functions are declared as like below : - void stage3(double Score, line_t *linePtr, line_t * bestmatch); - int scorecmp(int j, double Score, line_t * linePtr, line_t * bestmatch); And in main, you have to call as like stage3(Score,&line,bestmatch)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.