-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix-Project.cpp
347 lines (202 loc) · 8.07 KB
/
Matrix-Project.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <iostream>
#include <ctime>//in order to randomize the sizes and the values of the matrixes
#include <fstream>//in order to Read and Write to a file
#include <string>//in order to use the getline function
using namespace std;
//*******************************************************************************************************************************
//************* STUDENT NAME: BEYZA KOMis ******************
//************* ASSiGNMENT #: Project 1 ******************
//************* and i got help from some recources ******************
//*******************************************************************************************************************************
void generate(int **B,int **C,int b,int c) {//Creatingg a generate function to generate the B input matrix and the core matrix into the file
ofstream file;//calling the fstream library for output
{ file.open("data.txt", ios::out);//opening the file named data.txtt
if (file.is_open())//Checking if the file open or not.
{
//now time for generating them in the file
file << "input matrix" << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++)
{
file << B[i][j] << " ";
}
file << endl;
}
file << "core matrix" << endl;
for (int i = 0; i < c; i++)
{
for (int j = 0; j < c; j++)
{
file << C[i][j] << " ";
}
file << endl;
}
file.close();//close the file
}}
}
void retrieve() {//creating the retrieve function to store the matrixes from the file.
ifstream file;//calling the fstream libray for input and reading.
file.open("data.txt", ios::in);
char p;//creating a char p to know where my breaking point will start
streampos position;//i used int before this but it was not matching with the tellg return type so i used streampos
while (true)//creating an infinite loop to break when needed
{ //this first while loop is for to know where we stopped.
position = file.tellg();//pinning the begining to work with it later
file >> p;//saving every letter of the file
if (isdigit(p))//UNTiL it reaches a digit
{
break;
}
}
file.clear();//clearing the error flags that might happen
file.seekg(position, ios::beg);//setting it back to where i pinned.
int size = 0;//initializing the size of inputmatrix
string line;//initializing for the get line function to count and read the lines
char q;//again using a char to check and save the charachters in a file
while (true)//this loop is for counting the size of my array
{
file >> q;//again saving every letter to the file
getline(file, line);//this is to read the whole line until it encounters an alphabet more apecifically the start of the title 'core matrix'
if (!(isdigit(q)))
{
break;
}
size++;//and know it counts until the digits have finished
}
file.clear();//clearing the file
file.seekg(position, ios::beg);//setting this to the start of the file before storing it into a new array
int** M = new int* [size * size];//now we know the size and we can use it to store it to a new matrix named M
for (int i = 0; i < size; i++) { M[i] = new int[size]; }//initializing a 2d array dynamically by using new keyword
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
file >> M[i][j];//storing them one by one in the file.
}
}
//AND doing the whole process for the CORE MATRiX
char r;
streampos positioncore;
while (true)
{
positioncore = file.tellg();
file >> r;
if (isdigit(r))
{
break;
}
}
file.clear();
int sizecore = 0;
string lines;
char s;
while (getline(file, lines))
{
file >> s;
sizecore++;
}
file.clear();
file.seekg(positioncore, ios::beg);
int** N = new int* [sizecore * sizecore];
for (int i = 0; i < sizecore; i++) { N[i] = new int[sizecore]; }
for (int i = 0; i < sizecore; i++)
{
for (int j = 0; j < sizecore; j++)
{
file >> N[i][j];
}
}
int outputsize = size - sizecore + 1;//initilizing the size of the output array
int** A = new int* [outputsize];//initializing the outpout array
for (int i = 0; i < outputsize; i++)
{
A[i] = new int[outputsize];
}
cout << "The Output Matrix:" << endl;
for (int i = 0; i <= outputsize-1; i++) {//This for function is to shift the input matrix window
for (int j = 0; j <= outputsize-1; j++) {//i did -1 because i think it wasnt getting the last members or the coloumns
int output = (0);//initiliazing the output matrixes values like this for proper code running
//This for loops is to multiply the matrixes and with the indices correctly put this for loops helps us to reverse the B input matrix as well.
for (int x = 0; x < sizecore; x++) {
for (int y = 0; y < sizecore; y++) {
//Here we are storing every multiplied matrix and summing the value in the output variable
output += N[x][y] * M[sizecore + i - x - 1][j + sizecore - y - 1];
//We're using the '-x' and the '-y' to reverse B matrix and....
//We're using the '+i'and '+j 'in order to shift the window and -1 to access the last element of the output matrix.
}
}
//Here we are storing each summed output variable to the output matrix by dividing it with the number of elements the core matrix consists.
A[i][j] = output / (sizecore * sizecore);
}
}
for (int i = 0; i < outputsize; i++)//This is to print the output matrix.
{
for (int j = 0; j <outputsize; j++)
{
cout << A[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < outputsize; i++) {//Then deleting the output matrix from the heap to free mmemory
delete[] A[i];
}
delete[] A;
A = NULL;
file.close();//Closing the file.
}
int main() {
srand(static_cast<unsigned int>(time(0)));//so i have used srand((time0)) but it was giving me an error and found this block of code that was more efficient to use apparently
int b = 5 + (rand() % (20 - 5 + 1));//randomizing the size of the input matrix B
int c = 2 + (rand() % (5 - 2 + 1));//randomizing the size of the core matrix C
//initializing the B input matrix dynamically
int** B = new int* [b];
for (int i = 0; i < b; i++)
{
B[i] = new int[b];
}
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++)
{
B[i][j] = 10 + (rand() % (20 - 10 + 1));//and randomizing the values inside from 10 to 20
}
}
//initializing the C core matrix dynamically
int** C = new int* [c];
for (int i = 0; i < c; i++)
{
C[i] = new int[c];
}
for (int i = 0; i < c; i++)
{
for (int j = 0; j < c; j++)
{
C[i][j] = 1 + (rand() % (10 - 1 + 1));//and randomizing the values inside from 1 to 10
}
}
//using a switch case to create the menu
int x;//initializing the integer user going to enter
cout << "Press 1-To generate new matrix or press 2-To read the matrixes" << endl;//asking the user
cin >> x;//user entering
switch (x)
{
case 1: generate( B, C, b, c);//if case 1 is chosen its going to generate the wanted matrixes in the file
retrieve();//and this is to print out the output matrix
break;//if case 1 is called its goint to generate the B and the C matrix.
case 2: retrieve(); break;//in here we are retreiving the matrixes with this function whem 2 is selected
default:cout << "Please enter either the integer 1 or 2!" << endl;//and if the user has entered some other variable it breaks right away.
break;
}
// i have read and print the function here regardless of everything so we can see the input and the core matrix
ifstream file;
file.open("data.txt", ios::in);
string line;
while (getline(file, line))
{
cout << line << endl;
}
file.close();
return 0;
}