forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
42 lines (33 loc) · 1.01 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
// main.cpp
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
extern "C" int vecsum (int *, int);
// 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.
// Author: Adam Ferrari
int main () {
int n, *vec, sum;
cout << "Please enter a vector size: ";
cin >> n;
// sanity check the vector size
if (n <= 0) {
cerr << "Vector size must be greater than zero.\n";
return 1;
}
// allocate, initialize, and display vector
vec = new int[n];
// use current time as random seed
srand((unsigned) time(NULL));
for (int i = 0; i < n; ++i) {
vec[i] = rand() % 100;
cout << "\tvec[" << i << "] = " << vec[i] << endl;
}
// sum up the vector and print out results
sum = vecsum(vec, n);
cout << "The sum of all vector elements is " << sum << endl;
return 0;
}