Repository navigation
Expand file tree
/
Copy pathauxiliary_functions.cpp
More file actions
133 lines (115 loc) · 3.38 KB
/
Copy pathauxiliary_functions.cpp
File metadata and controls
133 lines (115 loc) · 3.38 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* This file contains functions that can be used by
* the sorts that we will implement.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <math.h>
using namespace std;
/* Returns true if the arguments given to program are correct.
* Returns false is arguments are incorrect.
* Arguments should be <input_file> <output_file>
* Where <input_file> is a file containing numbers to be sorted.
* Where <output_file> is a file where the sorted output should be placed.
*/
bool correctArgumentFormat(int argc, char* argv[]);
/* Can be called by sort functions to read the input file.
* Stores input in int vector, buffer.
* returns 0 if operation completes successfully.
* returns 1 if operation fails for any reason
* Argument should be <input_file> <buffer>
* Where <input_file> contains the numbers to be sorted.
* Where <buffer> is an empty int vector
* readInputFile modifies <buffer> and stores the input inside <buffer>
*
*/
int readInputFile(string file_name, std::vector<int>&buffer);
/* Can be called by sort functions to output sorted numbers to file.
* Reads output from int vector, buffer.
* returns 0 if operation completes successfully.
* returns 1 if operation fails for any reason
* Argument should be <output_file> <buffer>
* Where <output_file> is the file where numbers should be output
* Where <buffer> contains sorted numbers to be output
* outputToFile reads from <buffer> and outputs its content to file_name
*
*/
int outputToFile(string file_name, std::vector<int>&buffer);
/* Using an float vector of runtimes for any given sort, calculate the
* average time for that set of runs.
*/
void calcAvgTime(vector<float>& times);
void calcAvgTime(vector<float>& times)
{
float sum = 0;
for(u_int i = 0; i < times.size(); i++)
{
sum += times[i];
}
cout << "Number of runs: " << times.size() << endl;
cout << "Average time: " << sum/ times.size() << endl;
cout << endl;
}
int outputToFile(string file_name, std::vector<int>&buffer){
ofstream output;
string output_file = file_name;
output.open(output_file);
if(!output.is_open()){return 0;}
for(int i = 0; i < (int) buffer.size(); i++)
{
output << buffer[i] << endl;
}
output.close();
return 0;
}
int readInputFile(string file_name, std::vector<int>& buffer){
string input_file = file_name;
ifstream input;
input.open(input_file);
//input_file failed to open for any reason
if(!input.is_open()){return 1;}
int eachNumber;
while(input >> eachNumber)
{
buffer.push_back(eachNumber);
}
input.close();
return 0;
}
bool correctArgumentFormat(int argc, char* argv[]){
int NUM_CORRECT_ARGUMENTS = 3;
string USAGE_MESSAGE = "USAGE: <input-file> <output-file>";
if(argc != NUM_CORRECT_ARGUMENTS)
{
cout<<"Expected "<<NUM_CORRECT_ARGUMENTS<<". Received "<<argc<<" arguments."<< endl;
cout << USAGE_MESSAGE << endl;
return false;
}
stringstream ss;
ss << argv[1];
string inputFile;
ss >> inputFile;
if(ss.fail())
{
cout << "Error. input-file must be a string." << endl;
return false;
}
ifstream input;
input.open(inputFile);
if(!input.is_open())
{
cout << "Error. input-file must exist." << endl;
return false;
}
ss.clear();
ss << argv[2];
string outputFile;
ss >> outputFile;
if(ss.fail())
{
cout << "Error. output-file must be a string." << endl;
return false;
}
return true;
}