Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Making a C Code into an Android App

  • 13-02-2011 9:48pm
    #1
    Registered Users, Registered Users 2 Posts: 12,619 ✭✭✭✭


    Hey guys I have a code in C which runs some mathematical calculations for me. It's grand having it around on the laptop, but it would be much easier to have it as an app, is it hard to convert them?


Comments

  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Not a huge amount of effort if you know Java. Few hours depending on complexity.


  • Registered Users, Registered Users 2 Posts: 5,238 ✭✭✭humbert


    What Khannie is saying is that you'll have to rewrite it in java. I'd imaging you'd also have to create a simple android application. There is a learning curve attached to that which means more time if you haven't done it before.

    You could probably use the ndk to compile the c for the phones architecture. and run it from a terminal on the phone.


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    errloyd,
    I think that's what you want:

    http://marakana.com/forums/android/examples/49.html

    Please give us some feedback if it works for you!


  • Registered Users, Registered Users 2 Posts: 5,238 ✭✭✭humbert


    That's pretty cool but involves the NDK and creating an android application. The right solution if the original c code is very complicated and important enough to merit the time.


  • Registered Users, Registered Users 2 Posts: 2,320 ✭✭✭Chet T16


    I converted some of my (very very simple) C code into apps using app inventor


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 12,619 ✭✭✭✭errlloyd


    The Code is very very simple. It just does a couple of a calculations in order to run a basic simulation. It just needs input for 3 variables. I'll check this out now.


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    errlloyd wrote: »
    The Code is very very simple. It just does a couple of a calculations in order to run a basic simulation. It just needs input for 3 variables. I'll check this out now.
    If that's something simple converting it to java seems to be the easiest option. Share the code, if it's not private/top secret project ;-)


  • Registered Users, Registered Users 2 Posts: 12,619 ✭✭✭✭errlloyd


    Right, so a quick explanation. The code is designed to predict the course of a debating competition 1000 times. Just to explain why. A debating competition starts off with a power paired league table. First round is random, 4 teams are in each "match up". The winner gets 3 points, second place 2, third 1, last 0. After that they are randomly power paired. So you always go against teams with the same amount of points as you. (Apart from random pull ups or pull downs if there is a number of teams on points not divisible by 4).

    That may go on for say 5 rounds of match ups. After that the top 4/8/16/32 teams are skimmed off into play offs. (If there are multiple teams tying there is a separate way to choose them, its not particularly relevant to this application).

    The purpose of this app was to predict how many points a team would need to make the play offs at the start of the competition. So the variables are the amount of rounds, teams, and the amount of teams which reach the play offs.

    I tested it on the laptop works fine. But I don't trek my laptop around, and lots of peeps seem to want it... Most debaters are android users.

    Here's the code.
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int printteamlist(int **S, int T);
    int rounds(int **S, int T, int R);
    int sortscores(int **S, int T);
    int runround(int **S, int T);
    int rnd(int range);
    int randomlist(int **S, int T);
    void printmatrix(int **S, int T);

    int main(int argc, char *argv[])
    {
    int T=atoi(argv[1]), i, k, n, s, p, w; //T is no. of teams (must be divis by 4)
    int R=atoi(argv[2]); //R is no. of rounds
    int B=atoi(argv[3]); //B is no. of teams that break to next round
    int **S;
    double **r, q;

    S=malloc(T*sizeof(int));

    r=malloc((3*R+1)*sizeof(double));

    for(i=0; i<T; i++)
    S=malloc(5*sizeof(int));

    for(i=0; i<=3*R; i++)
    r=malloc(5*sizeof(double));

    srand((unsigned)time(NULL));

    for(i=0;i<=3*R;i++)
    {
    r[0]=i+1;
    r[1]=0;
    r[2]=0;
    r[3]=0;
    r[4]=0;
    }

    /*for(i=0;i<T;i++)
    {
    for(k=0;k<5;k++)
    S[k]=0;
    S[0]=i+1;
    S[2]=i+1;
    }*/
    //Just sets all entries to 0, except in the first column, where entries are set to team numbers

    //printmatrix(S,T);.l
    //printteamlist(S,T);
    for(s=0;s<1000;s++)
    {
    for(i=0;i<T;i++)
    {
    for(k=0;k<5;k++)
    S[k]=0;
    S[0]=i+1;
    S[2]=i+1;
    }

    rounds(S,T,R);
    //resets S, and runs rounds
    for(i=0;i<=3*R;i++)
    {
    r[1]=0;
    r[2]=0;
    }
    //refreshers r, but not the last column
    for(i=0;i<3*R;i++)
    {
    w=r[2];
    for(k=0;k<T;k++)
    {
    if(S[k][1]==i+1)
    {
    r[2]=r[2]+1;
    if(S[k][2]<=B)
    r[1]=r[1]+1;
    }
    }
    if(w-r[2]!=0)
    r[4]++;
    }
    //gets the required values of r
    for(i=0; i<3*R; i++)
    {
    q=0;
    p=r[2];
    if(abs(p)>0.1)
    q=(r[1]/r[2]);
    r[3]=r[3]+q;
    }
    //for(i=0;i<3*R;i++)
    //printf("%f\t%f\t%f\t%f\t%f\n",r[0],r[1],r[2],r[3],r[4]);
    //sums the fraction part
    //printf("\n");
    }

    for(i=0;i<3*R;i++)
    {
    if(r[4]==0)
    {
    r[4]=1;
    r[3]=-1;
    }
    }

    for(i=0;i<3*R;i++)
    printf("%f\t%f\n",r[0],r[3]/r[4]);
    printf("\n(-1 means this value did not occur at all)\n");

    return 0;

    }

    //
    ////
    //The main matrix, S, is going to "do all the work". The first column is just the numbers 1 to T in
    //order, just the list of teams. The second column is the (running) total score. The third column
    //is the "sort" column, where the teams are ranked by score. The fourth column gives the "order" in
    //which to check team scores (because that process needs to be randomised). This column actually
    //isn't related to the first column (all the rest are...?), so probably shouldn't be in the matrix,
    //and should have its own array. I might change that. The fifth column is a "checklist" column, to
    //check if a row has been "checked" already. More columns later probably. Actually probably not,
    //and that fifth column seems rather useless.
    ////
    //

    int printteamlist(int **S, int T)
    {
    int i,k,a,j,s=0;

    k=S[0][1];
    for(i=0;i<T;i++)
    {
    if(S[1]>k)
    k=S[1];
    }

    for(i=0;i<=k;i++)
    {
    a=0;
    for(j=0;j<T;j++)
    {
    if(S[j][1]==i)
    a++;
    }
    s=s+a;
    printf("%d - %d\n",i,a);
    }
    }
    //Looks for the highest score attained, then just finds how many teams got every possible point total
    //(i.e. from 0 up to the highest attained) and prints this list.

    int rounds(int **S, int T, int R)
    {
    int i;

    for(i=0;i<R;i++)
    {
    randomlist(S,T);
    runround(S,T);
    sortscores(S,T);
    }
    }
    //runs all rounds: randomises the order (in which they're checked, probably not that important)
    //then runs a round, i.e. groups the teams and then gives them points, then finally sorts the
    //teams again. It does this R times (i.e. the amount of rounds there are). This is effectively
    //one competition

    int sortscores(int **S, int T)
    {
    int i,k,l,m,q,n=1;
    k=S[0][1];
    for(i=0;i<T;i++)
    {
    if(S[1]>k)
    k=S[1];
    }

    for(i=k;i>=0;i--)
    {
    for(l=0;l<T;l++)
    {
    m=S[l][3]-1;
    if(S[m][1]==i&&S[m][4]==0)
    {
    S[m][2]=n;
    S[m][4]=1;
    n++;
    }
    }
    }
    for(i=0;i<T;i++)
    S[4]=0;
    }
    //Looks through the second column, and then ranks each number in the second column relative
    //to the other elements in the second column. A random element means no ties, so elements in
    //the second column with the same values will have a different rank (randomly chosen)

    int runround(int **S, int T)
    {
    int i,k,m1,m2,m3,m4,c,p1,p2;
    int q[4];
    for(i=0;i<4;i++)
    q=i;

    for(i=0;i<T;i=i+4)
    {
    for(k=0;k<T;k++)
    if(S[k][2]==i+1)
    m1=k;
    for(k=0;k<T;k++)
    if(S[k][2]==i+2)
    m2=k;
    for(k=0;k<T;k++)
    if(S[k][2]==i+3)
    m3=k;
    for(k=0;k<T;k++)
    if(S[k][2]==i+4)
    m4=k;
    for(k=0;k<30;k++)
    {
    p1=rnd(4);
    p2=rnd(4);
    c=q[p1];
    q[p1]=q[p2];
    q[p2]=c;
    }
    S[m1][1]=S[m1][1]+q[0];
    S[m2][1]=S[m2][1]+q[1];
    S[m3][1]=S[m3][1]+q[2];
    S[m4][1]=S[m4][1]+q[3];
    }
    }
    //Groups teams in 4's, then randomly adds 1,2,3 or 0 points to each team (each score given
    //once per 4some)

    void printmatrix(int **S, int T)
    {
    int i,k;

    for(i=0; i<T; i++)
    {
    for(k=0; k<5; k++)
    printf("%d\t", S[k]);
    printf("\n");
    }
    }
    //Prints S

    int rnd(int range)
    {
    int r;

    r=rand()%range;
    return(r);
    }
    //Gives a random number in the required range, in our case from 0 to T-1

    int randomlist(int **S, int T)
    {
    int x,i,c,p1,p2;
    i=rnd(T);
    for(x=0;x<T;x++)
    {
    c=i%T+1;
    S[x][3]=c;
    i++;
    }

    for(x=0;x<T;x++)
    {
    p1=rnd(T);
    p2=rnd(T);
    c=S[p1][3];
    S[p1][3]=S[p2][3];
    S[p2][3]=c;
    }
    }
    //This basically sticks the numbers 1 to T into the fourth column randomly. Seems like a ****ty
    //function, but strangely efficient I think (it is **** though)


  • Registered Users, Registered Users 2 Posts: 5,238 ✭✭✭humbert


    Well the conversion to java should take about 20 minutes, then a simple android application (maybe not so easy).

    This seems like a completely deterministic probability question. It's not an area of math I've much experience but looking (briefly) at your code it seems like a Monte Carlo method. It's possible you have much greater expertise than me but are you sure it's not possible to calculate the result formulaically(ugly ugly word)?


  • Registered Users, Registered Users 2 Posts: 12,619 ✭✭✭✭errlloyd


    We tried creating a formula and tried spreadsheets before turning to the code. Not gonna lie had a good friend of mine do most of the coding. I understand it roughly, but he's a maths student and understands it a lot more clearly.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 5,238 ✭✭✭humbert


    Well an android app would be the way to go but I have a suspicion you haven't written one before and perhaps haven't a lot of experience with java. Someone here might do one up for you but while it's not very difficult it is pretty boring and tedious.

    One of the other posters used app inventor (this I assume). It might provide a nice shortcut but I know nothing about it.

    If you've a general grasp of programming but no specific experience with java or android I'd say you'd get it done in a day from scratch if you put your mind to it.

    I'd suggest throwing the problem at the folk in the math forum just to see if there's a simple solution.


  • Registered Users, Registered Users 2 Posts: 12,619 ✭✭✭✭errlloyd


    Both of those suspicions are defo true. I'd like to learn it tbh, I think this might be a good ish way to start. Thanks for all the help lads. Will see how it goes.


Advertisement