-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleApplication3.cpp
118 lines (113 loc) · 4.87 KB
/
ConsoleApplication3.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
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
#include "pch.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <string>
#include <sstream>
//I think the assigment was to transpose the matrice given in a file. I don't remember if this is the right solution.
//I had a solution that worked for few cases and a general one. This was week 9.
using namespace std;
int main()
{
// open the input file as an input file stream (ifstream)
ifstream dataFile("xys_data.txt");
// dynamic arrays made for storing the matrices
double *matrix;
double *transpose_matrix;
int width = 0;
int height = 0;
int k = 0;
// if the file is open...
if (dataFile.is_open())
{
// and while it's not the end of the file
while (!dataFile.eof())
{
string Line;
getline(dataFile, Line);
stringstream line(Line);
double i;
if (height == 0)
{
while (line >> i)
{
if (line.peek() == ',')
{
line.ignore();
}
width++;
}
}
height++;
}
matrix = new double[width*height];
transpose_matrix = new double[width*height];
dataFile.clear();
dataFile.seekg(0, dataFile.beg);
while (!dataFile.eof())
{
string Line;
getline(dataFile, Line);
stringstream line(Line);
double i;
while (line >> i)
{
matrix[k] = i;
if (line.peek() == ',' || '\n')
{
line.ignore();
}
k++;
}
}
// now at the file end, close it
dataFile.close();
}
else
{
// if the file couldn't be opened, write a message and quit
cout << "Unable to open file\n";
exit(1);
}
for (int n = 0; n < width*height; n++) {
int i = n / height;
int j = n % height;
transpose_matrix[n] = matrix[width*j + i];
}
//transposing
ofstream outFile("transposed_matrix.txt");
if (outFile.is_open())
{
for (int r = 1; r <= width * height; r++)
{
cout << width << endl;
cout << height << endl;
outFile << transpose_matrix[r - 1];
cout << transpose_matrix[r - 1] << endl;
cout << r << endl;
if (r % height == 0)
{
outFile << endl;
continue;
}
if (r == width * height)
{
continue;
}
outFile << ",";
}
// now at the file end, close it
outFile.close();
}
else
{
// if the file couldn't be opened, write a message and quit
cout << "Unable to open file for writing!\n";
exit(1);
}
//deleting pointers
delete[] matrix;
delete[] transpose_matrix;
return 0;
}