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

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

Saturday, March 25, 2017

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

Wednesday, November 30, 2016

dynamic 2d array using double data type

 #include <stdio.h>  
 #include <iostream>  
   
   
 int main() {  
  // dimensions  
  int N = 74;  
  int M = 3;  
   
  int i, j;  
    
  // dynamic allocation  
  double** ary = new double*[N];   //2d array  
  for(i = 0; i < N; ++i)         //↑  
    ary[i] = new double[M];      //1d array  
    
  for(i=0; i<N; i++){  
       for(j=0; j<M; j++){  
          ary[i][j] = i+j;  
          printf("point[%d][%d] = %.2f \n", i, j, ary[i][j]);  
       }  
    }  
    
  // free  
  for(i = 0; i < N; ++i)  
   delete [] ary[i];  
  delete [] ary;  
    
  return 0;  
 }  

//reffer to [http: codeformatter.blogspot.jp]

Wednesday, November 2, 2016

passing and returning array in c language

#include                                         //standard library
#include                                        //standard mathematical library

//first derivative function
static double* GetDerivative(double t, double A[], double B[], double C[])       
                                                              //the function must be pointer double*
                                                              //for returning array

{
    //P・(t) = 3At^2 + 2Bt + C
    static double Pdot[3] = {0};                      //must be static
   
    Pdot[0] = 3*A[0]*t*t + 2*B[0]*t + C[0];
    Pdot[1] = 3*A[1]*t*t + 2*B[1]*t + C[1];
    Pdot[2] = 3*A[2]*t*t + 2*B[2]*t + C[2];
   
    return Pdot;                                       //return the array
}
//first derivative function

int main ()
{
    //P(t) = At3 + Bt2 + Ct + D
    ////P・(t) = 3At^2 + 2Bt + C
    double *derivative = 0;                        //must be pointer double *
                                                          //because the function is a pointer as well

    //double t[4] = {0.0, 0.25, 0.5, 0.75};
    double t[1] = {1};
    double A[] = {1, 2, 3};
    double B[] = {4, 6, 6};
    double C[] = {7, 8, 9};
    double D[] = {10, 11, 12};

    derivative = GetDerivative(t[0], A, B, C);    //put the return function to derivative
                                                              //plus make it as an array
                                                              //do not add [] even if it is array,
                                                              // unless you want to point the specific
                                                              // address of the array like t[0]
   
    printf("%ft2 + %ft + %f \n", derivative[0], derivative[1], derivative[2]);

    return 0;
}

//output 18.000000t2 + 26.000000t + 30.000000

Sunday, August 7, 2016

yodogawa fireworks!






Yodogawa fireworks is the biggest fireworks event in Osaka, I am glad that we came there because that was one of the best firework event I have ever came to visit. But, to be honest the smoke was a bit disturbing as you can see in the picture, sigh!

なにわ淀川花火大会は大阪で最大の花火イベントです。嫁と二人で行ってきましたが、非常によかったです。日本歴9年間の中でベストスリーの花火大会に入るぐらいの迫力だと思います。ただ、煙が邪魔でいい写真を撮るのは難しかったです。



Our place was there (near minamikata station), we arrived there at 18:00 but we can still found a good spot easily. Although a bit far from the main spot, yet, quite a view. Next year, we want to try to find a spot at Yodogawa athletic park so we can get a good close spot.

ところで撮影スポットは以下の通りで、南方駅から近いです。場所的には18時についてもまだ余裕で座れるし、屋台も多くてにぎやかなスポットだし、写真もよかったです。次回行くときには打ち上げ場所にもっと近づいて淀川河川公園から撮影してみたいです。