2014年2月26日 星期三

#define in C is expands textually

#include<stdio.h>
#define square(x) x*x
main() 
{
    int x = 2;
    printf("%d\n", square(x + 1));
    return 0;
}

The answer is 5 not 9, because the preprocessor expands it textually like below.

x * x = 2+1 * 2+1 = 2+(1*2)+1=2+2+1=5
That is not function. We can fix it as

#define square (x) (x)*(x) 
or
#define square (x) ((x)*(x)) 


Using Sparse ARFF files as the input of weka association rule

the format is as below test.arff

 @relation test
 @attribute a {y}
 @attribute b {y}
 @attribute c {y}
 @attribute d {y}
 @attribute e {y}
 @attribute f {y}
 @attribute g {y}
 @attribute h {y}

@data
{1 y,3 y,5 y}
{2 y,3 y,5 y}
{2 y,6 y}
{1 y,3 y,5 y}
{3 y,5 y,7 y}
{1 y,2 y,3 y}
{2 y,4 y,5 y}
{2 y,3 y,5 y}
{3 y,5 y,7 y}
{1 y,2 y}
{1 y,5 y,7 y}