Skip to content

Instantly share code, notes, and snippets.

@injust90
Last active December 26, 2022 21:42
Show Gist options
  • Save injust90/230d89602d177307d8ce06fd112c5a9b to your computer and use it in GitHub Desktop.
Save injust90/230d89602d177307d8ce06fd112c5a9b to your computer and use it in GitHub Desktop.
Cross counting X pattern in a vector
#include <iostream>
#include <vector>
using namespace std;
// Defining rows and columns of
// vector of vectors
int main()
{
int diagonalRight = 0;
int diagonalLeft = 0;
int absDifference = 0;
// Initializing the vector of vectors
vector<vector<int>> vect
{
{1, 2, 3},
{4, 5, 6},
{10, 8, 9}
};
// Print the values we just declared
for (int i = 0; i < vect.size(); ++i)
{
for (int j = 0; j < vect[i].size(); ++j)
{
if (i == j)
{
diagonalRight += vect[i][j];
}
if (i == vect.size() - 1 - j)
{
cout << "vect[" << i << "]" << "[" << j << "]" << endl;
diagonalLeft += vect[i][j];
}
}
}
absDifference = diagonalRight - diagonalLeft;
if (absDifference < 0)
{
absDifference *= -1;
}
cout << absDifference << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment