Zdravim tento program me funguje na ctvercove matice, kdyz zadam ale treba matici
A:
1 -1
2 0
-3 4
B:
1 2
3 4
#include <stdio.h>
#include <stdlib.h>
double **vytvor(int,int);
void nastav(int *,int *,char);
void smazat(double **,int);
double **nacti(double **,int, int,char);
double **soucin(double **,double **,int,int, int);
void vypis(double **,int,int,char);
int main()
{
int ax,ay,bx,by,cx,cy;
int n;
double **a,**b,**c;
nastav(&ax,&ay,'A');
nastav(&bx,&by,'B');
if(ay == bx)
{
n = ay;
cx = ax;
cy = by;
}
else
{
printf("!!! Pocet sloupcu matice A se musi rovnat poctu radkum matice B\n");
return 1;
}
a = vytvor(ax,ay);
b = vytvor(bx,by);
nacti(a,ax,ay,'A');
nacti(b,bx,by,'B');
c = soucin(a,b,n,cx,cy);
vypis(c,cx,cy,'C');
smazat(a,ay);
smazat(b,by);
smazat(c,cy);
return 0;
}
double **vytvor(int x,int y)
{
double **mat;
int i;
mat = (double**)malloc(x * sizeof(double *));
for(i = 0;i < y; i++)
mat[i] = (double*)malloc(y * sizeof(double));
return mat;
}
void nastav(int *x,int *y,char nazev)
{
printf("Zadej pocet radku matice %c: ",nazev);
scanf("%d",x);
printf("Zadej pocet sloupcu matice %c: ",nazev);
scanf("%d",y);
}
void smazat(double **mat,int y)
{
int i;
for(i = 0;i < y; i++)
free(mat[i]);
free(mat);
}
double **nacti(double **mat,int x, int y,char nazev)
{
int i,j;
printf("Zadej matici %c o rozmeru %d x %d:\n",nazev,x,y);
for(i = 0;i < x;i++)
{
for(j = 0;j < y;j++)
{
printf("A[%d][%d] = ",i+1,j+1);
scanf("%lf",&mat[i][j]);
}
printf("\n");
}
return vytvor(x,y);
}
double **soucin(double **a,double **b,int n,int x, int y)
{
double **c;
c = vytvor(x,y);
int i,j,k;
for(i = 0;i < x;i++)
for(j = 0;j < n;j++)
{
c[i][j] = 0;
for(k = 0;k < n;k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
return c;
}
void vypis(double **mat,int x,int y,char nazev)
{
int i,j;
printf("Matice %c:\n",nazev);
for(i = 0;i < x;i++)
{
for(j = 0;j < y;j++)
printf("%5.2lf ",nazev,i+1,j+1,mat[i][j]);
printf("\n");
}
}