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);

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]);  
    printf("\n");  
 }  
   
 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  
   showme(hd);  
   
   //free the memory  
   if(hd) free(hd);  
 }  
   

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);  
 }  
   

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
         }  
         printf("\n");  
    }  
   
    //must free the memorry allocation after using it  
    for(i=0; i<row; i++)  
         free(ar[i]);      
  } 
result:
[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

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");  
     printf("--\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  
 } 
 
result:
-------------- 
 array number: 5
[0]:0.10
[1]:0.20
[2]:0.30
[3]:0.40
[4]:0.50
lets double it and put in your new array using malloc
--
array[0]=0.20
array[1]=0.40
array[2]=0.60
array[3]=0.80
array[4]=1.00 

binomial coefficient c code

 #include<stdio.h>  
   
 //binomial coefficient function  
 double Ni(  
         int deg,    //degree   
         int b       //for binomial  
         )  
 {  
     if( deg < 0 )  
         return 0.0;  
     else if ( deg == 0 )  
         return 1.0;  
     else{  
         double temp = 1.0;  
         for(int i=1; i<=b;i++ ){  
             temp = temp* (double)(deg-i+1)/i;  
             printf("%f\n", temp);  
         }  
         return temp;  
     }  
 }  
   
   
 void main ()  
 {  
     int a=10, b=3;  
     double bin;  
     bin = Ni(a, b);  
     printf("\n=============\n");  
     printf("%f\n", bin);  
 }  

comparison of secant and bisection method

   
 #include<stdio.h>  
 #include<math.h>  
 #include<time.h>  
   
 //example of function f(t), you can make any function you like  
 float f(float t)  
 {  
   return(((48.2291-30.6208*t-43) * (48.2291-30.6208*t-43)) + 35*t - 25.0);   
 }  
   
 void main()  
 {  
     
   float a = 0.1, b = 0.3, c = 0, e = 7.5e-3;  
   //a, b interval  
   //e error  
   
   int count = 1, n = 50; //n=maximum iteration  
     
   //  
   //secant  
   //  
   clock_t begin = clock();  
   do  
   {      
     c = (a*f(b) - b*f(a)) / (f(b) - f(a));  
     //c = b - (f(b)*(b-a) / (f(b) - f(a)));  
     a=b;  
     b=c;  
     printf("i No-%d  t=%f  d=%f\n",count,c, f(c));  
     count++;  
     if(count==n) break;  
   } while(fabs(f(c))&gt;e);  
   
   clock_t end = clock();  
   double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;  
   printf("time:%f", time_spent);  
   
   printf("\nThe required solution is %f\n",c);  
   printf("d = %f\n", f(c) + 25);  
   
   //  
   //bisection  
   //  
   double fa, fb, fc;  
   a = 0.1; b = 0.3; c = 0; e = 7.5e-3;  
   count = 1;  
   clock_t begin2 = clock();  
   do{  
     c = (a+b)/2;  
     fa = f(a);  
     fb = f(b);  
     fc = f(c);  
   
     if(fa*fc &lt; 0){  
       b = c;  
     }  
     else{  
       a = c;  
     }  
     printf("i No-%d  t=%f  d=%f\n", count, c, fc);  
     count++;  
     if(count==n) break;  
   } while(fabs(fc)&gt;e);  
   
   clock_t end2 = clock();  
   double time_spent2 = (double)(end2 - begin2) / CLOCKS_PER_SEC;  
   printf("time:%f", time_spent2);  
   
   printf("\nThe required solution is %f\n",c);  
   printf("d = %f\n", fc + 25);  
   
    
 }