Question

Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the...

Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the sums of its diagonals.

Function Description

Complete the diagonalDifference function described below to calculate the absolute difference between diagonal sums.

diagonalDifference( integer: a_size_rows, integer: a_size_cols, integer array: arr)
Parameters:
a_size_rows: number of rows in array
a_size_cols: number of columns in array
a: array of integers to process
Returns: integer value that was calculated

Constraints

-100 < = elements of the matrix < = 100

Raw Input Format

The first line contains a single integer,  denoting the number of rows and columns in the matrix .
The next  lines denote the matrix 's rows, with each line containing  space-separated integers describing the columns.

Sample Input 0

3
11 2 4
4 5 6
10 8 -12

Sample Output 0

15

Explanation 0

The primary diagonal is:

11
   5
     -12

Sum across the primary diagonal: 11 + 5 - 12 = 4

The secondary diagonal is:

     4
   5
10

Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 - 19| = 15

Note: |x| is the absolute value of x

#include <bits/stdc++.h>

using namespace std;

/*
* Complete the diagonalDifference function below.
*/
int diagonalDifference(vector<vector<int>> a) {
/*
* Write your code here.
*/

}

Please write the pseudocode along with the C++ source and explain in full detail both the pseudocode and the source code. Thanks. Mathematically speaking, I know we are trying to sum both diagonal lines of a square matrix and so we would have to use matrix notation for the pseudocode to accompisl the diagonal difference. Is this right?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int diagonalDifference(vector<vector<int>> a) {

int sumFirst = 0, sumSecond = 0;

int N = a.size();

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

sumFirst += a[i][i];

}

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

sumSecond += a[i][N - i - 1];

}

  

return abs(sumFirst - sumSecond);

}

===========
It passed all the test
Test Case #0 Test Case #3 Test Case #6 Test Case #9 Test Case #1 Test Case #4 Test Case #7 Test Case #10 Test Case #2 Test Ca

Run and let me know if there is any concern


PSEUDOCODE

Algorithm DiagonalDifference(Vector<Vector>A)

START

N = A.size()
sumF = 0
while i<N
sumF = sumF + A[i][i]
i = i + 1

i = 0, sumS = 0
while i<N
sumS = sumS +
[i][N - i - 1];
i = i + 1

RETURN abs(sumS - sumF)

END

Add a comment
Know the answer?
Add Answer to:
Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the...
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
  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • Using C programming...

    Write a text file contains a square matrix of integer numbers. The file contains an integer number indicating the identical number of rows and columns followed by the data itself (that number cannot be larger than 100). For example, a file containing a 3x3 matrix would contain 3 followed by 9 other integer numbers. The program will call one function to determine if all the numbers on the main diagonal are the same.The function is called checkdiag (int checkdiag (int matrix[][100], int...

  • // Use the following Project: Perfect Square Table    A “Perfect Square Table” is a square...

    // Use the following Project: Perfect Square Table    A “Perfect Square Table” is a square of positive integers such that the    sum of each row, column, and diagonal is the same constant.    This program reads square tables from files, checks if they are perfect squares,    and displays messages such as “This is a Perfect Square Table with a constant of 34!”    or “This is not a Perfect Square Table”.    NAME:    IDE:    */...

  • Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e....

    Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e. a two-dimensional array), number of rows and number of columns as input and prints the matrix after rotating it 90 degrees clockwise. Example: void rotate-matrix (int* matrix, int row, int column) Inputs: row 4, column-6 and the pointer to the matrix below: 2 3 6 5 8 0 Output:

  • Theory: A vector with nonnegative entries is called a probability vector if the sum of its entries is 1. A square matrix...

    Theory: A vector with nonnegative entries is called a probability vector if the sum of its entries is 1. A square matrix is called right stochastic matrix if its rows are probability vectors; a square matrix is called a left stochastic matrix if its columns are probability vectors; and a square matrix is called a doubly stochastic matrix if both the rows and the columns are probability vectors. **Write a MATLAB function function [S1,S2,P]=stochastic(A) which accepts a square matrix A...

  • Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array...

    Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array is randomly initialized (using Math.random()). Write a method to find the maximum value in this two dimensional array; Write a method to find the average value in the array; Write a method to count how many elements are smaller than average; Write a method to copy contents in a two-dimensional array to a one-dimensional array. The method will return this one-dimensional array. Method is...

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • 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...

  • how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagona...

    how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagonals have the same sum. For example, A- 3 5 7 4 9 2 Complete the following magic matrices 17? ?? 3 ? 2 ? 2? ? Do the following steps in each case: 1. Write the system of equations and put it under the...

  • ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given....

    ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...

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