Skip to content

Instantly share code, notes, and snippets.

@lefty313
Created June 1, 2011 19:21
Show Gist options
  • Save lefty313/1003084 to your computer and use it in GitHub Desktop.
Save lefty313/1003084 to your computer and use it in GitHub Desktop.
pointer_matrix_operation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int ** allocate_memory(int w, int k)
{
int **tab, i;
tab = (int **) calloc(w, sizeof(int*));
for ( i = 0; i < w; ++i)
{
tab[i] = (int *) calloc(k, sizeof(int));
}
return tab;
}
void populate_memory(int ** tab,int w, int k)
{
int i,j;
for (i = 0; i < w; ++i)
{
for (j = 0; j<k; ++j)
{
tab[i][j] = rand() % 100;
}
}
}
void show_memory(int ** tab,int w, int k)
{
int i,j;
for (i = -1; i < w;++i)
{
i == -1 ? 0 : printf("%d:\t",i);
for (j = 0; j<k; ++j)
{
// wypisz naglowki macierzy jezeli i == -1
i == -1 ? printf("\t%d",j) : printf("%d\t",tab[i][j]);
// wstaw \n jezeli j = ostatnia kolumna
if (j % (k - 1) == 0 && j != 0) printf("\n");
}
}
}
void clean_memory(int ** tab,int w)
{
int i;
for (i = 0; i<w; ++i) {
free(tab[i]);
}
free(tab);
}
int add_or_substract_matrix(int ** tab_r,int ** tab_a, int ** tab_b, int w, int k, char sign)
{
int i,j;
for (i = 0; i < w; ++i)
{
for (j = 0; j < k; ++j)
{
if (sign == '+')
{
tab_r[i][j] = tab_a[i][j] + tab_b[i][j];
}
else if (sign == '-')
{
tab_r[i][j] = tab_a[i][j] - tab_b[i][j];
}
}
}
return 1;
}
int multiplication_matrix( int ** tab_r, int ** tab_a, int ** tab_b, int w1, int k1, int w2, int k2)
{
int i,j,k;
for(i = 0; i < w1; ++i)
{
for(j = 0; j < k2;j++)
{
tab_r[i][j]=0;
for(k = 0; k < k1; k++) tab_r[i][j]+=tab_a[i][k] * tab_b[k][j];
}
}
return 1;
}
int main(int argc, char *argv[])
{
int **first_multi_array,**second_multi_array,**result_matrix,i;
int rows,cols;
srand ( time(NULL) );
rows = 2;
cols = 2;
first_multi_array = allocate_memory(rows,cols);
populate_memory(first_multi_array,rows,cols);
printf("\nLosowo wygenerowana macierz \"A\"\n\n");
show_memory(first_multi_array,rows,cols);
printf("\n");
second_multi_array = allocate_memory(rows,cols);
populate_memory(second_multi_array,rows,cols);
printf("\nLosowo wygenerowana macierz \"B\"\n\n");
show_memory(second_multi_array,rows,cols);
printf("\n");
result_matrix = allocate_memory(rows,cols);
add_or_substract_matrix(result_matrix,first_multi_array,second_multi_array,rows,cols,'-');
printf("\nOdejmowanie macierzy A od macierzy B\n\n");
show_memory(result_matrix,rows,cols);
add_or_substract_matrix(result_matrix,first_multi_array,second_multi_array,rows,cols,'+');
printf("\nDodawanie macierzy A do macierzy B\n\n");
show_memory(result_matrix,rows,cols);
printf("\nMnozenie macierzy A przez macierz B\n\n");
multiplication_matrix(result_matrix,first_multi_array,second_multi_array,rows,cols,rows,cols);
show_memory(result_matrix,rows,cols);
clean_memory(result_matrix,rows);
clean_memory(first_multi_array,rows);
clean_memory(second_multi_array,rows);
getchar();
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment