Question

Imagine your 2d vector is a chess board with alternating squares of white and black (...

Imagine your 2d vector is a chess board with alternating squares of white and black ( google for chess board if you don't know what one looks like ).

Write a method whiteMinusBlackSquares( vector<vector<int>> numbers ) that returns an integer.

return the sum of all values 'on' white squares minus the sum of all values 'on' black squares.

For example, the top left (0,0) square is white.

If I pass a vector<vector<int>> of { 1, 2, 3, 4 }, {5, 6, 7, 8}, {9,10, 11, 12}, { 13, 14, 15, 16}

1 and 3 would be on white squares in the first row

6 and 8 woudl be on white squares in the second row

9 and 11 would be on white squares in the third row

and 14 and 16 woudl be on white squares in the fourth row.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <vector>

using namespace std;

int whiteMinusBlackSquares( vector< vector< int > > numbers ) {
        int whites = 0;
        int blacks = 0;

        for(int i=0; i < numbers.size(); i++) {
                bool white = (i % 2 == 0);
                
                for(int j=0; j<numbers[i].size(); j++) {
                        if(white) {
                                whites += numbers[i][j];
                        } else {
                                blacks += numbers[i][j];
                        }
                        white = !white;
                }

        }

        return whites - blacks;
}

int main() {
        vector< vector< int > > of{{ 1, 2, 3, 4 }, {5, 6, 7, 8}, {9,10, 11, 12}, { 13, 14, 15, 16}};

        cout << whiteMinusBlackSquares(of) << endl;
}

clang version 7.0.0-3~ubun final) clang++-7-pthread -o ma . ./main int whiteMinusBlackSquares vector< vector< int > > numbers
Hi. Please find the answer above.. In case of any doubts, you may ask in comments. You may upvote the answer if you feel i did a good work!

Add a comment
Know the answer?
Add Answer to:
Imagine your 2d vector is a chess board with alternating squares of white and black (...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Concepts: multi-dimension array and the Knight's Tour Problem A chess board consists of an 8 x 8 "array" of squares: int board[ROW][COL]={0}; A knight may move perpendicular to the edges o...

    Concepts: multi-dimension array and the Knight's Tour Problem A chess board consists of an 8 x 8 "array" of squares: int board[ROW][COL]={0}; A knight may move perpendicular to the edges of the board, two squares in any of the 4 directions, then one square at right angles to the two-square move. The following lists all 8 possible moves a knight could make from board [3][3]: board[5][4] or board[5][2] or board[4][5] or board[4][1] or board[1][4] or board[1][2] or board[2][5] or board[2][1]...

  • In Python, starting with the 8x8 board solution that will be appended here add the following...

    In Python, starting with the 8x8 board solution that will be appended here add the following functionality: 1) Add code to create a list, containing 8 lists, with each of the 8 lists containing 8 null strings as values. Call the list of lists board. code provided by prof: import turtle validMovesList=['A0','A2','A4','A6','B1','B3','B5','B7', 'C0','C2','C4','C6','D1','D3','D5','D7', 'E0','E2','E4','E6','F1','F3','F5','F7', 'G0','G2','G4','G6','H1','H3','H5','H7','quit'] def drawSquare(t,length,color): t.fillcolor(color) t.begin_fill() for num in range(4): t.forward(length) t.left(90) t.end_fill() def drawRow(t,length,color1,color2): for i in range(4): drawSquare(t,length,color1) t.forward(length) drawSquare(t,length,color2) t.forward(length) def drawCircleFilled(t,size,color): t.fillcolor(color) t.begin_fill()...

  • Magic Squares; Java code

    Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic square if the sum ofthe elements in each row, in each column, and in the two diagonals is the same value. For example,16 3 2 135 10 11 89 6 7 124 15 14 1Write a program that reads in n2 values from the keyboard and tests whether they form a magic squarewhen arranged as a square matrix. You...

  • 11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single...

    11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single user-defined function that analyzes a 2D array of chars. You do NOT need to write any of the code in main0 that calls your function. Make sure to use the template and only add code to the user-defined function. Write a function win0 with the following definition (i.e. prototype): int win(char board17)15), char player) The function win0 should return a 1 if the character...

  • Please develop the following code using C programming and using the specific functions, instructi...

    Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...

  • SPSS Lab Assignment #2 Output Analysis One Descriptives Depression_13 2323 WHITE BLACK AMER IND ASIAN HISPAN...

    SPSS Lab Assignment #2 Output Analysis One Descriptives Depression_13 2323 WHITE BLACK AMER IND ASIAN HISPAN Mean 10.0010 10.0930 10.1038 10.1060 10.1062 10.0343 Std. Deviation 24975 27993 25361 27124 27267 26409 95% Confidence interval for Mean Std. ErrorLower Bound Upper Bound 00518 9.9908 10.0111 .00817 10.0770 10.1090 .03699 10.0294 10.1783 04952 10.0047 10.2073 .04158 10.0223 10.1901 00439 10.0257 10.0429 30 43 3617 Total ANOVA Depression 73 Mean Square Sum of Squares 7.230 244.967 1.808 26.651 Between Groups Within Groups Total...

  • Question 1) Suppose a program has the following code: const int X = 2, Y =...

    Question 1) Suppose a program has the following code: const int X = 2, Y = 3; int sum; int values[X][Y] = {{1, 2, 3},                                  {4, 5, 6}}; for(int i=0; i<X; i++) {      sum=0;      for(int j=0; j<Y; j++)         sum+=values[i][j];    cout<<sum<<endl; } What is this program getting the sum of? Group of answer choices D-) The columns of the 2D array C-) The rows of the 2D array A-) All of the elements of the 2D...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • Game Description: Most of you have played a very interesting game “Snake” on your old Nokia...

    Game Description: Most of you have played a very interesting game “Snake” on your old Nokia phones (Black & White). Now it is your time to create it with more interesting colors and features. When the game is started a snake is controlled by up, down, left and right keys to eat food which appears on random locations. By eating food snake’s length increases one unit and player’s score increases by 5 points. Food disappears after 15 seconds and appears...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT