Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void dodaj(int **mac1, int **mac2, int x, int y)
- {
- int i,j,**macw = (int*) malloc (sizeof(int)*x);
- for (i=0;i<x;i++)
- {
- macw[i]=(int*) malloc (sizeof(int)*y);
- }
- for (i=0;i<x;i++)
- {
- for (j=0;j<y;j++)
- {
- macw[i][j]=mac1[i][j]+mac2[i][j];
- }
- }
- for (i=0;i<x;i++)
- {
- for (j=0;j<y;j++)
- {
- printf("%d ",macw[i][j]);
- }
- printf("\n");
- }
- for (i=0;i<x;i++)
- {free(*macw[i]);}
- }
- void odejm(int **mac1, int **mac2, int x, int y)
- {
- int i,j,**macw = (int*) malloc (sizeof(int)*x);
- for (i=0;i<x;i++)
- {
- macw[i]=(int*) malloc (sizeof(int)*y);
- }
- for (i=0;i<x;i++)
- {
- for (j=0;j<y;j++)
- {
- macw[i][j]=mac1[i][j]-mac2[i][j];
- }
- }
- for (i=0;i<x;i++)
- {
- for (j=0;j<y;j++)
- {
- printf("%d ",macw[i][j]);
- }
- printf("\n");
- }
- for (i=0;i<x;i++)
- {free(*macw[i]);}
- }
- void mnoz(int **mac1, int **mac2, int x, int y, int x2, int y2)
- {
- int i,j,k,**macw = (int*) malloc (sizeof(int)*x);
- for (i=0;i<x;i++)
- {
- macw[i]=(int*) malloc (sizeof(int)*y2);
- }
- for (i=0;i<x;i++)
- {
- for (j=0;j<y2;j++)
- {
- macw[i][j]=0;
- for (k=0;k<y;k++)
- {
- macw[i][j]=macw[i][j]+(mac1[i][k]*mac2[k][j]);
- }
- }
- }
- for (i=0;i<x;i++)
- {
- for (j=0;j<y2;j++)
- {
- printf("%d ",macw[i][j]);
- }
- printf("\n");
- }
- //for (i=0;i<x;i++)
- //{free(*macw[i]);}
- }
- void transp(int **mac1, int x, int y)
- {
- int i,j,**macw = (int*) malloc (sizeof(int)*y);
- printf("przed:\n");
- for (i=0;i<x;i++)
- {
- for (j=0;j<y;j++)
- {
- printf("%d ",mac1[i][j]);
- }
- printf("\n");
- }
- for (i=0;i<y;i++)
- {
- macw[i]=(int*) malloc (sizeof(int)*x);
- }
- for (i=0;i<y;i++)
- {
- for (j=0;j<x;j++)
- {
- macw[i][j]=mac1[j][i];
- }
- }
- **mac1=**macw;
- printf("\npo:\n");
- for (i=0;i<y;i++)
- {
- for (j=0;j<x;j++)
- {
- printf("%d ",macw[i][j]);
- }
- printf("\n");
- }
- /*for (i=0;i<x;i++)
- {free(*macw[i]);}*/
- }
- int main()
- {
- int x,x2,y,y2,i,j,d;
- printf("Podaj rozmiary maciezy #1. \nX = ");
- scanf("%d",&x);
- printf("Y = ");
- scanf("%d",&y);
- int **mac1 = (int*) malloc (sizeof(int)*x);
- for (i=0;i<x;i++)
- {
- mac1[i]=(int*) malloc (sizeof(int)*y);
- }
- for (i=0;i<x;i++)
- {
- for (j=0;j<y;j++)
- {
- printf("podaj wartosc %dx%d : ",i+1,j+1);
- scanf("%d",&
- mac1[i][j]);
- }
- }
- printf("Podaj rozmiary maciezy #2. \nX = ");
- scanf("%d",&x2);
- printf("Y = ");
- scanf("%d",&y2);
- int **mac2 = (int*) malloc (sizeof(int)*x2);
- for (i=0;i<x2;i++)
- {
- mac2[i]=(int*) malloc (sizeof(int)*y2);
- }
- for (i=0;i<x2;i++)
- {
- for (j=0;j<y2;j++)
- {
- printf("podaj wartosc %dx%d : ",i+1,j+1);
- scanf("%d",&mac2[i][j]);
- }
- }
- if ((x==x2) && (y==y2)) {printf("1-dodaj je\n2-odejmij macierze #1-#2\n");}
- if (y==x2) {printf("3-pomnoz macierz #1 * #2\n");}
- printf("4-Transponuj macierz nr 1\n");
- printf("decyzja: ");
- scanf("%d",&d);
- switch(d)
- {
- case 1:
- dodaj(mac1,mac2,x,y);
- break;
- case 2:
- odejm(mac1,mac2,x,y);
- break;
- case 3:
- mnoz(mac1,mac2,x,y,x2,y2);
- break;
- case 4:
- transp(mac1,x,y);
- break;
- default:break;}
- for (i=0;i<x;i++)
- {free(*mac1[i]);}
- for (i=0;i<x2;i++)
- {free(*mac2[i]);}
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement