Agribusiness × AI
The role of AI in Agribusiness and study case in our lab
Laboratory of Management Informatics
Agribusiness × AI
The role of AI in Agribusiness and study case in our lab
Laboratory of Management Informatics
#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
#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);
}
#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))>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 < 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)>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);
}