Thursday, June 22, 2017




Friday, June 16, 2017

compute derivative coefficient of n-th degree polynomial equation

 #include <stdio.h>  
 #define ELE 4  
 int main()  
  int i;  
  int g = 3; //degree  
  double c[ELE][3] = {{1, 5, 9},   
                      {2, 6, 10},  
                      {3, 7, 11},  
                      {4, 8, 12}};   
  //ELE coefficient of parametric equation  
  //eg ELE == 0 4x^3 + 3x^2 + 2x + 1 2d element of array is x, y, z  
  for(int i=0; i<ELE; i++)  
   for(int j=0; j<3; j++)  
     printf("%.1f ", c[i][j]);  
  int n = 2; //n-th derivative   
  for(int drv=1; drv<=n; drv++)  
   for(i=g; i>=0; i--)  
     for(int axs=0; axs<3; axs++) //if we want the derivative not be 3 dimensional parametric equation delete this loop  
  for(int i=0; i<ELE; i++)  
   for(int j=0; j<3; j++)  
     printf("%.1f ", c[i][j]);  

Friday, June 9, 2017

2d dynamic array with known number of column element

how to make dynamic array with known number of column element
for example for a 3d coordinate.

suppose i need to make 2d dynamic array for a 3d coordinate, because the element of 3d coordinate is x, y, z so the number column is 3.
let's say i need to have
double point[some_number][3];
and that number is suppose unknown until we read the file, lets say ptscount.

one simple way to allocate the dynamic 2d array is as follow. 

double (*point)[3] = (double (*)[3])malloc(sizeof(double) * ptscount*3);

brief explanation
- double (*point)[3]: double variable of  "point" that has 3 array element, this will make point[ptscount][3]

- (double (*)[3]): casting to double of pointer that has 3 array element

- malloc(sizeof(double) * ptscount*3): memory allocation for (the size of double variable) times(×) (ptscount (number or row of the array) with 3 column)

after using it you have to free the memory
free (point);

Thursday, June 1, 2017

use malloc for struct

 #include <stdio.h>  
 #include <stdlib.h>  
 struct Healdirect  
    double m_tors;  
    double curvedir[3];  
 void showme(struct Healdirect *healdeci)  
    struct Healdirect test;  
    test.m_tors = healdeci->m_tors;  
    test.curvedir[0] = healdeci->curvedir[0];  
    test.curvedir[1] = healdeci->curvedir[1];  
    test.curvedir[2] = healdeci->curvedir[2];  
    printf("torsion: %.2f \n", test.m_tors);  
    printf("curvature direction: ");  
    for(int i=0; i<3; i++)  
       printf("%.2f ", test.curvedir[i]);  
 void main()  
    //example of struct using pointer and malloc  
    struct Healdirect *hd = malloc(sizeof(hd));  
    //sample data   
    hd->m_tors = 0.24;  
   hd->curvedir[0] = 0.99;  
   hd->curvedir[1] = 0.88;  
   hd->curvedir[2] = 0.77;  
   //show sample data after the memory is filled  
   //free the memory  
   if(hd) free(hd);  

Tuesday, May 30, 2017

dynimcally allocated array 2

 #include <stdio.h>  
 #include <stdlib.h>  
 void main()  
    double *ary;   //will be your dynamic 1d array  
    int num;       //your array element  
    //input your number of array element  
    printf("num: ");  
    scanf("%d", &num);  
    //syntax: number of block address times byte needed (sizeof) for double variable  
    ary = malloc(num * sizeof(double));     
    //fill the array (can be changed to any number) and display them  
    for(int i=0; i<num; i++)  
       ary[i] = i * i * 2.8;  
       printf("%.2f\n", ary[i]);  
    //must free the memory allocation to not make memory leak  
    if(ary) free(ary);  

Thursday, May 18, 2017

dynamically allocated 2d array using an array of pointers

 #include <stdio.h>   
 #include <stdlib.h>   
  void main()   
    int row = 5, col = 3;   
    int i, j;   
    double *ar[row]; //will be your 2d array   
    for(i=0; i<row; i++)  //make it into 2d array
         ar[i] = malloc(sizeof(int)*col);  //get the column number of array
    for(i=0; i<row; i++)  
         for(j=0; j<col; j++)  
              ar[i][j] = (i + j) * 1.0;  //filling the array element
    for(i=0; i<row; i++)  
         for(j=0; j<col; j++)  
          printf("[%d][%d]=%.2f ", i, j, ar[i][j]);   //print the array after filling it
    //must free the memorry allocation after using it  
    for(i=0; i<row; i++)  
[0][0]=0.00 [0][1]=1.00 [0][2]=2.00
[1][0]=1.00 [1][1]=2.00 [1][2]=3.00
[2][0]=2.00 [2][1]=3.00 [2][2]=4.00
[3][0]=3.00 [3][1]=4.00 [3][2]=5.00
[4][0]=4.00 [4][1]=5.00 [4][2]=6.00

Friday, May 12, 2017

dynamically allocated array

 #include <stdio.h>  
 #include <stdlib.h>  
 void main()  
     double *ar; //will be your array  
     double elem[] = {0.1, 0.2 ,0.3 ,0.4 ,0.5};   
     int num = 5, i;  
     printf("array number: %d\n", num);  
     for(i=0; i<num; i++)  
         printf("[%d]:%.2f\n", i, elem[i]);  
     printf("lets double it and put in your new array using malloc\n");  
     ar = malloc(sizeof(double)*num); //allocate the memory for your array  
     for(i=0; i<num; i++){  
       *(ar+i) = elem[i] * 2;  
     for(i=0; i<num; i++)   
         printf("array[%d]=%.2f\n", i, ar[i]);  
 free(ar); //must free the memorry allocation after using it  
 array number: 5
lets double it and put in your new array using malloc