Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void beolvas(int *mx, int sor, int oszlop);
- void kiir(int *mx, int sor, int oszlop);
- int *osszead(int *mx1, int *mx2, int sor, int oszlop);
- int *osszeszoroz(int *mx1 , int *mx2, int sor1, int oszlop1, int oszlop2);
- //main fuggveny
- int main(void){
- int t1[2][3]={ 1, 2, 3,
- 4, 5, 6 };
- int t2[2][3];
- int t3[3][4]={ 1, 1, 1, 1,
- 2, 2, 2, 2,
- 3, 3, 3, 3 };
- int *eredmeny;
- int sor1=2;
- int sor2=2;
- int sor3=3;
- int oszlop1=3;
- int oszlop2=3;
- int oszlop3=4;
- printf("Az elso matrix: \n");
- kiir(t1, sor1, oszlop1);
- printf("A masodik matrix : \n");
- beolvas(t2, sor2, oszlop2);
- kiir(t2, sor2, oszlop2);
- if(sor1==sor2 && oszlop1==oszlop2)
- eredmeny=osszead(t1, t2, sor1, oszlop1);
- printf("A ket matrix osszeadva: \n");
- kiir(eredmeny, sor1, oszlop1);
- free(eredmeny);
- if(oszlop2==sor3)
- eredmeny=osszeszoroz(t2, t3, sor2, oszlop2, oszlop3);
- printf("A ket matrix osszeszorozva: \n");
- kiir(eredmeny, sor2, oszlop3);
- free(eredmeny);
- return 0;
- }
- void beolvas(int *mx, int sor, int oszlop){
- char inbuf[100];
- char ch;
- int i;
- int j;
- int k;
- int inbufm = 100;
- int jo;
- for(i=0; i<sor; i++){
- for(j=0; j<oszlop; j++){
- jo=1;
- do{
- jo=1;
- printf("%d. sor %d. oszlop eleme: ", i+1, j+1);
- scanf("%s", inbuf);
- if(sscanf(inbuf, "%d", &mx[i*oszlop+j]) !=1){
- printf("Hibas adat!\n");
- jo=0;
- }
- }while(!jo);
- }
- }
- return ;
- }
- void kiir(int *mx, int sor, int oszlop){
- int i;
- int j;
- for(i=0; i<sor; i++){
- for(j=0; j<oszlop; j++){
- printf("%d. sor %d. oszlop eleme: %d\n", i+1, j+1, mx[i*oszlop+j]);
- }
- }
- return ;
- }
- int * osszead(int *mx1, int *mx2, int sor, int oszlop){
- int i;
- int j;
- int *e;
- e=(int *)malloc(sor*oszlop*sizeof(int));
- if(e==NULL){
- printf("Nincs eleg memoria!\n");
- exit(EXIT_FAILURE);
- }
- for(i=0; i<sor; i++){
- for(j=0; j<oszlop; j++){
- e[i*oszlop+j] = mx1[i*oszlop+j]+mx2[i*oszlop+j];
- }
- }
- return e;
- }
- int *osszeszoroz(int *mx1 , int *mx2, int sor1, int oszlop1, int oszlop2){
- int i;
- int j;
- int k;
- int sum;
- int *e;
- e=calloc(sor1*oszlop2, sizeof(int));
- if(e==NULL){
- printf("Nincs eleg memoria!\n");
- exit(EXIT_FAILURE);
- }
- for(i=0; i<sor1; i++){
- for(j=0; j<oszlop2; j++){
- sum=0;
- for(k=0; k<oszlop1; k++){
- sum += mx1[i*oszlop1+k] * mx2[i*oszlop2+j];
- }
- e[i*oszlop2+j] = sum;
- }
- }
- return e;
- }
Advertisement
Add Comment
Please, Sign In to add comment