Como seria este programa con apuntadores?
Publicado por Horacio E. (8 intervenciones) el 16/04/2015 21:46:48
Hola, estuve trabajando en un programa cuyo codigo esta mas abajo, pero me preguntaba como puedo hacerlo usando apuntadores, ya que recien empiezo a trabajar con apuntadores y se me dificulta un poco. Gracias de antemano por la ayuda.
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
#include <iostream>
using namespace std;
void Lee(float A[][100],int f, int c);
void Suma(float A[][100],float B[][100],int f, int c);
void Resta(float A[][100],float B[][100],int f,int c);
void Producto(float A[][100],float B[][100],int f1,int c2, int c1);
int main(){
float A[100][100],B[100][100];
int f1,c1,f2,c2,opcion;
cout<<"Ingrese el numero de filas y el numero de columnas de la primera matriz"<<endl;
cin>>f1>>c1;
cout<<"Ingrese el numero de filas y el numero de columnas de la segunda matriz"<<endl;
cin>>f2>>c2;
cout<<"Leyendo matriz 1\n";
Lee(A,c1,f1);
cout<<"Leyendo matriz 2\n";
Lee(B,c2,f2);
cout<<"\nIngrese 1 si desea calcular la suma de las dos matrices\n";
cout<<"Ingrese 2 si desea calcular la resta de las dos matrices\n";
cout<<"Ingrese 3 si desea calcular el producto de las dos matrices\n";
cin>>opcion;
switch(opcion){
case 1:
if((f1!=f2) || (c1!=c2))
cout<<"Las matrices no se pueden sumar porque no tienen el mismo numero de filas y columnas\n";
else{
cout<<"El resultado es: \n";
Suma(A,B,c1,f1);
}
break;
case 2:
if((f1!=f2) || (c1!=c2))
cout<<"Las matrices no se pueden restar porque no tienen el mismo numero de filas y columnas\n";
else{
cout<<"El resultado es: \n";
Resta(A,B,c1,f1);
}
break;
case 3:
if(c1!=f2)
cout<<"El numero de columnas de la 1ra matriz debe ser igual al numero de columnas de la segunda matriz para poder calcular el producto\n";
else{
cout<<"El resultado es: \n";
Producto(A,B,f1,c2,c1);
}
cout<<"\n";
break;
default:
cout<<"Opcion no valida\n";
break;
}
system("pause");
return 0;
}
void Lee(float A[][100],int f, int c){
for(int i=0;i<f;i++){
for(int j=0;j<c;j++){
cout<<"M["<<i<<"]["<<j<<"] = ";
cin>>A[i][j];
}
cout<<"\n";
}
for(int i=0;i<f;i++){
for(int j=0;j<c;j++)
cout<<"\t"<<A[i][j]<<"\t";
cout<<endl;
}
}
void Producto(float A[][100],float B[][100],int f1,int c2,int c1){
float C[100][100],acumulador;
for(int i=0;i<f1;i++)
for(int j=0;j<c2;j++){
acumulador=0;
for(int k=0;k<c1;k++){
acumulador+=A[i][k]*B[k][j];
C[i][j]=acumulador;
}
}
for(int i=0;i<f1;i++){
for(int j=0;j<c2;j++)
cout<<"\t"<<C[i][j]<<"\t";
cout<<endl;
}
}
void Suma(float A[][100],float B[][100],int f, int c){
float C[100][100];
for(int i=0;i<f;i++)
for(int j=0;j<c;j++)
C[i][j]=A[i][j]+B[i][j];
for(int i=0;i<f;i++){
for(int j=0;j<c;j++)
cout<<"\t"<<C[i][j]<<"\t";
cout<<endl;
}
}
void Resta(float A[][100],float B[][100],int f, int c){
float C[100][100];
for(int i=0;i<f;i++)
for(int j=0;j<c;j++)
C[i][j]=A[i][j]-B[i][j];
for(int i=0;i<f;i++){
for(int j=0;j<c;j++)
cout<<"\t"<<A[i][j]<<"\t";
cout<<endl;
}
}
Valora esta pregunta


0