forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathmain.cpp
53 lines (38 loc) · 1.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// main.cpp
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
extern "C" long vecsum (long*, long);
// Purpose: This main program produces a vector of random numbers
// between 0 and 99, then calls the externally defined function
// 'vecsum' to add up the elements of the vector.
// Originally written by Adam Ferrari, and updated by Aaron Bloomfield
int main () {
// delcare the local variables
long n, *vec, sum;
// how big is the array we want to use?
cout << "Please enter a array size: ";
cin >> n;
// sanity check the array size
if (n <= 0) {
cerr << "Array size must be greater than zero.\n";
return 1;
}
// allocate the array
vec = new long[n];
// use current time as random seed
srand((unsigned) time(NULL));
// fill the array with random values
for (long i = 0; i < n; ++i) {
vec[i] = rand() % 100;
cout << "\tvec[" << i << "] = " << vec[i] << endl;
}
// sum up the array and print out results
sum = vecsum(vec, n);
cout << "The sum of all array elements is " << sum << endl;
// properly deallocate the array
delete [] vec;
// all done!
return 0;
}