Created
November 29, 2012 17:03
-
-
Save CaglarGonul/4170409 to your computer and use it in GitHub Desktop.
Structured Matrix Summation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct { | |
int SatirSayisi; | |
int KolonSayisi; | |
int *dizi; // matris elemanlari satir-majör sirada bu dizide | |
} Matris; | |
Matris *yapMatris(int nsatir, int nkolon, int *d) { | |
Matris *temp= (Matris *)malloc(sizeof(Matris)); | |
temp->SatirSayisi = nsatir; | |
temp->KolonSayisi = nkolon; | |
temp->dizi = (int *)malloc(sizeof(int)*(nsatir*nkolon)); | |
int i,j; | |
for(i=0;i<nsatir;i++){ | |
for(j=0;j<nkolon;j++){ | |
temp->dizi[i*nkolon + j] = d[i*nkolon + j]; | |
} | |
} | |
return temp; | |
} | |
void silMatris(Matris *M) { | |
free(M->dizi); | |
free(M); | |
} | |
Matris *toplaMatris(Matris *A, Matris *B) { | |
if(A->SatirSayisi != B ->SatirSayisi) { | |
printf("satir sayilari ayni degil\n"); | |
return 0; | |
} | |
if(A->KolonSayisi != B ->KolonSayisi) { | |
printf("Kolon sayilari ayni degil\n"); | |
return 0; | |
} | |
Matris *temp = (Matris *)malloc(sizeof(Matris)); | |
int nsatir = A->SatirSayisi; | |
int nkolon = A->KolonSayisi; | |
temp->dizi = (int *)malloc(sizeof(int)*(nsatir*nkolon)); | |
temp->SatirSayisi = nsatir; | |
temp->KolonSayisi = nkolon; | |
int i,j; | |
for(i=0;i<nsatir;i++){ | |
for(j=0;j<nkolon;j++){ | |
temp -> dizi[i*nkolon + j] = A -> dizi[i*nkolon + j] + B -> dizi[i*nkolon + j]; | |
} | |
} | |
return temp; | |
} | |
void yazMatris(Matris *M) { | |
int i,j; | |
for(i=0;i< M->SatirSayisi;i++) { | |
for(j=0;j< M->KolonSayisi;j++) | |
printf("%3d ", M->dizi[i*M->KolonSayisi + j]); | |
printf("\n"); | |
} | |
printf("\n"); | |
} | |
int main() { | |
Matris *M1, *M2, *M3; | |
int d1[]= {1,2,3,4,5,6,7,8}; | |
int d2[]= {7,6,5,10,12,12,23,26}; | |
M1= yapMatris(2,4,d1); | |
M2= yapMatris(2,4,d2); | |
yazMatris(M1); | |
yazMatris(M2); | |
M3= toplaMatris(M1, M2); | |
yazMatris(M3); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment