** EDIT: sorry, typo in my title. I meant to write 3 pointers instead of 2.

I am trying to revise/create a function in which it is given given three pointers to floats and reorders the values pointed to into ascending order.

The prototype for the function would be:

void reorder(float *a, float *b, float *c);

i.e. when the function is done, the following should hold:
*a <= *b <= *c

Input: 3 1 2
Expected output: 1 2 3

I have already created one function (swapbig) and a test driver (main) for it. Swapbig () simply swaps the values in the cells pointed to such that the value pointed to by the first is less than (or equal to) the value pointed to by the second.
My assignment is to create a new driver an another function (reorder) that will implement the swapbig function.

Here are my programs along with their headers:

/* File:  driver1.c */

/* Driver to test out swapbig function */

#include <stdio.h>
#define DEBUG

int main()
{
  // Declarations
  float a, b;
  float flag;

        // Prompt user for values of pointeres
        #ifdef DEBUG
        printf("Enter value for a, b: ");
        #endif

        // Scan input
        flag = scanf("%f %f", &a, &b);

        // While there is more input
        while ( flag != EOF)
        {
                // Print initial values
                printf("Given values:\t a = %f, b = %f\n", a, b);

                // Call function
                swap_big(&a, &b);

                #ifdef DEBUG
                printf("Enter value for a,b: ");
                #endif

                // Re-scan input
                flag = scanf("%f %f", &a, &b);
        }

}

-----

/* File:  swapbig.h */

// Header file for swapbig.c

void swap_big(float *a, float *b);
/* Given  : two pointers to floats
   Returns: swaps the values in the cells pointed 
            to such that the value pointed by the first 
            is less than or equal to the vlaue pointed to
            by the second 
*/

-----

/* File:  swapbig.c */

#include <stdio.h>

#include "swapbig.h"

#define DEBUG

void swap_big(float *x, float *y)
/* Given  : two pointers to floats
 * Returns: swaps the values in the cells pointed
           to such that the value pointed by the first
           is less than or equal to the vlaue pointed to
           by the second
*/
{
 // Declarations
 float tempx, tempy;

       // save first  value
       tempx = *x;

       // save second value
       tempy = *y;

       // SWAPPING VALUES:

        // move second value to first value
        *x = *y;

        // move first value to second
        *y = tempx;

       #ifdef DEBUG
       printf("Swapped values:\n");
       printf(" First pointer  *a = %f\n", *x);
       printf(" Second pointer *b = %f\n", *y);
       #endif

       // If value pointed by first is greater than second
       if (*x > *y)
       {

               *x = tempx;
               *y = tempy;

               #ifdef DEBUG
               printf("First pointer must be less than or\n equal to second pointer, so correct swap is:\n");
               printf(" First pointer  *a = %f\n", *x);
               printf(" Second pointer *b = %f\n", *y);
               #endif

       }

       else
       {
               #ifdef DEBUG
               printf("Value pointed by first is less than or\n equal to value pointed by second.\n");
               #endif
       }

       // Skip a line for readable next input
       printf("\n");
}

And this is what I am now working on:

/* File:  driver2.c */

/* Driver to test out reorder() function */

#include <stdio.h>

#include "swapbig.h"
#include "reorder.h"

#define DEBUG

int main()
{
 // Declarations
 float a, b, c;
 float flag;

        // Prompt user for float values
        #ifdef DEBUG
        printf("Enter values for a, b, c: ");
        #endif

        flag = scanf("%f %f %f", &a, &b, &c);

        while ( flag != EOF )
        {
                // Print inital values
                printf("Given values: a = %f, b = %f, c = %f\n", a, b, c);

                // Call function to order values into ascending order
                reorder(&a, &b, &c);

                // Prompt user for float values
                #ifdef DEBUG
                printf("Enter values for a, b, c: ");
                #endif

                flag = scanf("%f %f %f", &a, &b, &c);

        }
}

-----

/* File:  reorder.c */

// This file contains function reorder()

#include <stdio.h>

#include "reorder.h"
#include "swapbig.h"

#define DEBUG

void reorder(float *a, float *b, float *c)
/* Given  : three pointers to float
 * Returns: the value reordered into ascending order
 */
{
        swap_big(*a, *b);

}

I suddenly got stuck on the file reorder.c because I just realized that swap_big does not return any values and I am supposed to keep this function declared as void. If it weren't void, I would have used if-else statements. But perhaps there might be an easier way to do this? Could someone please give me some tips or suggestions?

>But perhaps there might be an easier way to do this?
Well, if you don't want to do the if statement route (probably best in this case), you could go overkill and use qsort...

#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b)
{
    const float *ia = a;
    const float *ib = b;

    if (*ia < *ib)
        return -1;
    else if (*ia > *ib)
        return +1;

    return 0;
}

void sort3(float *a, float *b, float *c)
{
    float temp[3];

    temp[0] = *a;
    temp[1] = *b;
    temp[2] = *c;

    qsort(temp, 3, sizeof(float), &compare);

    *a = temp[0];
    *b = temp[1];
    *c = temp[2];
}

int main(void)
{
    float a = 3;
    float b = 1;
    float c = 2;

    printf("%f %f %f\n", a, b, c);
    sort3(&a, &b, &c);
    printf("%f %f %f\n", a, b, c);

    return 0;
}

It's easier, but unlikely to be "better".

Why do you need the function swap big ?
You do the usual if else thingy in the function reorder.
Anyway you have pointers to the variables a,b,c as function parameter. You do the if else thingy and put the values in correct order in the variables if else. Some thing of this sort
http://answers.yahoo.com/question/index?qid=20061012215313AAYk5BA

Does that solve your problem or am I missing some thing ?

But the if else code becomes more and more complicated as soon as the number of variables increase. In such a scenario qsort will be better

Hi how about if I would like to use this function:

void reorder3(int a, int *b, int **c);

how could I do it?. I realy would apreciate some help.
thanks
regards
Lars-Åke

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.