-
Notifications
You must be signed in to change notification settings - Fork 11
/
GenDataMP4.cc
82 lines (61 loc) · 1.66 KB
/
GenDataMP4.cc
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// GenDataMP1.cpp: Generate data for assignment MP1
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
typedef std::vector< float > FloatVec;
float genRandomFloat()
{
return ( (float) rand() / RAND_MAX );
}
void genVector( FloatVec& vec, int vecLen )
{
for ( int i = 0; i < vecLen; ++i )
vec.push_back( genRandomFloat() );
}
void sumVector( const FloatVec& vecA,FloatVec &vecB)
{
vecB.clear();
float sum=0;
for ( int i = 0; i < (int) vecA.size(); ++i )
sum +=vecA[i] ;
vecB.push_back(sum);
}
void writeVector( const FloatVec& vec, const char* fname )
{
std::ofstream outFile( fname );
if ( !outFile )
{
std::cout << "Error! Opening file: " << fname << " for writing vector.\n";
exit(1);
}
std::cout << "Writing vector to file: " << fname << std::endl;
const int vecLen = (int) vec.size();
outFile << vecLen << std::endl;
for ( int i = 0; i < vecLen; ++i )
outFile << vec[i] << std::endl;
}
int main( int argc, const char** argv )
{
// Info for user
std::cout << "GenDataMP4: Generates data files to use as input for assignment MP.\n";
std::cout << "Invoke as: " << argv[0] << " [VectorLength]\n\n";
// Read input
if ( 2 != argc )
{
std::cout << "Error! Wrong number of arguments to program.\n";
return 0;
}
// Create vectors
const int vecLen = atoi( argv[1] );
FloatVec vecA;
FloatVec vecB;
genVector(vecA, vecLen);
sumVector(vecA, vecB);
// Write to files
writeVector( vecA, "vecSumA.txt" );
writeVector( vecB, "vecSumResult.txt" );
return 0;
}