**Stay up to date**– embedded code automagically updates, each snippet and article has a feed Join Siafoo Now or Learn More

# Stou's I/O and sorting assignment 0

Language | C++ |

# 's

1/*

2Simple I/O "Assignment"

3

4The "assignment":

5

61. Load a file of arbitrary length that is composed of 3 tab-separated columns

7 with floating point numbers representing x, y, z coordinates of points. Lines

8 starting with the pound character (i.e. #) are comments and your program

9 should ignore them.

10

112. Accept file on the command lines:

12 $ ./a.out some_data.dat

13

143. Use STL sort to sort all data points beyond the first by their distance from

15 the first data point (shortest to longest).

16

174. Output sorted list as x y z distance use only 4 significant figures (output

18 only once we can compare speed and correctness with a script later).

19

205. (bonus) Implement your own sorting method and compare to STL method.

21

226. (bonus) Try some of the Boost libraries

23

247. (bonus) Accept command line arguments to pick algorithm, sort order, data

25 points. Try Boost Program Options

26

27C++ suggestions:

28

291. Write a C++ class (or struct) that represents a vector (you can templatize it

30 on a constant to make it N dimensional yet relatively efficient)

31

322. Overload >> input operator to read things from the file in a more idiomatic

33 way

34

353. Overload << output operator to print things from the file in a more idiomatic

36 way

37

384. Use an STL container to store loaded vectors. You can probably pull it off by

39 dynamically growing your own arrays, or maybe reading the file twice but the

40 STL stuff will work better and it's easier.

41

42Warning

43 C++ has a datatype called vector, this is a dynamic list not a mathematical

44 vector.

45

465. Look at the pow<N>(x) routine in the boost math library (under

47 special_functions).

48

49Oh and I suggest sticking to the standard libraries (STL and Python)... if you

50decide to use stuff from Boost and numpy it would probably be good to implement

51your own too and compare them.

52

53*/

54#include <iostream>

55#include <fstream>

56#include <sstream>

57#include <vector>

58#include <cmath>

59using namespace std;

60

61struct Point

62{

63 float x, y, z, r;

64 int use_default;

65};

66

67struct dist_compare

68{

69 bool operator() (Point const& a, Point const& b) {

70 return a.r < b.r;

71 }

72};

73

74int set_options(int argc, char* argv[], Point& p0)

75{

76 vector<string> args (argv + 2, argv + argc);

77

78 p0.use_default = 1;

79 for (int i = 0; i < args.size(); i++) {

80 if (args[i] == "-xyz") {

81 istringstream sstream (args[i+1] + " " + args[i+2] + " " + args[i+3]);

82 sstream >> p0.x;

83 sstream >> p0.y;

84 sstream >> p0.z;

85 p0.use_default = 0;

86 }

87 }

88

89 return 0;

90}

91

92int get_coords(ifstream &fstream, vector<Point>& p_list)

93{/*

94 Build up a vector (of point objects) from x,y,z values in the given file.

95 Empty lines and lines starting with # are ignored.

96

97 */

98 float val;

99 string line;

100 Point p;

101

102 while (fstream.good()) {

103 getline(fstream, line);

104

105 // Skip empty lines or lines that start with #

106 if (line == "" || line[0] == '#') {

107 continue;

108 }

109

110 // Get floats from the line string, make the point, add to vector

111 istringstream sstream (line);

112 sstream >> p.x;

113 sstream >> p.y;

114 sstream >> p.z;

115

116 p_list.push_back(p);

117 }

118

119 return 0;

120}

121

122int calc_dist(vector<Point>& p_list, Point& p0)

123{

124 if (p0.use_default == 1) {

125 p0.x = p_list[0].x;

126 p0.y = p_list[0].y;

127 p0.z = p_list[0].z;

128 }

129

130 for (int i = 0; i < p_list.size(); i++) {

131 p_list[i].r = sqrt(pow(p_list[i].x - p0.x, 2) +

132 pow(p_list[i].y - p0.y, 2) +

133 pow(p_list[i].z - p0.z, 2));

134 }

135

136 return 0;

137}

138

139int dist_sort(vector<Point>& p_list)

140{

141 sort(p_list.begin(), p_list.end(), dist_compare());

142

143 return 0;

144}

145

146int pretty_print(vector<Point>& p_list)

147{

148 for (int i = 0; i < p_list.size(); i++) {

149 printf("%.4f\t%.4f\t%.4f\t%.4f\n",

150 p_list[i].x, p_list[i].y, p_list[i].z, p_list[i].r);

151 }

152}

153

154int main (int argc, char* argv[])

155{

156 string filename = argv[1];

157

158 Point p0;

159 set_options(argc, argv, p0);

160

161 // Build the point list

162 vector<Point> p_list;

163 ifstream fstream (filename.c_str());

164 get_coords(fstream, p_list);

165 fstream.close();

166

167 // Calculate distances

168 calc_dist(p_list, p0);

169

170 // Sort the points based on distances

171 dist_sort(p_list);

172

173 // Print the point coords and distances

174 pretty_print(p_list);

175

176 return 0;

177}

## Comments