Subversion Repositories programming

Rev

Rev 298 | 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
/*******************************************************************************
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
 
42
    int *numbers;
43
 
44
    /* Parse the command line options */
45
    opterr = 0;
46
 
298 ira 47
    while ((c = getopt (argc, argv, "n:")) != -1)
294 ira 48
    {
49
        switch (c)
50
        {
51
 
52
            /* Try to get n */
53
            case 'n':
54
                n = atoi (optarg);
55
                break;
56
 
57
            /* Catch bad options */
58
            case '?':
59
                if (isprint (optopt))
60
                    fprintf (stderr, "Unknown option '-%c'.\n", optopt);
61
                else
62
                    fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt);
63
 
64
                return 1;
65
                break;
66
            default:
67
                abort ();
68
        }
69
    }
70
 
71
    /* Seed the random number generator */
298 ira 72
    srand (time(NULL));
294 ira 73
 
74
    /* Check if the n value is valid */
75
    if (n<=0)
76
    {
77
        fprintf (stderr, "Bad value for n, must be greater than 0\n");
78
        fprintf (stderr, "Please specify the '-n NUMBER' option\n");
79
        return 1;
80
    }
81
 
82
    /* Allocate memory for the random number */
83
    if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
84
    {
85
        fprintf (stderr, "Memory allocation for array failed, exiting...\n");
86
        return 2;
87
    }
88
 
89
    /* Generate random numbers, staying in the range [1,10n] */
90
    for (i=0; i<n; i++)
91
        numbers[i] = (rand() % (n*10)) + 1;
92
 
93
    /* Find the max and min of them */
94
    for (i=0; i<n; i++)
95
    {
96
        /* max */
97
        if (numbers[i] > max)
98
            max = numbers[i];
99
 
100
        if (numbers[i] < min)
101
            min = numbers[i];
102
    }
103
 
104
    /* Print the results */
105
    printf ("min: %d\n", min);
106
    printf ("max: %d\n", max);
107
 
108
#ifdef DEBUG
109
    for (i=0; i<n; i++)
110
        printf ("numbers[%d] = %d\n", i, numbers[i]);
111
#endif
112
 
113
    return 0;
114
}
115