Subversion Repositories programming

Rev

Rev 302 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * proj1-sequential.c - implement a simple program that will find the minimum
 *                      and maximum numbers out of a randomly generated list
 *                      of integers, using MPI to parallelize the operation.
 *
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 ******************************************************************************/

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

int main (int argc, char *argv[])
{
    int i;
    int c = -1;
    int n = -1;
    int min = INT_MAX;
    int max = INT_MIN;

    int *numbers;

    /* Parse the command line options */
    opterr = 0;

    while ((c = getopt (argc, argv, "n:")) != -1)
    {
        switch (c)
        {

            /* Try to get n */
            case 'n':
                n = atoi (optarg);
                break;

            /* Catch bad options */
            case '?':
                if (isprint (optopt))
                    fprintf (stderr, "Unknown option '-%c'.\n", optopt);
                else
                    fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt);

                return 1;
                break;
            default:
                abort ();
        }
    }

    /* Seed the random number generator */
    srand (time(NULL));

    /* Check if the n value is valid */
    if (n<=0)
    {
        fprintf (stderr, "Bad value for n, must be greater than 0\n");
        fprintf (stderr, "Please specify the '-n NUMBER' option\n");
        return 1;
    }

    /* Allocate memory for the random number */
    if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
    {
        fprintf (stderr, "Memory allocation for array failed, exiting...\n");
        return 2;
    }

    /* Generate random numbers, staying in the range [1,10n] */
    for (i=0; i<n; i++)
        numbers[i] = (rand() % (n*10)) + 1;

    /* Find the max and min of them */
    for (i=0; i<n; i++)
    {
        /* max */
        if (numbers[i] > max)
            max = numbers[i];

        if (numbers[i] < min)
            min = numbers[i];
    }

    /* Print the results */
    printf ("min: %d\n", min);
    printf ("max: %d\n", max);

#ifdef DEBUG
    for (i=0; i<n; i++)
        printf ("numbers[%d] = %d\n", i, numbers[i]);
#endif

    /* Clean up memory now that we're done with it */
    free (numbers);
    numbers = NULL;

    return 0;
}