Skip to content

Instantly share code, notes, and snippets.

@lefty313
Created June 1, 2011 19:17
Show Gist options
  • Save lefty313/1003074 to your computer and use it in GitHub Desktop.
Save lefty313/1003074 to your computer and use it in GitHub Desktop.
Memory allocation with struct
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Matrix {
int w;
int k;
int **container;
};
struct Matrix create_matrix(int w, int k)
{
struct Matrix tab;
int i;
tab.w = w;
tab.k = k;
tab.container = (int **) calloc((tab.w=w), sizeof(int*));
for ( i = 0; i < tab.w; ++i)
{
tab.container[i] = (int *) calloc((tab.k = k), sizeof(int));
}
return tab;
}
void populate_memory(struct Matrix tab)
{
int i,j;
for (i = 0; i < tab.w; ++i)
{
for (j = 0; j<tab.k; ++j)
{
tab.container[i][j] = rand() % 100;
}
}
}
void free_memory(struct Matrix tab)
{
int i;
for (i = 0; i<tab.w; ++i) {
free(tab.container[i]);
}
free(tab.container);
}
void print_matrix(struct Matrix tab)
{
int i,j;
printf("\n");
for (i = -1; i < tab.w; ++i)
{
i == -1 ? 0 : printf("%d:\t",i);
for (j = 0; j< tab.k; ++j)
{
// wypisz naglowki macierzy jezeli i == -1
i == -1 ? printf("\t%d",j) : printf("%d\t",tab.container[i][j]);
// wstaw \n jezeli j = ostatnia kolumna
if (j % (tab.k - 1) == 0 && j != 0) printf("\n");
}
}
}
struct Matrix add_or_substract_matrix(struct Matrix tab1, struct Matrix tab2, char sign)
{
struct Matrix result_tab;
int i,j;
result_tab.container = NULL;
result_tab = create_matrix(tab1.w,tab1.k);
// mozna dodawac i odejmowac macierze tylko o tych samych wymiarach
if (tab1.w == tab2.w && tab1.k == tab2.k)
{
for (i = 0; i < result_tab.w; ++i)
{
for (j = 0; j < result_tab.k; ++j)
{
if (sign == '+')
{
result_tab.container[i][j] = tab1.container[i][j] + tab2.container[i][j];
}
else if (sign == '-')
{
result_tab.container[i][j] = tab1.container[i][j] - tab2.container[i][j];
}
}
}
}
return result_tab;
}
struct Matrix multiplication_matrix(struct Matrix tab1, struct Matrix tab2)
{
struct Matrix result_tab;
int i,j,k;
result_tab.container = NULL;
if (tab1.k == tab2.w)
{
result_tab = create_matrix(tab1.w,tab2.k);
for(i = 0; i < result_tab.w; ++i)
{
for(j = 0; j < result_tab.k;j++)
{
result_tab.container[i][j]=0;
for(k = 0; k < tab1.k; k++) result_tab.container[i][j]+=tab1.container[i][k] * tab2.container[k][j];
}
}
}
return result_tab;
}
int main(int argc, char *argv[])
{
int rows,cols;
struct Matrix tab1,tab2,result_tab;
srand ( time(NULL) );
rows = 2;
cols = 2;
tab1 = create_matrix(rows,cols);
populate_memory(tab1);
tab2 = create_matrix(rows,cols);
populate_memory(tab2);
printf("\nLosowo wygenerowana macierz \"A\"\n\n");
print_matrix(tab1);
printf("\nLosowo wygenerowana macierz \"B\"\n\n");
print_matrix(tab2);
result_tab = add_or_substract_matrix(tab1,tab2,'+');
printf("\nDodawanie macierzy A do macierzy B\n\n");
print_matrix(result_tab);
result_tab = add_or_substract_matrix(tab1,tab2,'-');
printf("\nOdejmowanie macierzy A od macierzy B\n\n");
print_matrix(result_tab);
result_tab = multiplication_matrix(tab1,tab2);
printf("\nMnozenie macierzy A przez macierz B\n\n");
print_matrix(result_tab);
free_memory(tab1);
free_memory(tab2);
free_memory(result_tab);
getchar();
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment