#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]);
printf("\n");
}
printf("\n--\n");
//derivative
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
c[i][axs]=c[i][axs]*(i-(drv-1));
//derivative
for(int i=0; i<ELE; i++)
{
for(int j=0; j<3; j++)
printf("%.1f ", c[i][j]);
printf("\n");
}
compute derivative coefficient of n-th degree polynomial equation
Friday, June 16, 2017
2d dynamic array with known number of column element
Friday, June 9, 2017
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);
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
Thursday, June 1, 2017
#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
Tuesday, May 30, 2017
#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
Thursday, May 18, 2017
#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
Friday, May 12, 2017
#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
Saturday, March 25, 2017
#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);
}
Subscribe to:
Posts (Atom)
