#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
 
  int i=0,j,h,v,k,t;
  float sc,pc,gv,pf[200],B,q,a=0,d,qr,ql[200],qp,n=1.0; //pf[] to store commulative profit,ql[] to store order quantity,qr to store lower bound of demand
  printf("\n Enter the selling price : ");
  scanf("%f",&sc);
  printf("\n Enter the purchase price : ");
  scanf("%f",&pc);
  printf("\n enter the daily demand : ");
  scanf("%f",&d);
  printf("\nenter the demand variance : ");
  scanf("%d",&v);
  printf("\nenter the garbage value : ");
  scanf("%f",&gv);
  printf("\nenter the penalty for not fullfilling demand : ");
  scanf("%f",&B);
 qr=d-3.0*v;
 printf("%f",qr);  //displaying lowest bound for demand
 h=6*v;
 srand(time(0));
 for(t=0;t<5;t++)  //loop to get replicates
  {
  for(i=0;i<h;i++)
   {
    pf[i]=0;                              //initially putting 0 in all commulative profit variable
    ql[i]=qr;                              //storing the minimum order quantity in the order quantity variable
   }
  for(i=0;i<h;i++)                          //this loop will execute 6*v times to cover all the order quantity in the range of +3*v to -3*v of the normally distributed demand
    {                 
                    ql[i]=ql[i]+i;        //in each loop the order quantity is incremented by 1 and stored at a specific location[i] in the array ql
                    printf("\n%f",ql[i]);    //optional to display the order quantity for which commulative profit is calculated
                   for(j=0;j<50;j++)       //executing this loop 50 times to generate random normally distributed demand to calculate commulative profit which is the sum of profit for the given order quantity for 50 random uniformally distributed demand 
                   {                       //when the mean demand becomes high then execute this loop more number of times to get more exact results
                   qp=qr+rand()%h;         //generating random demamnds
                   if(ql[i]<=qp)           //if order quantity is less than demand condition check
                            { 
                                 n=n+1.0;
                                 pf[i]=pf[i]+ql[i]*(sc-pc)-B*(qp-ql[i]);      //adding the profit generated to the initial value of commulativ profit
                            }
                   else if(ql[i]>qp)     //if order quantity is greater than the demand condition check
                       {    
                            n=n+1.0;
                            pf[i]=pf[i]+qp*sc-ql[i]*pc+gv*(ql[i]-qp); //adding the profit generated to the initial value of commulativ profit
                       }
                   }
     printf("\t%f",pf[i]); //displaying commulative profit for each of the order quantity
    }
 
 for(i=0;i<6*v;i++)   //this loop will search for the maximum commulative profit and the order quantity reffering to this profit is stored in q
 {
                   if(pf[i]>a)
                   {
                              a=pf[i];
                              q=ql[i];
                   }
 }
    printf("\noptimal order quantity = %f",q);//displaying the optimal order quantity 
  system("PAUSE");
 }  //replication loop ends here
   
  return 0;
}

________________________________________________________________________
          INPUT FORMAT
RR->recommended range for input
Enter the selling price :       \\price at which perishable items are sold(RR 3-5)
Enter the purchaserice :    \\RR 2-4 
Enter the daily demand :      \\RR 100-150
Enter the demand variance :    \\RR 5-18
Enter the garbage value :       \\RR .5-1.5
Enter the penalty for not fullfilling the demand :   \\.5-2
------------------------------------------------------------------------------------------------------
Output 
Accumulated profit for each of the order quantity
Displaying the optimal order quantity
New loop is executed for Getting the replicates
_________________________________________________________________________

