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

Remember \"The Matrix\" movie where the machines \"grow\" humans to provide a so

ID: 3809151 • Letter: R

Question

Remember "The Matrix" movie where the machines "grow" humans to provide a source of power? Assuming that the two classes, a Matrix and a HumanCell, have for been already defined, write code to implement a friend overloaded output operator list the Matrix class, which will output the Matrix instance's "name", followed by the of Human Cells plugged in. The Matrix holds a handle to a dynamic rectangle array of Human Cells called "battery". The attributes "rows" and "columns" hold the size of the rectangle terms of Human Cells. Assume that the Cell lot is full-there is a Human plugged in in every spot Assume that the output operator for a HumanCell class has been provided, and that it outputs a Human's name and age, an needed for the report

Explanation / Answer

PROGRAM CODE:

/*

* matrix.cpp

*

* Created on: 02-Apr-2017

* Author: kasturi

*/

#include <iostream>

using namespace std;

class HumanCell

{

public:

   string name;

   int age;

   HumanCell()

   {

       name = "";

       age = 0;

   }

   HumanCell(string n, int a)

   {

       name = n;

       age = a;

   }

   friend ostream& operator<<(ostream &o, HumanCell cell)

   {

       o<<cell.name<<", "<<cell.age;

       return o;

   }

};

class Matrix

{

private:

   int rowCounter;

   int colCounter;

public:

   string name;

   int rows, columns;

   HumanCell **battery;

   Matrix(string n, int row, int col)

   {

       name = n;

       rows = row;

       columns = col;

       battery = new HumanCell*[rows];

       rowCounter = 0;

       colCounter = 0;

   }

   void addHumanCells(HumanCell cell)

   {

       if(colCounter== columns)

       {

           colCounter = 0;

           rowCounter++;

       }

       if(colCounter ==0)

       {

           battery[rowCounter] = new HumanCell[columns];

       }

       battery[rowCounter][colCounter] = cell;

       colCounter++;

   }

   friend ostream& operator<<(ostream &out, Matrix max)

   {

       out<<"Matrix Name: "<<max.name<<endl;

       for(int i=0; i<max.rows; i++)

       {

           for(int j=0; j<max.columns; j++)

           {

               out<<"Cell ["<<to_string(i+1)<<"]["<<to_string(j+1)<<"]: ";

               out<<max.battery[i][j]<<endl;

           }

       }

       return out;

   }

};

int main()

{

   Matrix matrix("Blue Lagoon", 1,2);

   HumanCell cell1("Andy", 22);

   HumanCell cell2("Anna", 18);

   matrix.addHumanCells(cell1);

   matrix.addHumanCells(cell2);

   cout<<matrix;

}

OUTPUT:

Matrix Name: Blue Lagoon

Cell [1][1]: Andy, 22

Cell [1][2]: Anna, 18