Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB (Octave) Question You work at an electronics company that designs and man

ID: 3732285 • Letter: M

Question

MATLAB (Octave) Question

You work at an electronics company that designs and manufactures audio equipment. As an industrial engineer, you oversee (among other things) the production of a line of high end speakers. Your company's current practice is to produce the same number of units as were sold the previous week. The company also has a policy that states that if the inventory ever exceeds the sales of the previous week, the company will only produce half of the number of units as were sold the previous week (in order to ensure that inventory does not get too high). Your boss asks you to do an analysis on the inventory based on projected sales below:

Week: 1 2 3 4 5 6 7 8 9 10

Production: 50 50 55 60 etc.

Sales: 50 55 60 80 70 55 80 80 70 55

Assume that the first week's production is 50 speakers (based on week zero's numbers). Run the program for three cases--a) an initial inventory of 50 speakers and b) an initial inventory of 30 speakers and c) an initial inventory of 10 speakers. Does the inventory ever drop below zero? Should your company change their policy?

To illustrate this example, let's take the case of part a. You have an inventory of 50 speakers, you produce 50 speakers (for a total of 100 speakers), and then you sell 50 speakers. Thus, you are left with 100 - 50 = 50 speakers as your inventory starting off week 2. Then, on week 2, you produce 50 speakers (based on the previous week's sales), giving you a total of 50+50 =100 speakers, and then you sell 55, leaving you with an inventory of 100-55 = 45 speakers at the end of week 2.

Please be as simple as possible complete in full.

Explanation / Answer

MATLAB CODE:

#include       

#include       

#include       

#include       

#include       

#include       

#include       

#include

// PURPOSE: To tell the maximum value that a tree can have.

const int       MAX_VALUE               = 64;

// PURPOSE: To tell how many problems to do.

const int       NUM_PROBLEMS            = 4096;

#include        "Node.h"

#include        "NodeBuffer.h"

/*-------------------------------------------------------------------------*

---                                                                   ---

---           Node.h                                                  ---

---                                                                   ---

---       This file defines classes for nodes used to represent math ---

---   expressions.                                                    ---

---                                                                   ---

---   ----    ----    ----    ----    ----    ----    ----    ----    ---

---                                                                   ---

---                                                                   ---

*-------------------------------------------------------------------------*/

// PURPOSE: To distinguish among the mathematical operators.

typedef         enum            {

                                  ADD_OP,

                                  SUBTRACT_OP,

                                  MULTIPLY_OP,

                                  DIVIDE_OP,

                                  NUM_OPS

                                }

                                operator_ty;

// PURPOSE: To serve as the base class for the Node classes.

class           Node

{

public :

Node                          ()

                                { }

virtual

~Node                         ()

                                { }

virtual

double        eval            ()

                               const

                                = 0;

virtual

std::string   toString        ()

                                const

                                = 0;

};

// PURPOSE: To represent a constant.

class           ConstNode : public Node

{

double                        constant_;

public :

ConstNode                     () :

                                Node(),

                                constant_((double)((rand() % MAX_VALUE) + 1) )

                                { }

double        eval            ()

                                const

                                { return(constant_); }

std::string   toString        ()

                                const

                                {

                                  std::ostringstream    stream;

                                  stream << constant_;

                                  return(stream.str());

                                }

};

// PURPOSE: To return a randomly generated Node.

extern

Node*           makeNode        ();

// PURPOSE: To represent an operation.

class           OperatorNode : public Node

{

operator_ty                   operator_;

Node*                         lhsPtr_;

Node*                         rhsPtr_;

public :

OperatorNode                  () :

                                Node(),

                                operator_((operator_ty)(rand() % NUM_OPS)),

                                lhsPtr_(makeNode()),

                                rhsPtr_(makeNode())

                                { }

~OperatorNode                 ()

                                {

                                  delete(rhsPtr_);

                                  delete(lhsPtr_);

                                }

double        eval            ()

                                const

                                {

                                  double        lhs     = lhsPtr_->eval();

                                 double        rhs     = rhsPtr_->eval();

                                  double        result;

                                  switch (operator_)

                                  {

                                  case ADD_OP :

                              

                                    result      = lhs + rhs;

                                    break;

                                  case SUBTRACT_OP :

                                    result      = lhs - rhs;

                                    break;

                                  case MULTIPLY_OP :

                                    result      = lhs * rhs;

                                    break;

                                  case DIVIDE_OP :

                                    result      = lhs / rhs;

                                    break;

                                  }

                                  return(result);

                                }

std::string   toString        ()

                                const

                                {

                                  std::ostringstream    stream;

                                  const char*           operatorNameCPtr;

                                  switch (operator_)

                                  {

                                  case ADD_OP :

                                    operatorNameCPtr    = " + ";

                                    break;

                                  case SUBTRACT_OP :

                                    operatorNameCPtr    = " - ";

                                    break;

                                  case MULTIPLY_OP :

                                    operatorNameCPtr    = " * ";

                                    break;

                                  case DIVIDE_OP :

                                    operatorNameCPtr    = " / ";

                                    break;

                                  }

                                  stream << "(" << lhsPtr_->toString()

                                         << operatorNameCPtr

                                         << rhsPtr_->toString() << ")";

                                  return(stream.str());

                                }

};

/*-------------------------------------------------------------------------*

---                                                                   ---

---           NodeBuffer.h                                            ---

---                                                                   ---

---       This file defines a class that implements a thread-safe     ---

---   buffer of pointers to math expressions.                         ---

---                                                                   ---

---   ----    ----    ----    ----    ----    ----    ----    ----    ---

---                                                                   ---

---   Version 1a              2018 February 22        Joseph Phillips ---

---                                                                   ---

*-------------------------------------------------------------------------*/

class   NodeBuffer

{

enum { SIZE   = 16 };

Node*   array_[SIZE];

int   inIndex_;

int   outIndex_;

int   numItems_;

public :

NodeBuffer        ()

{

    for (int i = 0; i < SIZE; i++)

    {

      array_[i] = NULL;

    }

    inIndex = outIndex = numItems_ = 0;

}

~NodeBuffer       ()

{

}

int   getNumItems () const

{ return(numItems_); }

void putIn (Node* nodePtr)

{

    while (getNumItems() >= SIZE)

    {

    }

    array_[inIndex_] = nodePtr;

    inIndex_++;

    numItems_++;

    if (inIndex_ >= SIZE)

      inIndex_ = 0;

}

Node*   pullOut ()

{

    while (getNumItems() <= 0)

    {

    }

    Node* toReturn        = array_[outIndex_];

    array_[outIndex_]     = NULL;

    outIndex_++;

    numItems_--;

    if (outIndex_ >= SIZE)

      outIndex_ = 0;

    return(toReturn);

}

};

/*-------------------------------------------------------------------------*

---                                                                   ---

---           mathSolver.cpp                                          ---

---                                                                   ---

---       This file defines the high-level functions of the math      ---

---   generator and solver program.                                   ---

---                                                                   ---

---   ----    ----    ----    ----    ----    ----    ----    ----    ---

---                                                                   ---

---                                                                   ---

*-------------------------------------------------------------------------*/

//

//      Compile with:

//      $ g++ mathSolver.cpp -o mathSolver -lpthread -g

//

#include        "mathSolverHeader.h"

void           evaluate        (void          vPtr

                                )

{

NodeBuffer*   nodeBufferPtr   = (NodeBuffer*)vPtr;

// YOUR CODE HERE

}

// PURPOSE: To return a randomly generated Node.

Node*           makeNode        ()

{

return( (rand() % 3) ? (Node*)new ConstNode() : (Node*)new OperatorNode() );

}

int             main            (int            argc,

                                 char*          argv[]

                                )

{

NodeBuffer    nodeBuffer;

pthread_t     consumer0;

pthread_t     consumer1;

int           toReturn        = EXIT_SUCCESS;

srand( (argc < 2) ? getpid() : atoi(argv[1]) );

return(toReturn);

}