Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
294 ira 1
/*******************************************************************************
2
 * proj1.c - implement a simple program that will find the minimum and maximum
3
 *           numbers out of a randomly generated list of integers, using MPI
4
 *           to parallelize the operation.
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
//#include <mpi.h>
33
 
34
int main (int argc, char *argv[])
35
{
36
    int i;
37
    int c = -1;
38
    int n = -1;
39
    int min = INT_MAX;
40
    int max = INT_MIN;
41
    unsigned int seed = 1;
42
 
43
    int *numbers;
44
 
45
    /* Parse the command line options */
46
    opterr = 0;
47
 
48
    while ((c = getopt (argc, argv, "s:n:")) != -1)
49
    {
50
        switch (c)
51
        {
52
 
53
            /* Try to get n */
54
            case 'n':
55
                n = atoi (optarg);
56
                break;
57
 
58
            /* Try to get a seed (optional) */
59
            case 's':
60
                seed = atoi (optarg);
61
                break;
62
 
63
            /* Catch bad options */
64
            case '?':
65
                if (isprint (optopt))
66
                    fprintf (stderr, "Unknown option '-%c'.\n", optopt);
67
                else
68
                    fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt);
69
 
70
                return 1;
71
                break;
72
            default:
73
                abort ();
74
        }
75
    }
76
 
77
    /* Seed the random number generator */
78
    srand (seed);
79
 
80
    /* Check if the n value is valid */
81
    if (n<=0)
82
    {
83
        fprintf (stderr, "Bad value for n, must be greater than 0\n");
84
        fprintf (stderr, "Please specify the '-n NUMBER' option\n");
85
        return 1;
86
    }
87
 
88
    /* Allocate memory for the random number */
89
    if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
90
    {
91
        fprintf (stderr, "Memory allocation for array failed, exiting...\n");
92
        return 2;
93
    }
94
 
95
    /* Generate random numbers, staying in the range [1,10n] */
96
    for (i=0; i<n; i++)
97
        numbers[i] = (rand() % (n*10)) + 1;
98
 
99
    /* Find the max and min of them */
100
    for (i=0; i<n; i++)
101
    {
102
        /* max */
103
        if (numbers[i] > max)
104
            max = numbers[i];
105
 
106
        if (numbers[i] < min)
107
            min = numbers[i];
108
    }
109
 
110
    /* Print the results */
111
    printf ("min: %d\n", min);
112
    printf ("max: %d\n", max);
113
 
114
#ifdef DEBUG
115
    for (i=0; i<n; i++)
116
        printf ("numbers[%d] = %d\n", i, numbers[i]);
117
#endif
118
 
119
    return 0;
120
}
121