Created
October 2, 2019 16:43
-
-
Save MontelAle/7b340562d3ee044bd8fa2c50fa3b7798 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In onore di un astuccio ormai defunto ✝✝ | |
#include <iostream> | |
#include <vector> | |
#include <random> | |
#include <ctime> | |
using namespace std; | |
int main() { | |
// Set random seed because cpp is awful | |
srand(time(NULL)); | |
// range is the range in which random integers will be picked, dimension is the length of every array | |
int range, dimension; | |
cout << "Immettere l'intervallo degli integer(+-A):"; | |
cin >> range; | |
cout << "Immettere la lunghezza di ogni array(r):"; | |
cin >> dimension; | |
vector< vector <int> > vertical; | |
for (int i = 0; i < dimension; i++) { | |
vector <int> horizontal; | |
// Load horizontal vector | |
for (int c = 0; c < dimension; c++) { | |
horizontal.push_back(-range + (rand() % static_cast<int>(range + range + 1))); | |
} | |
vertical.push_back(horizontal); | |
} | |
// Print matrix | |
for (int i = 0; i < dimension; i++) { | |
for (int c = 0; c < dimension; c++) { | |
cout << vertical[i][c] << " "; | |
} | |
cout << endl; | |
} | |
// Calculate first sum | |
int firstSum = 0, secondSum = 0; | |
for (int i = 0; i < dimension; i++){ | |
firstSum += vertical[i][i]; | |
} | |
for (int i = dimension - 1; i >= 0; i--) { | |
secondSum += vertical[i][dimension - 1 - i]; | |
} | |
cout << "Somma della prima diagonale:" << firstSum << endl; | |
cout << "Somma della seconda diagonale:" << secondSum << endl; | |
if (firstSum == secondSum) { | |
cout << "La somma delle due diagonali è uguale"; | |
} else { | |
cout << "La somma delle due diagonali non è uguale"; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment