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

I need help (comments through each function are a must) I need to write some cla

ID: 3875218 • Letter: I

Question

I need help (comments through each function are a must)

I need to write some classes for a 2-dimensional array of integers that can be “toggled” to 0 and back. Here are the classes and files that need to be created:

ToggleInt (define in a file “ToggleInt.h” – remember that C++ is case-sensitive)

A default constructor that sets the value to 0

A function value() that returns the current value – see the toggle() description

A function set(int) that sets the value of the object to the parameter

A function toggle() that “toggles” the value to 0 or back to the set value. Ex: if you called set(5), calling toggle() once causes the value() function to return 0. Calling toggle() again causes the value() function to return 5. Another call will cause value() to return 0 again, and so on.

ToggleGrid (define in a file ‘ToggleGrid.h”)

For this assignment, make the grid a 3 rows by 2 columns array of ToggleInt’s. Include a constructor that takes a one-dimensional array as a parameter and sets the values of the grid from the array. Assume that the array has exactly 6 values.

A function toggle(int row, int col) that toggles the ToggleInt at the specified coordinates. Do not return anything

A method toString() that returns a string representing the current values of the grid.

Use one line per row and end with a ‘ ’

Insert one space before each number, none after

For each number, append 0 if the number has been “toggled off”, the original value otherwise.

hw1driver.cpp – this file includes your main(). It should

output a description of the program

let the user initialize a ToggleGrid with user inputs – you do not need to handle errors, so assume the user inputs 6 integers that can fit in an int

and then let the user choose which squares to toggle, outputting the grid (use toString()) each time. The choice is indicated by 2 integers, the row and then the column of the square to toggle. Entering -1 as the row is the way to quit the program. A sample run’s output is added at the end.

As a general rule for both this and all future assignments, make sure to include all relevant header files, even if your IDE automatically does so. In particular, the and are sometimes automatically included by some IDE’s, but if you use anything from them you should #include the relevant headers.

Explanation / Answer

In ToggleInt.h:

class ToggleInt {

public:

// declaring variables to hold value of cell and current value of cell

int val, currval;

//constructor

ToggleInt() {

//initialise all values to zero as specified

val = 0;

currval = 0;

}

int value() {

//return current value of the cell

return currval;

}

void set(int x) {

//sets value of the cell as specified by the user

val = x;

currval = x;

}

void toggle() {

//toggles value based on the current value of the cell.

//If the current value is same as the set value, we need to change it to zero, else to the set value

if(val == currval)

currval = 0;

else

currval = val;

}

};

In ToggleGrid.h :

#include "ToggleInt.h"

#include <iostream>

using namespace std;

class ToggleGrid {

public:

//create an array of class ToggleInt to hold values

ToggleInt arr[3][2];

//constructor to initialise the value of each cell

ToggleGrid(int val[]) {

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

//create new object of ToggleInt class

ToggleInt newobj;

//Assign value to that object by calling set function

newobj.set(val[i]);

// (i/2) gives row number and (i%2) gives column number

// we assign new object to arr

arr[i/2][i%2] = newobj;

}

}

void toggle(int row, int col) {

//calls toggle function of ToggleInt class

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

}

void toString() {

//prints the entire grid

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

cout << arr[i/2][i%2].value();

if(i%2) //if it is last column

cout << endl;

else

cout << " ";

}

}

};

In hw1driver.cpp :

#include "ToggleGrid.h"

#include <iostream>

using namespace std;

int main() {

//description of the program, edit if needed.

cout << "The program toggles values on a 3 by 2 grid(row from 0 to 2 and columns from 0 to 1). Please enter initial values of the grid ";

int valarr[6];

//take inout from user

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

cin >> valarr[i];

}

//creating new grid with initial values as specified by user

ToggleGrid newgrid(valarr);

int cont = 1;

while(cont > 0) {

int x, y;

//get first value, if -1, user wants to exit, else this is row number

cin >> x;

if(x == -1) { //user wants to exit

cout << "exiting" << endl;

return 0;

}

cin >> y; //get column number

newgrid.toggle(x, y); //toggle cell specified by user

newgrid.toString(); //print the grid

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote