Using c language and MATLAB to find data by binary method
From the form can be found that the difference is that Matlab is a sentence to compile, and C language is a complete code snippet compiled, and C has a header file (#include (...) , and Matlab is not, for numerical operation Matlab is very simple and powerful, C is a bit cumbersome, for the algorithm requires C relatively higher.
Specific examples are as follows:
C language
#define M 10
#include <stdio.h>
void Main ()
{
static int a[m]={-12,0,6,16,23,56,80,100,110,115};
int n, low, Mid, high, found;
low=0;
High=m-1;
Found=0;
printf ("Input a number to be searched:");
scanf ("%d", &n);
while (Low<=high)
{
Mid= (Low+high)/2;
if (N==a[mid])
{
Found = 1; Break
}/* found, end loop */
else if (n > A[mid])
low=mid+1;
Else
High=mid-1; }
if (found==1)
printf ("The index of%d is%d", n,mid);
Else
printf ("There is not%d", n); }
The Mtalab implementation program is as follows:
X=fix (100*rand); n=7; Test=1; For K=1:7NUMB=INT2STR (n);d ISP ("a guess is a number between 0 and") guess=input (': '); if Guess<xdisp (' low ') ElseIf GUE Ss>xdisp (' high ') Else disp ("You Win") Test=0;break;endn=n-1;endif Test==1disp (' You lost ')
C language and Matlab