#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpc.h>

#define DELTA (0.5)

typedef struct {
          double len;
          double wid;
          double hei;
          double mass;
} rail;

double Density(double x, double y, double z) {
  return 6.0 * sqrt( exp( sin( sqrt( x*y*z ) ) ) );
}

int [*]main(int [host]argc, char **[host]argv) {
  repl N=3;
  
  if(argc>1)
    N = [host]atoi(argv[1]);
  if(N>0) {
    static rail [host]steel_hedgehog[[host]N];
    double [host]start;
    int [host]i;

    for(i=0; i<[host]N; i++) {
      steel_hedgehog[i].len = 200.0 * (i+1);
      steel_hedgehog[i].wid = 5.0 * (i+1);
      steel_hedgehog[i].hei = 10.0 * (i+1);
    }
    start = [host]MPC_Wtime();
    {
      net SimpleNet(N) mynet;

      [mynet]: {
        rail [mynet]myrail;
        double [mynet]x, [mynet]y, [mynet]z;

        myrail = steel_hedgehog[];
        for(myrail.mass=0., x=0.; x<myrail.len; x+=DELTA)
          for(y=0.; y<myrail.wid; y+=DELTA)
            for(z=0.; z<myrail.hei; z+=DELTA)
              myrail.mass += [mynet]Density(x,y,z);
        myrail.mass *= DELTA*DELTA*DELTA;
        [mynet]MPC_Printf("Rail #%d is %gcm x %gcm x%gcm and weights %g kg\n",
                          I coordof mynet, myrail.len, myrail.wid,
                          myrail.hei, myrail.mass/1000.0);
        [host]printf("The steel hedgehog weights %g kg\n",
                     [host]( (myrail.mass)[+]) / 1000.0 );
      }
    }
    [host]printf("\nIt took %.1f seconds to run the program.\n",
               [host]MPC_Wtime()-start );
  }
  else
    [host]printf("Wrong input (N=%d)\n", [host]N);    
}