Home Contents



/*      Introduction to program:
        Program - mmen.c.
        Example of top-down programming.
        Francis O'Donovan 26-1-98. */

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

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

/*      Function: main(). */

main()
{
        /* Declare functions. */

        void Min();
        void Max();
        void Even();
        void Negative();
 

        /* Initialize variables. */

        int n1, n2, n3;       /* The three numbers to be used. */
 
        /* Print introduction. */

        printf( "Program: mmen.c.\n" );
        printf( "Example of top-down programming.\n" );
        printf( "Francis O'Donovan 26-1-98.\n\n" );

        /* Input numbers from user. */

        printf( "Please type in three numbers and ");
        printf( "press return after each.\n" );
        printf( "\n#1: ");
        scanf( "%d", &n1 );
        printf( "\n#2: ");
        scanf( "%d", &n2 );
        printf( "\n#3: ");
        scanf( "%d", &n3 );
 
        /* Call the four functions. */

        Min( n1, n2, n3 );
        Max( n1, n2, n3 );
        Even( n1, n2, n3 );
        Negative( n1, n2, n3 );

}

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

/*      Function: Min().
        Purpose: Outputs the least of three numbers, if
                so desired.
        Arguements: Three ints.
        Returns: void.
*/

void Min( n1, n2, n3)
int n1, n2, n3;
{
        /* Initialise variables. */
 
        int min;        /* The minimum of the three numbers. */
        char ans;       /* The user's answer. */

        /* Ask user do they want the minimum. */

        printf( "\n\nDo you want to know the least number?\n" );
        printf( "Please type \'y\' or \'n\'.\n" );
        scanf( "%c", &ans );

        /* If yes, calculate and print out answer. */
        /* By not calculating min unless ans = 'y',
           I save computation time. */

        if( ans == 'y' )
        {
                min = n1;
                if( min > n2 )
                        min = n2;
                if( min > n3 )
                        min = n3;

                printf( "\nThe minimum of %d, %d and", n1, n2 );
                printf( " %d is %d.\n", n3, min );

        }

}
 
/* ************************************************ */

/*      Function: Max().
        Purpose: Outputs the greatest of three numbers, if
                so desired.
        Arguements: Three ints.
        Returns: void.
*/

void Max( n1, n2, n3)
int n1, n2, n3;
{
        /* Initialise variables. */
 
        int max;        /* The maximum of the three numbers. */
        char ans;       /* The user's answer. */

        /* Ask user do they want the maximum. */

        printf( "\n\nDo you want to know the greatest number?\n" );
        printf( "Please type \'y\' or \'n\'.\n" );
        scanf( "%c", &ans );

        /* If yes, calculate and print out answer. */

        if( ans == 'y' )
        {
                max = n1;
                if( max < n2 )
                        max = n2;
                if( max < n3 )
                        max = n3;

                printf( "\nThe maximum of %d, %d and", n1, n2 );
                printf( " %d is %d.\n", n3, max );

        }

}
 
/* ************************************************ */

/*      Function: Even().
        Purpose: Outputs which of the three numbers
        is even, if so desired.
        Arguements: Three ints.
        Returns: void.
*/

void Even( n1, n2, n3)
int n1, n2, n3;
{
        /* Initialise variables. */
 
        char ans;       /* The user's answer. */

        /* Ask user do they want the evens. */

        printf( "\n\nDo you want to know the even numbers?\n" );
        printf( "Please type \'y\' or \'n\'.\n" );
        scanf( "%c", &ans );

        /* If yes, calculate and print out answer. */

        if( ans == 'y' )
        {
                printf( "\n" );
                if( (n1%2)== 0)
                        printf( "%d is even.\n", n1);
                if( (n2%2)== 0)
                        printf( "%d is even.\n", n2);
                if( (n3%2)== 0)
                        printf( "%d is even.\n", n3);

        }

}
 
/* ************************************************ */

/*      Function: Negative().
        Purpose: Outputs which of the three numbers
                is negative, if so desired.
        Arguements: Three ints.
        Returns: void.
*/

void Negative( n1, n2, n3)
int n1, n2, n3;
{
        /* Initialise variables. */
 
        char ans;       /* The user's answer. */

        /* Ask user do they want the negatives. */

        printf( "\n\nDo you want to know the negative numbers?\n" );
        printf( "Please type \'y\' or \'n\'.\n" );
        scanf( "%c", &ans );

        /* If yes, calculate and print out answer. */

        if( ans == 'y' )
        {
                printf( "\n" );
                if( n1 < 0)
                        printf( "%d is negative.\n", n1);
                if( n2 < 0)
                        printf( "%d is negative.\n", n2);
                if( n3 < 0)
                        printf( "%d is negative.\n", n3);

        }

}


Home Contents



© Francis O'Donovan 1999.