Home Contents



/*      Introduction to program:
        Program - permgood.c.
        Example of how to permute three numbers.
        Francis O'Donovan 2-2-98. */

/* Files to be included. */
 
#include <stdio.h>

/* ********************************************** */

/*      Function: main(). */

main()
{
        /* Declare functions. */

        void Permute();

        /* Initialize variables. */

        int x, y, z;       /* The three numbers to be used. */
 
        /* Print introduction. */

        printf( "Program: permgood.c.\n" );
        printf( "Example of how to permute three numbers.\n" );
        printf( "Francis O'Donovan 2-2-98.\n\n" );

        /* Get user to input numbers. */

        printf( "Please type in three numbers, x, y, and z:\n" );
        scanf( "%d", &x );
        scanf( "%d", &y );
        scanf( "%d", &z );
 
        /* Call Permute(). */

        Permute( &x, &y, &z );

        /* Output the three numbers. */

        printf( "x = %d, y = %d, z = %d.\n", x, y, z );
}

/* ************************************************ */

/*      Function: Permute().
        Purpose: Permutes three integers.
        Arguements: Three pointers to ints.
        Returns: void.
*/

void Permute( x, y, z)
int *x, *y, *z;
{
        /* Initialise variables. */
 
        int temp;        /* temporary holder. */

        /* Attempt to permute the numbers. */

        temp = *x;
        *x = *y;
        *y = *z;
        *z = temp;
}

/* Comment:
        This program will permute the three numbers, because it passes the
        addresses of the variables in main() to the function Permute,
        so Permute can change them directly.
*/


Home Contents



© Francis O'Donovan 1999.