This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

please help to convert c++ algorithm to app inventor blocks

I wanna implement this code for the app inventor, but there was several problem here. I can't use array or index for developing my trapezoidal method.. like this..
code from mbed compiler  mbed compiler algorithm and also I've tried before using this guy method to implement array in app inventor array app inventor
 
here's the code that I wanna to implemnt in app inventor..
 
 for (int i=0; i<10; i++)
        {
          //formula
            totvect[i] = sqrt(pow((double)xaccl[i],2.0)+pow((double)yaccl[i],2.0)+pow((double)zaccl[i],2.0));
            wait(0.3);
            totave[i] = ((double)totvect[i]+(double)totvect[i-1])/2.0 ;
            wait(0.3);
            acc=acc+totave[i];
            wait(0.3);
            totvel[i]=(1*((double)totave[i]-(double)totave[i-1]/2.0)+totvel[i-1]);
            wait(0.3);
            totdist[i]=(1*((double)totvel[i]-(double)totvel[i-1]/2.0)+totdist[i-1]);
        }
ant knn method also..
do{
        wait(0.1);
        
        for(int i=0;i<k;i++)
        { //initiLIZATION
            b[i]=-1;
            c[i]=0;
        } 
        int min=1000;
        p = (readings[0]);
        q = (readings[1]);
        //uart1.scanf("%ld,%ld",&p,&q);
        if((p < 0) | (q < 0))
            exit(0);
        data n; //data point to classify
        n.setd(p,q,STANDING);
        data d[MAX]; //training set
        
        d[0].setd(1,1,STANDING);
        d[1].setd(1,2,STANDING);
        d[2].setd(1,3,STANDING);
        d[3].setd(1,4,STANDING);
        d[4].setd(1,5,STANDING);
        d[5].setd(1,6,STANDING);
        d[6].setd(1,7,STANDING);
        d[7].setd(2,1,STANDING);
        d[8].setd(2,2,STANDING);
        d[9].setd(2,3,WALKING);
        d[10].setd(2,4,WALKING);
        d[11].setd(2,5,WALKING);
        d[12].setd(2,6,WALKING);
        d[13].setd(2,7,WALKING);
        d[14].setd(5,1,CLIMBING);
        d[15].setd(5,2,CLIMBING);
        d[16].setd(5,3,CLIMBING);
        d[17].setd(5,4,CLIMBING);
        d[18].setd(5,5,CLIMBING);
        d[19].setd(5,6,CLIMBING);
        for(int i=0;i<20;i++){
            a[i]=dis(n,d[i]);
            //uart1.printf("\t\t %d\n", a[i]);
        }
        //k-nearest neighbours calculation i.e smallest k distances
        for(int j=0;j<k;j++)
        {
            min=1000;
            for(int i=0;i<20;i++)
            {
                if(i!=b[0]&&i!=b[1]&&i!=b[2])
                { 
                    if((a[i]<=min))
                    {
                        min=a[i];
                        b[j]=i;
                    }
                }
            } 
            
            //uart1.printf("%d\n",min);
        }
        //counting frequency of a class in each neighbour   
        for(int i=0;i<k;i++)
        {
            switch (d[b[i]].getcat())
            { 
            case STANDING:
                c[0]++;
                break;
            case WALKING:
                c[2]++;
                break;
            case CLIMBING:
                c[1]++;
                break;
            }
        } //counting max frequency
        int max=-1,j;
        for(int i=0;i<k;i++)
        {    
            if(c[i]>max){
                max=c[i];
                j=i;
            }
        }
       
            wait(0.1);
            printf("Prediction is:");
            switch (j)
            {    
            case 0:
                uart1.printf("STANDING\n");
                break;
            case 1:
                uart1.printf("CLIMBING\n");
                break;
            case 2:
                uart1.printf("WALKING\n");
                break;
            }
         
        
    }while(true); 
        
    }
please help me to convert this c++ code to app inventor blocks..
thanks
and here it's my final result
but there was error when debug to apk.. that shows like this one..
for the file project also here
Related