/* Stefan Miklosovic AI6
zadanie 3 - nasobenie matic
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int xa, ya, xb, yb, i, j, m;
int **pole_a, **pole_b, **pole_c;
do {
printf("Riadkov matice A: ");
scanf("%i", &xa);
printf("Stlpcov matice A: ");
scanf("%i", &ya);
printf("Riadkov matice B: ");
scanf("%i", &xb);
printf("Stlpcov matice B: ");
scanf("%i", &yb);
if (ya != xb) { printf("Zadane zle rozmery matic na sucin!\n\n"); }
}
while (ya != xb);
pole_a = (int**) malloc(xa * sizeof(int*)); //polia dynamicky
pole_b = (int**) malloc(xb * sizeof(int*));
for (i = 0; i < xa; ++i) {
pole_a[i] = (int*) malloc(ya * sizeof(int));
}
for (i = 0; i < xb; ++i) {
pole_b[i] = (int*) malloc(yb * sizeof(int));
}
for (i = 0; i < xa; ++i) { //nacitanie hodnot do matice pole_a
for(j = 0; j < ya; ++j) {
printf("A(%i,%i) = ", i+1, j+1);
scanf("%i", &pole_a[i][j]);
}
}
printf("\nMatica A\n\n");
for (i = 0; i < xa; ++i) { //vypis matice A
printf(" ");
for (j = 0; j < ya; ++j) {
printf("%3i ", pole_a[i][j]);
}
printf("\n\n");
}
printf("\n");
for (i = 0; i < ya; ++i) { //nacitanie hodnot do matice pole_b
for (j = 0; j < yb; ++j) {
printf("B(%i,%i) = ", i+1, j+1);
scanf("%i", &pole_b[i][j]);
}
}
printf("\nMatica B\n\n");
for (i = 0; i < xb; ++i) { //vypis matice B
printf(" ");
for (j = 0; j < yb; ++j) {
printf("%3i ", pole_b[i][j]);
}
printf("\n\n");
}
pole_c = (int**) malloc(xa * sizeof(int*)); //vytvorenie a vynulovanie matice pole_c
for (i = 0; i < xa; i++) {
pole_c[i] = (int*) malloc(yb * sizeof(int));
for (j = 0; j < yb; ++j) {
pole_c[i][j] = 0;
}
}
for (i = 0; i < xa; i++) { //vynasobenie
for (j = 0; j < yb; j++) {
if (ya == 1) {
for (m = 0; m < 1; ++m) { //yb-1 => ak sa rozmer rovna (k*1)x(1*k)
pole_c[i][j] += (pole_a[i][m] * pole_b[m][j]);
}
}
else {
for (m = 0; m <= xa; ++m) {
pole_c[i][j] += (pole_a[i][m] * pole_b[m][j]);
}
}
}
}
printf("\nMatica AxB\n");
for(i = 0; i < xa; i++) { //vypis matice pole_c
printf("\n ");
for(j = 0; j < yb; j++) {
printf("%3i ", pole_c[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
segmentation fault
Pre pridávanie komentárov sa musíte prihlásiť.
(int**) malloc(xa * sizeof(int*)) ? (ked to je z cecka a ja chcem c++)
for (m = 0; m < xa; ++m)
ma byt
for (m = 0; m < xa, +mm)
v tej prvej potom ides mimo pamate
dakujem a prajem pekny den :))
este, asi nie je spravny styl, deklarovat to pole malloc-m, ked som v c++, aj tie scanf a printf by sa mali nahradzovat cin a cout... a teda malloc "new"... je to tak pre programatorsky styl lepsie ?