-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathMatrizstring
More file actions
70 lines (58 loc) · 1.75 KB
/
Matrizstring
File metadata and controls
70 lines (58 loc) · 1.75 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
#include <iostream>
#include <string>
#define MAX 20
using namespace std;
// Prototipos de funciones
void cargarMatriz(string v[MAX][MAX], int n, int m, string palabra, int aux);
void mostrarMatriz(string v[MAX][MAX], int n, int m);
int main() {
string v[MAX][MAX];
string palabra;
int n, m, aux = 0;
// Validación del número de filas
do {
cout << "Por favor, ingrese el número de filas: ";
cin >> n;
if (n <= 0 || n > 20)
cout << "ERROR. Ingresar de nuevo." << endl;
} while (n <= 0 || n > 20);
// Validación del número de columnas
do {
cout << "Por favor, ingrese el número de columnas: ";
cin >> m;
if (m <= 0 || m > 20)
cout << "ERROR. Ingresar de nuevo." << endl;
} while (m <= 0 || m > 20);
// Limpiar buffer de entrada
cin.ignore();
// Ingreso de la palabra
cout << "Por favor, ingrese la respectiva palabra: ";
getline(cin, palabra);
// Llamada a funciones
cargarMatriz(v, n, m, palabra, aux);
mostrarMatriz(v, n, m);
return 0;
}
// Carga la matriz con la palabra repetida en cada fila
void cargarMatriz(string v[MAX][MAX], int n, int m, string palabra, int aux) {
for (int i = 0; i < n; i++) { // filas
for (int j = 0; j < m; j++) { // columnas
if (aux < palabra.length()) {
v[i][j] = palabra.at(aux);
aux++;
} else {
v[i][j] = " ";
}
}
}
}
// Muestra la matriz
void mostrarMatriz(string v[MAX][MAX], int n, int m) {
cout << "\nMatriz resultante:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << v[i][j] << " ";
}
cout << endl;
}
}