// L-36 MCS 360 Mon 20 Nov 2017 : weather.cpp

/*
  Consider a vector W of strings, which contains 3 weather type predictions,
  "summy", "cloudy", and "rain".
  Each weather type may occur with probability
  0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, or 0.1.
  The probabilities are stored in P, a vector of doubles.

  The string "90% sunny, 10% cloudy" corresponds to the prediction
  of 90% sunny and 10% cloudy weather.
  The sum of the probabilities in the prediction always equals 1.0,
  or 100% as shown in the prediction.

  Design a C++ function weather which takes on input the vectors W and P,
  to write all possible weather predictions, starting as below:

  90% sunny, 10% cloudy
  90% sunny, 10% rain
  90% cloudy, 10% rain
  80% sunny, 20% cloudy
  80% sunny, 20% rain
  80% sunny, 10% cloudy, 10% rain
  80% cloudy, 20% rain
  etc ...

  (1) Write the prototype of the function weather and document all
      input parameters of the function.
  (2) Give the definition of the function weather.
 */

#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void predict
 ( vector<string> W, vector<double> P,
   int Wstart, double sumprob, string accu );
/*
   Writes all possible weather predictions with probabilities.
   The input parameters are
   
   W       : weather types
   P       : probabilities
   Wstart  : start index in the vector W
   sumprob : sum of the chosen probabilities
   accu    : accumulates the output string
 */

int main ( void )
{
   vector<string> weathertypes;
   weathertypes.push_back("sunny");
   weathertypes.push_back("cloudy");
   weathertypes.push_back("rain");

   vector<double> probabilities;

   for(int i=9; i>0; i--)
      probabilities.push_back(i/10.0);

   predict(weathertypes, probabilities, 0, 0.0, "");

   return 0;
}

void predict
 ( vector<string> W, vector<double> P,
   int Wstart, double sumprob, string accu )
{
   if(sumprob == 1.0) cout << accu << endl;
   if(sumprob < 1.0)
   {
      for(int i=0; i<P.size(); i++)
      {
         ostringstream prob;
         prob << P[i]*100;
         string work = prob.str() + "% ";

         for(int j=Wstart; j<W.size(); j++)
         {
            string morework;
            if(accu == "")
                morework = work + W[j];
            else
                morework = ", " + work + W[j];

            predict(W, P, j+1, sumprob+P[i], accu+morework);
         }
      }
   }
}
