Sunday, February 1, 2015

Malloc C

Kode C berikut memiliki fungsi-fungsi untuk melakukan berbagai operasi matrix floating point format dengan menggunalkan malloc yang berperan untuk mengalokasikan memori ketika mengeksekusi program. Untuk mengaktivasi malloc anda memerlukan library #include

Copy-lah kode dibawah ini dengan gedit lalu berinama matrix.c

lalu eksekusi dengan perintah:
gcc -o matrix matrix.c -lm  lalu enter
./matrix lalu enter

-lm digunakan untuk membuat link dengan math.h

matrixA.txt
5.0    5.0
5.0    5.0

matrixX.txt
0.2    0.2
0.2    0.2

//This code is slightly modified from www.thelearningpoint.net
//The original version requires keyboard input.
//I modified it to be able to read input from files

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

float **matrixA;
float **matrixB;
float **matrixRes;
int rowA,columnA,rowB,columnB,row,column,navigate;

//Addition
void add()
{
    matrixRes=(float **)malloc(rowA*sizeof(float));
    for(row=0;row<rowA;row++)
    matrixRes[row]=(float *)malloc(columnA*sizeof(float));
    for(row=0;row<rowB;row++)
    {
        for(column=0;column<columnB;column++)
        {
            matrixRes[row][column]=matrixA[row][column]+matrixB[row][column];
            printf("%.2f ",matrixRes[row][column]);
        }
        printf("\n");
    }
    for(row=0;row<rowA;row++)
        free(matrixRes[row]);
    free(matrixRes);
}

//Substraction
void sub()
{
    matrixRes=(float **)malloc(rowA*sizeof(float));
    for(row=0;row<rowA;row++)
    matrixRes[row]=(float *)malloc(columnA*sizeof(float));
    for(row=0;row<rowB;row++)
    {
        for(column=0;column<columnB;column++)
        {
            matrixRes[row][column]=matrixA[row][column]-matrixB[row][column];
            printf("%.2f ",matrixRes[row][column]);
        }
        printf("\n");
    }
    for(row=0;row<rowA;row++)
        free(matrixRes[row]);
    free(matrixRes);
}

//Multiplication
void mult()
{
    matrixRes=(float **)malloc(rowA*sizeof(float));
    for(row=0;row<rowA;row++)
    matrixRes[row]=(float *)malloc(columnB*sizeof(float));
    for(row=0;row<rowB;row++)
    {
        for(column=0;column<columnB;column++)
        {   matrixRes[row][column]=0;
            for(navigate=0;navigate<columnA;navigate++)
            matrixRes[row][column]+=matrixA[row][navigate]*matrixB[navigate][column];
            printf("%.2f ",matrixRes[row][column]);
        }
        printf("\n");
    }
    for(row=0;row<rowA;row++)
        free(matrixRes[row]);
    free(matrixRes);
}

//Determinant
float determinant(float **matrix,int size)
{
    float **temp;
    float det=0;

    if(size==1)
    det=matrix[0][0];
else
    if(size==2)
    det=matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];
else
    {
        int cctr,ictr,ctr;
    for(column=0;column<size;column++)
    {
        temp=(float **)malloc((size-1)*sizeof(float));
        for(ctr=0;ctr<size-1;ctr++)
        temp[ctr]=(float *)malloc((size-1)*sizeof(float));
        for(ctr=1;ctr<size;ctr++)
        {
            cctr=0;
            for(ictr=0;ictr<size;ictr++)
            {
                if(ictr==column)
                    continue;
                temp[ctr-1][cctr]=matrix[ctr][ictr];
                cctr++;
            }
        }
            det=det+pow(-1,column)*matrix[0][column]*determinant(temp,size-1);
        for(ctr=0;ctr<size-1;ctr++)
        free(temp[ctr]);
        free(temp);
    }
}
    return det;
}

//Inverse

void matrixInverse(float **matrix,int size,float res)
{
float **tem;
int ctr,ictr,rowT,columnT;
float **matrixCft;
matrixCft=(float **)malloc(size*sizeof(float));
tem=(float **)malloc((size-1)*sizeof(float));
for(ctr=0;ctr<size-1;ctr++)
tem[ctr]=(float *)malloc((size-1)*sizeof(float));
for(ctr=0;ctr<size;ctr++)
matrixCft[ctr]=(float *)malloc(size*sizeof(float));
for(row=0;row<size;row++)
{
for(column=0;column<size;column++)
{
    rowT=0;
    columnT=0;
    for(ctr=0;ctr<size;ctr++)
        for(ictr=0;ictr<size;ictr++)
            if(ctr!=row&&ictr!=column)
                {
     tem[rowT][columnT++]=matrix[ctr][ictr];
                    if(columnT==size-1)
                    {
                        rowT++;
                        columnT=0;
                    }
                }
    matrixCft[row][column]=pow(-1,(row+column))*determinant(tem,size-1);
}
}
for(column=0;column<size;column++)
for(row=0;row<size;row++)
matrixCft[column][row]=matrixCft[column][row]/res;
float **Result;
Result=(float **)malloc(size*sizeof(float));
for(ctr=0;ctr<size;ctr++)
Result[ctr]=(float *)malloc(size*sizeof(float));
for(column=0;column<size;column++)
for(row=0;row<size;row++)
Result[column][row]=matrixCft[row][column];
for(row=0;row<size;row++)
    {
        for(column=0;column<size;column++)
        printf("%.2f ",Result[row][column]);
        printf("\n");
    }
for(ctr=0;ctr<size-1;ctr++)
    free(tem[ctr]);
free(tem);
for(row=0;row<size;row++)
    {
        free(Result[row]);
        free(matrixCft[row]);
    }
free(Result);
free(matrixCft);
}



//Read
void readA()
{
FILE *inputA=fopen("matrixA.txt","r");
 for(row=0;row<rowA;row++)
    {
        for(column=0;column<columnA;column++)
        {
    fscanf(inputA,"%f",&matrixA[row][column]);
        }
    }
  fclose(inputA);
}

void readB()
{
FILE *inputB=fopen("matrixX.txt","r");
 for(row=0;row<rowB;row++)
    {
        for(column=0;column<columnB;column++)
        {
    fscanf(inputB,"%f",&matrixB[row][column]);
        }
    }
  fclose(inputB);
}



//Main
int main()
{
    //Define row and col
    rowA=2;columnA=2;
    rowB=2;columnB=2;

    matrixA=(float **)malloc(rowA*sizeof(float));
    matrixB=(float **)malloc(rowB*sizeof(float));

    for(row=0;row<rowA;row++)
    matrixA[row]=(float *)malloc(columnA*sizeof(float));

    for(row=0;row<rowA;row++)
    matrixB[row]=(float *)malloc(columnB*sizeof(float));
    readA();
    readB();
    add();
    sub();
    mult();
    if(rowA==columnA)
    {
    float resA=determinant(matrixA,rowA);
    printf("Determinant of matrix A is %.2f\n",resA);
    if(resA!=0)
    {
    printf("The matrix inverse is\n");
    matrixInverse(matrixA,rowA,resA);
    }
    else
    printf("The inverse can not be calculated\n");
    }
    else
    {
        printf("Determinant of matrix A can not be calculated\n");
        printf("Inverse of matrix A can notbe calculated\n");
    }
    if(rowB==columnB)
    {
    float resB=determinant(matrixB,rowB);
    printf("Determinant of matrix B is %.2f\n",resB);
    if(resB!=0)
    {
    printf("The matrix inverse is\n");
    matrixInverse(matrixB,rowB,resB);
    }
    else
    printf("The inverse can not be calculated");
    }
    else
    {
        printf("Determinant of matrix B can not be calculated\n");
        printf("Inverse of matrix B can not be calculated\n");
    }
    for(row=0;row<rowA;row++)
        free(matrixA[row]);
    for(row=0;row<rowB;row++)
        free(matrixB[row]);
    free(matrixA);
    free(matrixB);
}


No comments: