Subversion Repositories programming

Rev

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

Rev Author Line No. Line
294 ira 1
/*******************************************************************************
303 ira 2
 * proj1-sequential.c - implement a simple program that will find the minimum
3
 *                      and maximum numbers out of a randomly generated list
4
 *                      of integers, using MPI to parallelize the operation.
294 ira 5
 *
6
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
7
 * All rights reserved.
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to
11
 * deal in the Software without restriction, including without limitation the
12
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13
 * sell copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in
17
 * all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
 * IN THE SOFTWARE.
26
 ******************************************************************************/
27
 
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <limits.h>
31
#include <unistd.h>
32
 
33
int main (int argc, char *argv[])
34
{
35
    int i;
36
    int c = -1;
37
    int n = -1;
38
    int min = INT_MAX;
39
    int max = INT_MIN;
40
 
41
    int *numbers;
42
 
43
    /* Parse the command line options */
44
    opterr = 0;
45
 
298 ira 46
    while ((c = getopt (argc, argv, "n:")) != -1)
294 ira 47
    {
48
        switch (c)
49
        {
50
 
51
            /* Try to get n */
52
            case 'n':
53
                n = atoi (optarg);
54
                break;
55
 
56
            /* Catch bad options */
57
            case '?':
58
                if (isprint (optopt))
59
                    fprintf (stderr, "Unknown option '-%c'.\n", optopt);
60
                else
61
                    fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt);
62
 
63
                return 1;
64
                break;
65
            default:
66
                abort ();
67
        }
68
    }
69
 
70
    /* Seed the random number generator */
298 ira 71
    srand (time(NULL));
294 ira 72
 
73
    /* Check if the n value is valid */
74
    if (n<=0)
75
    {
76
        fprintf (stderr, "Bad value for n, must be greater than 0\n");
77
        fprintf (stderr, "Please specify the '-n NUMBER' option\n");
78
        return 1;
79
    }
80
 
81
    /* Allocate memory for the random number */
82
    if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
83
    {
84
        fprintf (stderr, "Memory allocation for array failed, exiting...\n");
85
        return 2;
86
    }
87
 
88
    /* Generate random numbers, staying in the range [1,10n] */
89
    for (i=0; i<n; i++)
90
        numbers[i] = (rand() % (n*10)) + 1;
91
 
92
    /* Find the max and min of them */
93
    for (i=0; i<n; i++)
94
    {
95
        /* max */
96
        if (numbers[i] > max)
97
            max = numbers[i];
303 ira 98
 
294 ira 99
        if (numbers[i] < min)
100
            min = numbers[i];
101
    }
102
 
103
    /* Print the results */
104
    printf ("min: %d\n", min);
105
    printf ("max: %d\n", max);
106
 
107
#ifdef DEBUG
108
    for (i=0; i<n; i++)
109
        printf ("numbers[%d] = %d\n", i, numbers[i]);
110
#endif
111
 
302 ira 112
    /* Clean up memory now that we're done with it */
113
    free (numbers);
114
    numbers = NULL;
115
 
294 ira 116
    return 0;
117
}
118