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

I need help with this assignment 1. I have to modify my ToggleGrid class code do

ID: 3875867 • Letter: I

Question


I need help with this assignment

1. I have to modify my ToggleGrid class code done prevously, CODE shown at the end of procedure:

I must to do the following in order to modify it:

·         Support a method sumRow(int) which takes a row index (the first row is row 0) and returns the sum of the current values of the ToggleInt’s in the row. Note that you get the current value by calling ToggleInt’s value() method

·         Support a method sumColumn(int) which does the same for a column of the grid

·         Support the == operator. This will consider two ToggleGrid’s to be equal if every corresponding row has the same sum (as indicated by sumRow()) and every corresponding column has the same sum (as indicated by sumColumn())

o    Ex: the following grids are considered equal

1 2             0 3

2 1             3 0

4 4             4 4

2.      Write a new driver hw2driver.cpp to test your functions.

Initialize a grid, toggle a value in each selected row and column. Then display the grid, then the results of one call to sumRow (indicate in the output which row you are summing), one call to sumColumn (indicate the column). For convenience, hard-code which row and which column to add up.

Now try the == operator. Include one case where the grids are equal, one where they are not. Your output should show the grids and then the result of the == comparison.

Zip together your files into a .zip/.dmg/.tgz file and submit that

It must have :

·         sumRow, sumColumn

·         hw1driver.cpp

·         Comments, something for each method/function, other comments to explain parts of the code when it's not clear

·         Choosing appropriate names for variables, functions, constants, classes, packages.

Here is my code:

-------------------------------------------------------------------------
FILE NAME: ToggleInt.h

#ifndef _TOGGLE_INT_

#define _TOGGLE_INT_

//class toggle in definition

class ToggleInt {

     public:

     //variables to store the current and previous valus

     int gridVal, gridCurrVal;

     //default constructor

     ToggleInt() {

          //set the value to 0

          gridVal = 0;

          gridCurrVal = 0;

     }

     //value to return the current value

     int value() {

          //return current value of the cell

          return gridCurrVal;

     }

     //method to set the value

     void set(int x) {

          //set the value of the object

          gridVal = x;

          gridCurrVal = x;

     }

     //method

     void toggle() {

          //if the original value is there

          if(gridVal == gridCurrVal)

              //toggle to 0

              gridCurrVal = 0;

          //if value was 0, restore the previous value

          else

              gridCurrVal = gridVal;

     }

};

#endif
--------------------------------------------------------------------------
FILE NAME:: ToggleGrid.h

#ifndef _TOGGLE_GRID_

#define _TOGGLE_GRID_

#include "ToggleInt.h"

#include

using namespace std;

//class ToggleGrid

class ToggleGrid {

     public:

     //create the Toggle int array of size 3*2

     ToggleInt togIntArray[3][2];

     //class constructor

     ToggleGrid(int val[]) {

     for(int itr = 0; itr < 6; itr++) {

          //create the Toggleint object

          ToggleInt togIntObj;

          //assign the value

          togIntObj.set(val[itr]);

          //set the geid value

          togIntArray[itr/2][itr%2] = togIntObj;

          }

     }

     //method to toggel the specified coordinates

     void toggle(int row, int col) {

          //perform the toggle

          togIntArray[row][col].toggle();

     }

     //method toString

     void toString() {

          cout<<"The grid's current values:"<

          //print the gid

          for(int itr = 0; itr < 6; itr++) {

              cout << togIntArray[itr/2][itr%2].value();

              if(itr%2)

                   cout << endl;

              else

              cout << " ";

          }

     }

};

#endif
------------------------------------------------------------------------
FILE NAME: hw1driver.cpp

#include "ToggleGrid.h"

#include

using namespace std;

int main() {

     //output the description of the program

     cout << "This program tests the ToggleGrid class ";

     cout << "Start by entering the 6 integer values for the grid, separated by spaces ";

     //Array to store the user input array

     int initalArray[6];

     //take six elements

     for(int itr = 0; itr < 6; itr++) {

          cin >> initalArray[itr];

     }

     //create a ToggleGrid object

     ToggleGrid gridObj(initalArray);

     cout<<"Now enter the row and column you want to toggle ";

     cout<<"Enter -1 when you want to quit ";

     //The while loop to infinte execution

     while(1) {

        

          //to store the row and column values

          int r, c;

          //take the row

          cin >> r;

          //the exit condition

          if(r == -1) {

              cout << "Exiting the program" << endl;

              return 0;

          }

          //get the column number

          cin >> c;

        

          //toggle the grid

          gridObj.toggle(r, c);

        

          //print using toString() method

          gridObj.toString();

          //display the message

          cout<<" Next square to toggle: ";

     }

     return 0;

}

Explanation / Answer

Given below are the modified files. Please do rate the answer if it was helpful. Thank you


ToggleInt.h (just add const to function value())
==========
#ifndef _TOGGLE_INT_

#define _TOGGLE_INT_

//class toggle in definition

class ToggleInt {
  
public:
  
//variables to store the current and previous valus
  
int gridVal, gridCurrVal;
  
//default constructor
  
ToggleInt() {
  
//set the value to 0
  
gridVal = 0;
  
gridCurrVal = 0;
  
}
  
//value to return the current value
  
int value() const {
  
//return current value of the cell
  
return gridCurrVal;
  
}
  
//method to set the value
  
void set(int x) {
  
//set the value of the object
  
gridVal = x;
  
gridCurrVal = x;
  
}
  
//method
  
void toggle() {
  
//if the original value is there
  
if(gridVal == gridCurrVal)
  
//toggle to 0
  
gridCurrVal = 0;
  
//if value was 0, restore the previous value
  
else
  
gridCurrVal = gridVal;
  
}
  
};

#endif


ToggleGrid.h
=============
#ifndef _TOGGLE_GRID_

#define _TOGGLE_GRID_

#include "ToggleInt.h"

#include <iostream>

using namespace std;

//class ToggleGrid

class ToggleGrid {
  
public:
  
//create the Toggle int array of size 3*2
  
ToggleInt togIntArray[3][2];
  
//class constructor
  
ToggleGrid(int val[]) {
  
for(int itr = 0; itr < 6; itr++) {
  
//create the Toggleint object
  
ToggleInt togIntObj;
  
//assign the value
  
togIntObj.set(val[itr]);
  
//set the geid value
  
togIntArray[itr/2][itr%2] = togIntObj;
  
}
  
}
  
//method to toggel the specified coordinates
  
void toggle(int row, int col) {
  
//perform the toggle
  
togIntArray[row][col].toggle();
  
}
  
//method toString
  
void toString() {
  
cout<<"The grid's current values:" << endl;
  
//print the gid
  
for(int itr = 0; itr < 6; itr++) {
  
cout << togIntArray[itr/2][itr%2].value();
  
if(itr%2)
  
cout << endl;
  
else
  
cout << " ";
  
}
  
}
  
  
//method to return sum of a given row
int sumRow(int row) const
{
int sum = 0;
if(row >= 0 && row < 3)
{
for(int col = 0; col < 2; col++)
sum += togIntArray[row][col].value();
}
return sum;
}
  
  
//method to return sum of a given column
int sumColumn(int col) const
{
int sum = 0;
if(col >= 0 && col < 2)
{
for(int row = 0; row < 3; row++)
sum += togIntArray[row][col].value();
}
return sum;
}
  
  
bool operator == (const ToggleGrid& grid2)
{
for(int row = 0; row < 3; row++)
{
if(sumRow(row) != grid2.sumRow(row))
return false;
}
  
for(int col = 0; col < 2; col++)
{
if(sumColumn(col) != grid2.sumColumn(col))
return false;
}
  
return true;
}

  
  
  
};

#endif

hw2driver.cpp
============
#include "ToggleGrid.h"

#include <iostream>

using namespace std;

int main() {
  
//output the description of the program
  
cout << "This program tests the ToggleGrid class ";
  
cout << "Start by entering the 6 integer values for the grid, separated by spaces ";
  
//Array to store the user input array
  
int initalArray[6];
  
//take six elements
  
for(int itr = 0; itr < 6; itr++) {
  
cin >> initalArray[itr];
  
}
  
//create a ToggleGrid object
  
ToggleGrid gridObj(initalArray);
  
cout<<"Now enter the row and column you want to toggle ";
  
cout<<"Enter -1 when you want to quit ";
  
//The while loop to infinte execution
  
while(1) {
  
  
  
//to store the row and column values
  
int r, c;
  
//take the row
  
cin >> r;
  
//the loop termination condition
  
if(r == -1) {
break;
}
  
//get the column number
  
cin >> c;
  
  
  
//toggle the grid
  
gridObj.toggle(r, c);
  
  
  
//print using toString() method
  
gridObj.toString();
  
//display the message
  
cout<<" Next square to toggle: ";
  
}
  
cout << "The sum of row 0 is " << gridObj.sumRow(0) << endl;
cout << "The sum of column 1 is " << gridObj.sumColumn(1) << endl;
  
cout << "---------------" << endl<< endl;

cout << "Testing == operator" << endl;
ToggleGrid g1((int[]){1, 2, 2, 1, 4, 4});
ToggleGrid g2((int[]){1, 3, 3, 2, 4, 4});
  
cout << "Testing not equal case" << endl;
cout << "Grid 1 " << endl;
g1.toString();
cout << "Grid 2 " << endl;
g2.toString();
if(g1 == g2)
cout << "Grid1 is equal to Grid 2" << endl;
else
cout << "Grid1 is NOT equal to Grid 2" << endl;
  
cout << "---------------" << endl<< endl;
cout << "Testing equal case" << endl;
g2.toggle(0, 0);
g2.toggle(1, 1);
cout << "Grid 1 " << endl;
g1.toString();
cout << "Grid 2 " << endl;
g2.toString();
if(g1 == g2)
cout << "Grid1 is equal to Grid 2" << endl;
else
cout << "Grid1 is NOT equal to Grid 2" << endl;
  
return 0;
  
}


output
=======

This program tests the ToggleGrid class
Start by entering the 6 integer values for the grid, separated by spaces
1 2 3 4 5 6
Now enter the row and column you want to toggle
Enter -1 when you want to quit
0 0
The grid's current values:
0 2
3 4
5 6

Next square to toggle: 2 1
The grid's current values:
0 2
3 4
5 0

Next square to toggle: -1
The sum of row 0 is 2
The sum of column 1 is 6
---------------

Testing == operator
Testing not equal case
Grid 1
The grid's current values:
1 2
2 1
4 4
Grid 2
The grid's current values:
1 3
3 2
4 4
Grid1 is NOT equal to Grid 2
---------------

Testing equal case
Grid 1
The grid's current values:
1 2
2 1
4 4
Grid 2
The grid's current values:
0 3
3 0
4 4
Grid1 is equal to Grid 2