ProgIng - Programación en Ingeniería
Loading...
Searching...
No Matches
Ejemplo039.c File Reference
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for Ejemplo039.c:

Go to the source code of this file.

Functions

double * crearMat (int r, int c)
int capturar (double *M, int r, int c, char *str)
int imprimir (double *M, int r, int c)
double * multiplicar (double *M1, int r1, int c1, double *M2, int r2, int c2)
int main (int argc, char *argv[])

Function Documentation

◆ capturar()

int capturar ( double * M,
int r,
int c,
char * str )

Definition at line 13 of file Ejemplo039.c.

14{
15 int i, j;
16 for(i=0; i<r; i++)
17 for(j=0; j<c; j++)
18 {
19 printf("%s[%d][%d] = ", str, i+1, j+1);
20 scanf("%lf", M+c*i+j);
21 }
22 return 0;
23}
#define M
Here is the caller graph for this function:

◆ crearMat()

double * crearMat ( int r,
int c )

Definition at line 4 of file Ejemplo039.c.

5{
6 double *M;
7 M = (double*)malloc(r*c*sizeof(double));
8 if(M==NULL)
9 return NULL;
10 return M;
11}
Here is the caller graph for this function:

◆ imprimir()

int imprimir ( double * M,
int r,
int c )

Definition at line 25 of file Ejemplo039.c.

26{
27 int i, j;
28 for(i=0; i<r; i++)
29 {
30 for(j=0; j<c; j++)
31 printf("%.4lf\t", M[c*i+j]);
32 printf("\n");
33 }
34 return 0;
35}
Here is the caller graph for this function:

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 59 of file Ejemplo039.c.

60{
61 int rA, rB, cA, cB;
62 double *A, *B, *C;
63 do{
64 printf("Ingrese el numero de renglones de A: ");
65 scanf("%d", &rA);
66 }while(rA<1);
67 do{
68 printf("Ingrese el numero de columnas de A: ");
69 scanf("%d", &cA);
70 }while(cA<1);
71 A = crearMat(rA, cA);
72 if(A==NULL)
73 return 1;
74 capturar(A, rA, cA, "A");
75 do{
76 printf("Ingrese el numero de renglones de B: ");
77 scanf("%d", &rB);
78 }while(rB<1);
79 do{
80 printf("Ingrese el numero de columnas de B: ");
81 scanf("%d", &cB);
82 }while(cB<1);
83 B = crearMat(rB, cB);
84 if(B==NULL)
85 {
86 free(A);
87 return 2;
88 }
89 capturar(B, rB, cB, "B");
90 C = multiplicar(A, rA, cA, B, rB, cB);
91 if(C==NULL)
92 {
93 free(A);
94 free(B);
95 return 4;
96 }
97 printf("A\n");
98 imprimir(A, rA, cA);
99 printf("B\n");
100 imprimir(B, rB, cB);
101 printf("C\n");
102 imprimir(C, rA, cB);
103 free(A);
104 free(B);
105 free(C);
106 return 0;
107}
int capturar(double *M, int r, int c, char *str)
Definition Ejemplo039.c:13
double * crearMat(int r, int c)
Definition Ejemplo039.c:4
double * multiplicar(double *M1, int r1, int c1, double *M2, int r2, int c2)
Definition Ejemplo039.c:37
int imprimir(double *M, int r, int c)
Definition Ejemplo039.c:25
Here is the call graph for this function:

◆ multiplicar()

double * multiplicar ( double * M1,
int r1,
int c1,
double * M2,
int r2,
int c2 )

Definition at line 37 of file Ejemplo039.c.

38{
39 int i, j, k;
40 double *M3;
41 if(c1!=r2)
42 {
43 printf("Error: En el tamaño de las matrices.\n");
44 return NULL;
45 }
46 M3 = crearMat(r1, c2);
47 if(M3==NULL)
48 return NULL;
49 for(i=0; i<r1; i++)
50 for(j=0; j<c2; j++)
51 for(k=0, M3[i*c1+j]=0; k<c1; k++)
52 M3[i*c2+j] += M1[i*c1+k]*M2[k*c2+j];
53// M3[i][j] = M3[i][j]+M1[i][k]*M2[k][j]
54// M3[i][j] += M1[i][k]*M2[k][j]
55// M3[i*c2+j] += M1[i*c1+k]*M2[k*r2+j]
56 return M3;
57}
Here is the call graph for this function:
Here is the caller graph for this function: