#include main() { float p1, p2, p; float cache=10000.00, remaining; int shares=0; float increase2=0.0, increase1,increase; scanf("%f",&p2); int extra_share; scanf("%f",&p1); increase1=(p1-p2)*100/p1; printf("Share price changes Action\n\n"); printf("%.2f ---> %f\n",p2, increase2); printf("%.2f ---> %f\n",p1, increase1); while(scanf("%f",&p) !=EOF) { increase=(p-p1)*100/p1; printf("%.2f ---> %.2f", p, increase); if( (increase2 > 10) && (increase1 > 10)) { //sell cache = cache + shares * p1; printf(" sold cache = %.2f", cache); } else if ( (increase2 >5) && (increase1 >5) ) { // buy half extra_share = (int) (0.5*cache/p1); shares+=extra_share; cache = cache - extra_share*p1; printf(" bought 1/2 %d shares remaining %.2f",extra_share, cache); } else if ( (increase2 <0) && (increase1 <0) ) { // buy all extra_share = (int) (cache/p1); shares+=extra_share; cache = cache - extra_share*p1; printf(" bought all %d shares remaining %.2f",extra_share, cache); } else {// do nothing printf(" No action "); } printf("\n"); p2=p1; increase2=increase1; p1=p; increase1 = increase; } //display total cache=cache+ shares*p2; if (cache > 10000) printf("Gain:$%.2f\n",cache-10000.00); else printf("Loss:$%.2f\n",10000.00-cache); }