/* Code:  Convex Exemplar SPP1200/XA-16 version of starter C problem          */
/* startcc.c -- cleaned up F97 version of start.c from borgstart.f using f2c  */
/* start.f -- translated by f2c (version 19940808).                           */
#include <stdio.h>
#include <math.h>
#include <time.h>

/* Define Global Parameters */
#define n 200
#define nn 6
#define nnd 5
#define NTime 20

FILE *out;

/* Main program */ 
main()
{
/* System generated locals */
    int i__1;
    double d__1;

/* Implicit Local variables */
    int i, j, k, l, m;
    int kt, n2, nd, ndd;
    double sm, tm, dev;

/* Local variables */
    double cog[n], fractn[n], box[n][3], t[n], y[n];
    double z[n], s[n], a[n], b[n], am[n], vm[n], zm[n];
    double d1[n][n], d2[n][n], d3[n][n], fv[3][n][n];
    int kk[n], ll[n], mm[n];

/* Time variables */
    int get_time[NTime];
    float ts1, tt1, tu1, tu2, ts2, tt2, tu3, ts3, tt3;
    double ttot[NTime], ttotf;

/* Initialized data */
    static double cutl = 6.;
    static double cinc = 4.;
    static double alfa = 3.6;
    static double xbar = 8.;

/* external functions */
    extern double fm_();
    extern /* Subroutine */ int sub_();
    extern int timeofday();

/* begin main code */
    kt = 1;
    get_time[0] = timeofday();
    ++kt;
    get_time[kt] = timeofday();
    /* do 5 i loop */
    for (i = 1; i <= n; ++i) {
	cog[i - 1] = (float)1.;
	fractn[i - 1] = (float)1.;
	t[i - 1] = (float)1.;
	y[i - 1] = (float)1.;
	s[i - 1] = (float)1.;
	z[i - 1] = (float)1.;
	a[i - 1] = (float)1.;
	b[i - 1] = (float)1.;
	mm[i - 1] = 1;
	kk[i - 1] = 1;
	ll[i - 1] = 1;
        /* do 5 j loop */
	for (j = 1; j <= 3; ++j) {
/* L5: */   box[i-1][j-1] = j * (float)1.5 * i / ((float)n+1.);
	}
    }
    /* do 6 i loop */
    for (i = 1; i <= n; ++i) {
        /* do 6 j loop */
	for (j = 1; j <= n; ++j) {
	    d1[i-1][j-1] = (float)12.5;
	    d2[i-1][j-1] = (float)12.5;
/* L6: */   d3[i-1][j-1] = (float)14.5;
	}
    }
    /* do 10 i loop */
    for (i = 1; i <= n; ++i) {
/* L10: */ y[i - 1] = t[i - 1] + s[i - 1];
    }
    /* do 20 m loop */
    for (m = 1; m <= n; ++m) {
	s[m - 1] = a[m - 1] * m;
/* L20: */ t[m - 1] = y[m - 1] + alfa + (float)2.7;
    }
    /* do 30 i loop */
    for (i = 1; i <= n; ++i) {
	cog[i - 1] = fractn[i - 1] / (float)6.;
/* L30: */ mm[i - 1] = kk[i - 1] * ll[i - 1] / 4;
    }
    k = 1;
    /* do 50 i loop */
    for (i = 1; i <= n; ++i) {
	k += 2;
	cog[i - 1] = cutl + (i - 1) * cinc;
	dev = cog[i - 1] + alfa - xbar;
        /* do 50 j loop */
	for (j = 1; j <= 3; ++j) {
/* L50: */  box[i-1][j-1] += (float).25;
	}
    }
    for (i = 1; i <= n; ++i) {
	for (j = 1; j <= n; ++j) {
	    for (k = 1; k <= n; ++k) {
		d1[i-1][j-1] = d1[i-1][j-1] + d2[i-1][j-1] + d3[i-1][j-1];
		for (l = 1; l <= 5; ++l) {
/* L60: */          d3[i-1][l-1] = d2[i-1][l-1] * d1[i-1][l-1];
		}
	    }
	}
    }
    for (i = 1; i <= n; ++i) {
	for (j = 1; j <= 3; ++j) {
/* L70: */  t[i - 1] -= box[i][j];
	}
    }
    for (i = 2; i <= n; ++i) {
	a[i - 1] = b[i - 1] * box[i - 1][0];
/* L80: */ z[i - 1] = t[i - 1] * z[i - 2] + cog[i - 1];
    }
    for (i = 4; i <= n-2; ++i) {
	b[i - 1] = s[i - 4];
	s[i - 1] = y[i - 1];
        cog[i - 1] = b[i - 1] * cutl;
/* L90: */ cog[i + 1] = y[i - 1] * xbar;
    }
    l = 1;
L100:
    y[l - 1] = t[l - 1] * s[l - 1];
    l += 2;
    if (l <= n) {
	goto L100;
    }
    sm = 0.;
    for (i = 1; i <= n; ++i) {
/* L110: */ sm += sqrt(exp(i * (float)-.25) + (float)1.);
    }
    for (i = 1; i <= n; ++i) {
	i__1 = i;
        tm = exp(i__1 * i__1 * (float)-.21) + 1 + sin(i * (float).25 + 6);
	d__1 = tm;
/* L120: */ am[i - 1] = tm * (float).9 / (d__1 * d__1 + 1);
    }
    for (i = 1; i <= n; ++i) {
/* L130: */ vm[i - 1] = fm_(&am[i - 1]);
    }
    for (i = 1; i <= 3; ++i) {
	for (j = 1; j <= n; ++j) {
	    for (k = 1; k <= n; ++k) {
		fv[i-1][j-1][k-1] = box[j-1][i-1]*(float).03*box[k-1][(3-i)]
		            /(box[j-1][i-1]*(float).34 + 1);
/* L140: */
	    }
	}
    }
    for (i = 1; i <= n; ++i) {
	sub_(am, vm, zm);
/* L150: */
    }
/* Clock: Elapsed Total Time: */
    ++kt;
    get_time[kt] = timeofday();
    ttot[kt] = get_time[kt] - get_time[1] - (get_time[1] - get_time[0]);
    ttotf = ttot[kt]/1.e6;
    nd=(n-1)/(nn-1);
    ndd=(n-1)/(nnd-1);
    n2=(n+1)/2;
    printf("\n Borg Starter Problem Output\n");
    printf("\n  Timing Output:\n");
    printf("\n   final total time=%12.4e,in seconds\n",ttotf);
/* Change:  Extra output statements: */
    printf("\n    a(1:nn):");
    for (i = 0; i < nn; ++i){printf("%11.3e",a[i]);}
    printf("\n   am(1:nn):");
    for (i = 0; i < nn; ++i){printf("%11.3e",am[i]);}
    printf("\n    b(1:nn):");
    for (i = 0; i < nn; ++i){printf("%11.3e",b[i]);}
    printf("\n     1:n:nd:");
    for (i = 0;i < n; i += nd){printf("%11d",i);}
    printf("\n  b(2:n:nd):");
    for (i = 1;i < n; i += nd){printf("%11.3e",b[i]);}
    printf("\n   box(i,2):");
    for (i = 0;i < n; i += nd){printf("%11.3e",box[i][1]);}
    printf("\ncog(2:n:nd):");
    for (i = 1;i < n; i += nd){printf("%11.3e",cog[i]);}
    printf("\n        dev:%11.3e",dev);
    printf("\n    1:n:ndd:");
    for (i = 0;i < n; i += ndd){printf("%13d",i);}
    printf("\n    d1(1,j):");
    for (j = 0;j < n; j += ndd){printf("%13.5e",d1[0][j]);}
    printf("\n   d1(n2,j):");
    for (j = 0;j < n; j += ndd){printf("%13.5e",d1[n2-1][j]);}
    printf("\n    d1(n,j):");
    for (j = 0;j < n; j += ndd){printf("%13.5e",d1[n-1][j]);}
    printf("\n    d1(i,1):");
    for (i = 0;i < n; i += ndd){printf("%13.5e",d1[i][0]);}
    printf("\n   d1(i,n2):");
    for (i = 0;i < n; i += ndd){printf("%13.5e",d1[i][n2-1]);}
    printf("\n    d1(i,n):");
    for (i = 0;i < n; i += ndd){printf("%13.5e",d1[i][n-1]);}
    printf("\n   d2(n2,j):");
    for (j = 0;j < n; j += ndd){printf("%13.5e",d2[n2-1][j]);}
    printf("\n    d3(i,1):");
    for (i = 0;i < n; i += ndd){printf("%13.5e",d3[i][0]);}
    printf("\n   d3(i,n2):");
    for (i = 0;i < n; i += ndd){printf("%13.5e",d3[i][n2-1]);}
    printf("\n    d3(i,n):");
    for (i = 0;i < n; i += ndd){printf("%13.5e",d3[i][n-1]);}
    printf("\n  fractn(i):");
    for (i = 0;i < n; i += nd){printf("%11.3e",fractn[i]);}
    printf("\n fv(1,n2,k):");
    for (k = 0;k < n; k += nd){printf("%11.3e",fv[0][n2-1][k]);}
    printf("\n fv(3,n2,k):");
    for (k = 0;k < n; k += nd){printf("%11.3e",fv[2][n2-1][k]);}
    printf("\n fv(1,k,n2):");
    for (k = 0;k < n; k += nd){printf("%11.3e",fv[0][k][n2-1]);}
    printf("\n fv(3,k,n2):");
    for (k = 0;k < n; k += nd){printf("%11.3e",fv[2][k][n2-1]);}
    printf("\n          k:%11d",k);
    printf("\n         kt:%11d",kt);
    printf("\n   kk(1:nn):");
    for (i = 0; i < nn; ++i){printf("%11d",kk[i]);}
    printf("\n   ll(1:nn):");
    for (i = 0; i < nn; ++i){printf("%11d",ll[i]);}
    printf("\n   mm(1:nn):");
    for (i = 0; i < nn; ++i){printf("%11d",mm[i]);}
    printf("\n         nd:%11d",nd);
    printf("\n        ndd:%11d",ndd);
    printf("\n         n2:%11d",n2);
    printf("\n    s(1:nn):");
    for (i = 0; i < nn; ++i){printf("%11.3e",s[i]);}
    printf("\n  s(2:n:nd):");
    for (i = 1;i < n; i += nd){printf("%11.3e",s[i]);}
    printf("\n         sm:%11.3e",sm);
    printf("\n  t(1:n:nd):");
    for (i = 0;i < n; i += nd){printf("%11.3e",t[i]);}
    printf("\n         tm:%11.3e",tm);
    printf("\n   vm(1:nn):");
    for (i = 0; i < nn; ++i){printf("%11.3e",vm[i]);}
    printf("\n  y(1:n:nd):");
    for (i = 0;i < n; i += nd){printf("%11.3e",y[i]);}
    printf("\n  z(1:n:nd):");
    for (i = 0;i < n; i += nd){printf("%11.3e",z[i]);}
    printf("\n   zm(1:nn):");
    for (i = 0; i < nn; ++i){printf("%11.3e",zm[i]);}
} /* end MAIN__ */

/* function */
double fm_(x)
double *x;
{
/* System generated locals */
    double ret_val;

    ret_val = *x * (float).532 * ((float)17.25 - *x) + (float)48.66;
    return ret_val;
} /* fm_ */

/* Subroutine */ int sub_(a, v, z)
double *a, *v, *z;
{
/* System generated locals */
    int i__1;
/* Local variables */
    int i;

/* Function Body */
    i__1 = n;
    for (i = 1; i <= i__1; ++i) {
	if (a[i] > v[i]) {
	    z[i] = a[i];
	} else {
	    z[i] = a[i] + v[i] * (float)2.6;
	}
/* L1: */
    }
    return 0;
} /* sub_ */

/* timeofday Microsecond Wall Timer procedure using gettimeofday C function  */
/* reference as "[variable] = timeofday()" getting microsecond wall time     */
int timeofday()
{
   long int tmicro,t1[2],t2[2];
   int gtod;
   float tod;
   gtod=gettimeofday(t1,t2);
/* gettimeofday returns gotod = 0 if successful; t2 gives the timezone;
   t1[0] in secs since 1/1/70, t1[1]in added microseconds */
/* cut t1[0] down to trailing  4 digits for Cray 8 byte float answer return */
   tmicro=(t1[0]-(t1[0]/10000)*10000)*1000000+t1[1];
   tod=tmicro/1000000.; /* big loss of microsec accuracy=> don't return */
/* testing print: remove comments for checking  */
/* printf("tsecs,tmicrosecs,tmicro,TOD= %d secs %d microsec %d %16.6f sec\n" 
                                 ,t1[0],t1[1],tmicro,tod) ;  */
/* return answer in microseconds integer accuracy; avoiding division truncation;
   with 5 leading digits truncated, OK if time < hour, BUT subject to integer
   OverFlow as are most integer clocks, check of time < 0 in application code */
   return(tmicro);
}

