0

So right now my program is simply storing the lowest values I input SIZE number of times, can someone help me out with this? I thought that testing the values against 'final' would fix this.

int least_to_greatest(int *scores){
int least_to_greatest[SIZE], a, b, c, n, low = 101, duplicate = 0, final = 0;

for (a = 0; a < SIZE; a++){     //assign low values to least_to_greatest[a]
    for (b = 0; b < SIZE; b++){ //chose low values of score
        if (scores[b] == low){  //deals with duplicates
            duplicate++;
        }
        if (scores[b] < low && scores[b] > final){  //assigns new lowest value to low as long as it's larger than previous low value
            low = scores[b];
            duplicate = 0;
        }
    }
    final = low;                            //final low value
    least_to_greatest[a] = final;
    if (duplicate > 0){                     //deals with duplicates
        for (c = 0; c < duplicate; c++){
            a++;
            least_to_greatest[a] = final;
        }
    }
}

for (n = 0; n < SIZE; n++){
    if (!(n % 5)){
        printf("\n"); //creates a newline after 5 values
    }
    printf("%d ", least_to_greatest[n]); //prints scores in least to greatest
}
getchar();

}

6
  • So you are sorting the array? As written, the b loop just repeats itself, and repeats the the (possibly multiple) lowest value(s) are found. If you want to keep the code in this form, then you need to somehow remove the low values found by the b loop so they aren't found again the next time around. Commented Apr 10, 2014 at 1:29
  • wouldn't the test scores[b] > final remove the lower terms? Commented Apr 10, 2014 at 1:30
  • You've not told us why your looking for duplicates, so that makes it hard to recommend a viable algorithm. In general though, you just need to work backwards through your result array from currentEntryCount to 1 copying the value from b-1. When you find a value with result[b-1] less than your new entry stop. Commented Apr 10, 2014 at 1:31
  • 2
    After each b loop, you need to reset duplicates to 0 and low to 101. Commented Apr 10, 2014 at 1:32
  • I'm just using the duplicates variable to keep track of low duplicates so that however many times they are duplicated they will be stored in the array if they are the lowest value. Commented Apr 10, 2014 at 1:34

1 Answer 1

1

Before the beginning of each 'b' loop, you need to set duplicate to 0 and low back to 101.

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

7 Comments

That just might be it! I'm going to try that out right now.
Did it work? I'm not suggesting this is the best way to sort numbers, but I was trying to fix your code. (Accept answer if it worked.)
Perfect! I don't know what I would do without this website, I'll have to make sure to look for that in the future.
Yea, I talked to a friend in school about it today who told me I'd have to use bubble sort, but I kinda like it this way because I'm able to learn instead of just copy and my teacher will know that it's genuine.
No problems. Accept the answer if it worked. Quicksort is the best sort (usually) but it is harder to implement.
|

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.