1

I am very new to programming. For an assignment I have to make a function that can sort the array in ascending order. I have written a function, however it does not work entirely. I have read a lot of questions about cases like this on this forum already (searched for bubble sort for example), however I still can't work out my problem.

my code:

void sortOnValue(float *values, int size) 
{
int i, d;
float swap;

    for (i = 0; i < (size - 1); i++)
    {
        for (d = 0; d < (size - 1 - i); d++);
        {
            if (values[d] > values[d+1])
            {
                swap = values[d];
                values[d] = values[d+1];
                values[d+1] = swap;
            }
        }
    }
}

void main()
{
int i;
float x, y;
float val[10]; //1.5, 2.2, 7.3, 9.2, 7.4, 7.5, -8.0, 1.5, 12
val[0] = 1.5;
val[1] = 2.2;
val[2] = 7.3;
val[3] = 9.2;
val[4] = 7.4;
val[5] = 7.5;
val[6] = -8.0;
val[7] = 1.5;
val[8] = 12;

printValues(val, 10);
sortOnValue(val, 10);
printValues(val, 10);
}

my output:

Values:  1.500 -8.000 2.200 7.300 9.200 7.400 7.500 0.000 1.500 12.000

Any idea why it is not working? I think it might be because the loop ends before it should, however I am not sure. Also, is there a more efficient way of assigning values to an array than what I did?

Thanks in advance for any help!

7
  • 1
    For one thing you don't initialize all elements in the array. Commented Sep 24, 2016 at 11:40
  • float val[10] = {1.5, 2.2, 7.3, 9.2, 7.4, 7.5, -8.0, 1.5, 12} ; for assigning. This way, unspecified values will also be set to 0. Commented Sep 24, 2016 at 11:41
  • Thank you! I tried that with ; seperating the values, so thanks for that tip Commented Sep 24, 2016 at 11:43
  • 3
    You should also learn how to use a debugger. With a debugger you can step through the code line by line while monitoring variables and their values. Being able to use a debugger is an essential skill for all programmers, no matter if it's just for a hobby or professionally. Commented Sep 24, 2016 at 11:45
  • 2
    for (d = 0; d < (size - 1 - i); d++); remove last ;. and printValues(val, 10); --> printValues(val, 9); , Pass as the size 9 instead of 10. Commented Sep 24, 2016 at 11:46

2 Answers 2

1

Look closely at this line:

   //              what's this? -------v
   for (d = 0; d < (size - 1 - i); d++);
   {

You have a stray semicolon at the end of this for. As a result, what you have here is an empty loop. Then the block below it runs every time.

Get rid of it and the program runs as expected.

As a matter of style, it's good practice to have the opening brace of a block on the same line as the statement that starts it as follows:

   for (d = 0; d < (size - 1 - i); d++) {

That helps to reduce the chances of something like this happening.

Also, if you want to initialize an array, you can do it like this:

float val[10] = { 1.5, 2.2, 7.3, 9.2, 7.4, 7.5, -8.0, 1.5, 12, 9.9 };

Note that previously you weren't initializing all 10 elements in the array. I added one more element here to fill in the whole thing.

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

Comments

1

Change

for (d = 0; d < (size - 1 - i); d++); and d+1

To

for ( d = i+1 ;d < size; d++) and d+i

Compare values[i] with values[d+i] and then swap accordingly.

1 Comment

Just note that since the last value isn't initialised, removing -1 might break something in this example.

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.