-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunciones.cpp
More file actions
402 lines (362 loc) · 12 KB
/
Funciones.cpp
File metadata and controls
402 lines (362 loc) · 12 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//
// Created by Sierpe on 17/05/2022.
//
#include <iomanip>
#include <iostream>
#include <vector>
#include "math.h"
#include "string"
#include <stdlib.h>
#include <time.h>
using namespace std;
#include "Funciones.h"
//Funcion para crear matriz Cuadrada recibiendo un puntero que apunte a otro puntero
void crearMatrizCuad(int **&crearMatriz, int dimensiones) {
crearMatriz = new int*[dimensiones]();
for (auto i =0;i<dimensiones;i++){
crearMatriz[i] = new int[dimensiones];
}
}
//Funcion para crear matriz Cuadrada recibiendo un puntero que apunte a otro puntero
void crearMatrizPropia(int **&crearMatriz, int filas, int columnas) {
crearMatriz = new int*[filas]();
for (auto i =0;i<filas;i++){
crearMatriz[i] = new int[columnas];
}
}
void matrizRandomsCuad(int **&matriz, int dimensiones,int limitesup,int limitinf) {
srand(time(NULL));
for (auto i=0 ; i<dimensiones;i++){
for (int j = 0; j < dimensiones; ++j) {
matriz[i][j]= (limitinf + rand() %(limitesup +1 - limitinf));
}
}
}
void matrizRandomsAsig(int **&matriz, int filas, int columnas,int limitesup,int limitinf) {
srand(time(NULL));
for (auto i=0 ; i<filas;i++){
for (int j = 0; j < columnas; ++j) {
matriz[i][j]= (limitinf + rand() %(limitesup +1 - limitinf));
}
}
}
void matrizRandomsParsCuad(int **&matriz, int dimension ,int limitesup,int limitinf) {
srand(time(NULL));
for (auto i = 0;i<dimension;i++){
for (auto j = 0 ; j<dimension;j++){
int m;
do{
m = ( limitinf + rand() % (limitesup + 1 - limitinf));
} while (m%2 != 0);
matriz[i][j]=m;
}
}
}
int numerorandom(int limitinf,int limitsup){
srand(time(0));
int a = (limitinf + rand() %(limitsup +1 - limitinf));
return a;
}
void matrizRandomsParsAsig(int **&matriz, int filas, int columnas,int limitesup,int limitinf) {
srand(time(NULL));
for (auto i = 0;i<filas;i++){
for (auto j = 0 ; j<columnas;j++){
int m;
do{
m = ( limitinf + rand() % (limitesup + 1 - limitinf));
} while (m%2 != 0);
matriz[i][j]=m;
}
}
}
void matrizRandomsImparsAsig(int **&matriz, int filas, int columnas,int limitesup,int limitinf) {
for (auto i = 0;i<filas;i++){
for (auto j = 0 ; j<columnas;j++){
int m;
do{
m = ( limitinf + rand() % (limitesup + 1 - limitinf));
} while (m%2 == 0);
matriz[i][j]=m;
}
}
}
void matrizRandomsImparsCuad(int **&matriz, int dimension,int limitesup,int limitinf) {
for (auto i = 0;i<dimension;i++){
for (auto j = 0 ; j<dimension;j++){
int m;
do{
m = ( limitinf + rand() % (limitesup + 1 - limitinf));
} while (m%2 == 0);
matriz[i][j]=m;
}
}
}
void matrizRandomsMultiploAsig(int **&matriz, int filas, int columnas,int multiplo,int limitesup,int limitinf) {
for (auto i = 0;i<filas;i++){
for (auto j = 0 ; j<columnas;j++){
int m;
do{
m = ( limitinf + rand() % (limitesup + 1 - limitinf));
} while (m%multiplo != 0);
matriz[i][j]=m;
}
}
}
void matrizRandomsMultiploCuad(int **&matriz, int dimension,int multiplo,int limitesup,int limitinf) {
for (auto i = 0;i<dimension;i++){
for (auto j = 0 ; j<dimension;j++){
int m;
do{
m = ( limitinf + rand() % (limitesup + 1 - limitinf));
} while (m%multiplo != 0);
matriz[i][j]=m;
}
}
}
bool esPrimo(int numero){
bool c = true;
for(auto i =2;i<numero;i++){
if (numero%i == 0){
c = false;
break;
}
}
return c;
}
void matrizRandomsPrimosAsig(int **&matriz, int filas, int columnas,int limitesup,int limitinf) {
for (auto i = 0;i<filas;i++){
for (auto j = 0 ; j<columnas;j++){
int m;
bool c;
do{
m = (limitinf + rand() % (limitesup + 1 -limitinf));
c = esPrimo(m);
} while (c == false);
matriz[i][j]=m;
}
}
}
void matrizRandomsPrimosCuad(int **&matriz, int dimension,int limitesup,int limitinf) {
for (auto i = 0;i<dimension;i++){
for (auto j = 0 ; j<dimension;j++){
int m;
bool c;
do{
m = (limitinf + rand() % (limitesup + 1 -limitinf));
c = esPrimo(m);
} while (c == false);
matriz[i][j]=m;
}
}
}
void imprimirMatricesCuad(int **matriz, int dimension) {
for (auto i = 0; i < dimension;i++){
for(auto j = 0 ;j < dimension;j++){
cout << setw(2)<<matriz[i][j]<<" ";
}
cout<<"\n";
}
}
void imprimirMatricesAsig(int **matriz, int filas, int columnas) {
for (auto i = 0; i < filas;i++){
for(auto j = 0 ;j < columnas;j++){
cout << setw(2)<<matriz[i][j]<<" ";
}
cout<<"\n";
}
}
void PrintMatrizTras(int **matriz, int filas, int columnas){
for (auto i = 0; i <columnas;i++){
for (auto j = 0;j<filas;j++){
cout<<setw(4)<<matriz[i][j]<<" ";
}
cout <<"\n";
}
}
void matrizHaciaAbajo(int **&matriz,int filas){
//Salvamos la ultima fila de la matriz por que va a ser reemplazada por la fila anterior
//Creo un puntero para guardar el array
int *ultimo = matriz[filas-1];
/*
1. Inicializo desde el ultimo indice
Para que de la sensación de que se reemplaza hacia abajo
2. Necesito que la filas tome el valor de la filas anterior
3. No le coloco hasta el cero, por que cuando llegue a indice 0 pedira un indice -1 que no existe
Y dara error
4. Le asigno a la primera fila ( indice 0 ) el valor que he salvado antes en un puntero
Por que se iba borrar al momento de ir reemplazan
*/
for (auto i = filas-1 ;i > 0;i--) {
matriz[i] = matriz[i - 1];
}
matriz[filas - filas] = ultimo;
}
void matrizHaciaArriba(int **&matriz,int filas){
//Salvamos la primera fila de la matriz por que va a ser reemplazada por la fila anterior
//Creo un puntero para guardar el array
/*
1. Inicializo desde el ultimo indice
Para que de la sensación de que se reemplaza hacia arriba
2. Necesito que la fila tome el valor de la fila siguiente
3. No le coloco hasta el ultimo, por que cuando llegue a ultimo indice pedira un ultimo
indice + 1 que no existe , dando error
4. Le asigno a la ultima fila ( ultimo indice ) el valor que he salvado antes en un puntero
Por que se iba borrar al momento de ir reemplazan
*/
int *primero = matriz[filas-filas];
for (auto i = 0; i<filas-1;i++){
matriz[i] = matriz[i + 1];
}
matriz[filas - 1] = primero;
}
void matrizHaciaIzquierdaAsig(int **&matriz,int filas,int columnas){
//Salvamos la primera columna de la matriz por que va a ser reemplazada por la columna siguiente
//Creo un puntero para guardar el dato
for (auto i = 0; i<filas;i++){
int primeracol = matriz[i][columnas- columnas];
for(auto j = 0;j < columnas -1;j++){
matriz[i][j] =matriz[i][j+1];
}
matriz[i][columnas-1] = primeracol;
}
}
void matrizHaciaIzquierdaCuad(int **&matriz,int dimensiones){
//Salvamos la primera columna de la matriz por que va a ser reemplazada por la columna siguiente
//Creo un puntero para guardar el dato
for (auto i = 0; i<dimensiones;i++){
int primeracol = matriz[i][dimensiones- dimensiones];
for(auto j = 0;j < dimensiones -1;j++){
matriz[i][j] =matriz[i][j+1];
}
matriz[i][dimensiones-1] = primeracol;
}
}
void matrizHaciaDerechaCuad(int **&matriz,int dimensiones){
//Salvamos la ultima columna fila de la matriz por que va a ser reemplazada por la columna anterior
//Creo un puntero para guardar el dato
for (auto i = 0; i<dimensiones;i++){
int ultimacol = matriz[i][dimensiones-1];
for(auto j = dimensiones-1;j >0 ;j--){
matriz[i][j] =matriz[i][j-1];
}
matriz[i][dimensiones-dimensiones]= ultimacol;
}
}
void matrizHaciaDerechaAsig(int **&matriz,int filas,int columnas){
//Salvamos la ultima columna fila de la matriz por que va a ser reemplazada por la columna anterior
//Creo un puntero para guardar el dato
for (auto i = 0; i<filas;i++){
int ultimacol = matriz[i][columnas-1];
for(auto j = columnas-1;j >0 ;j--){
matriz[i][j] =matriz[i][j-1];
}
matriz[i][columnas-columnas]= ultimacol;
}
}
void borraMatriz(int **&matriz,int filas){
for (auto i = 0;i<filas;i++){
delete[] matriz[i];
}
delete[]matriz;
matriz = nullptr;
}
void matrizTraspuestaCuad(int **&matriz, int dimension) {
for (auto i = 1;i < dimension;i++){
for (auto j = 0;j<i;j++){
int aux = matriz[i][j];
matriz[i][j] = matriz[j][i];
matriz[j][i]= aux;
}
}
}
//Funciones de Matriz Traspuesta
//VERSION 1
void matrizTraspuestaAsig1(int **&matriz,int **&matriztras2,int filas,int columnas) {
crearMatrizPropia(matriztras2,columnas,filas);
cout<<"Iniciando.."<<endl;
for (auto i = 0 ;i <columnas;i++){
for (int j = 0;j<filas;j++) {
int mod1;
mod1 = matriz[j][i];
matriztras2[i][j]=mod1;
}
}
}
//VERSION 2
void matrizTraspuestaAsig2(int **matriz, int **&matriz2,int filas,int columnas){
matriz2 = new int*[columnas];
for (auto i = 0; i < columnas;i++){
matriz2[i] = new int[filas];
}
for (auto i = 0; i < columnas;i++) {
for (auto j = 0; j <filas;j++){
matriz2[i][j] = matriz[j][i];
}
}
}
void matrizZerosCuad(int **&matriz, int dimension) {
for ( auto i = 0;i <dimension;i++){
for (auto j = 0;j <dimension;j++){
matriz[i][j]= 0;
}
}
}
void matrizZerosAsig(int **&matriz, int filas, int columnas) {
for ( auto i = 0;i <filas;i++){
for (auto j = 0;j <columnas;j++){
matriz[i][j]= 0;
}
}
}
void matrizIdentidadCuadad(int **&matriz,int dimension){
for ( auto i = 0;i <dimension;i++){
for (auto j = 0;j <dimension;j++){
if (i == j){
matriz[i][j]= 1;
}
else
matriz[i][j]=0;
}
}
}
void matrizIdentidadAsig(int **&matriz,int filas,int columnas){
for ( auto i = 0;i <filas;i++){
for (auto j = 0;j <columnas;j++){
if (i == j){
matriz[i][j]= 1;
}
else
matriz[i][j]=0;
}
}
}
void matrizZerosAsigComplete(int **&matriz, int filas, int columnas) {
matriz = new int*[filas];
for (auto i = 0;i<filas;i++){
matriz[i]=new int [columnas];
}
for ( auto i = 0;i <filas;i++){
for (auto j = 0;j <columnas;j++){
matriz[i][j]= 0;
}
}
}
void matrizSimetricaRandom(int **&matriz, int dimension,int limitinf,int limitsup){
srand(time(NULL));
for (auto i=0 ; i<dimension;i++){
for (int j = 0; j < dimension; ++j) {
if (i==j){
matriz[i][j] = (limitinf + rand() %(limitsup +1 - limitinf));
}
else if (i != j and matriz[i][j]==0){
matriz[i][j]= (limitinf + rand() %(limitsup +1 - limitinf));
matriz[j][i]= matriz[i][j];
}
}
}
/*
for(auto i=0;i<dimension;i++){
matriz[i][i] = numerorandom(limitinf,limitsup);
}
*/
}