Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <vector>
- #include <sstream>
- #include <climits>
- using namespace std;
- int testUnique(vector<int>);
- void bubbleSort(vector<int>&);
- int checkMagic(int, int);
- int main()
- {
- int i, j, length, tmp, n;
- char buff[1000];
- stringstream s (ios_base::in);
- cout << "N?" << endl;
- // Gets the side length of the square from the user
- // and makes sure only one whole number greater than
- // zero was entered
- cin.get(buff,1000);
- s.clear();
- s.str(buff);
- length = 0;
- while(!s.eof() ) {
- s >> n;
- ++length;
- if (s.fail()) {
- s.clear();
- s.ignore();
- continue;
- }
- }
- if (length != 1 || n%1 != 0 || n<1) {
- cerr << "Non-valid dimension." << endl;
- exit(EXIT_FAILURE);
- }
- // Reads in the numbers of the matrix row-wise
- cout << "Enter your numbers:" << endl;
- cin.clear();
- cin.ignore();
- cin.get(buff, 1000);
- s.clear();
- s.str(buff);
- vector<int> nums(n*n);
- i = 0;
- while(!s.eof() ) {
- s >> nums[i];
- ++i;
- if (s.fail()) {
- s.clear();
- s.ignore();
- continue;
- }
- }
- // Test for n^2 numbers:
- if (i != n*n) {
- cerr << "Not enough elements!" << endl;
- exit(EXIT_FAILURE);
- }
- if (testUnique(nums) != 0) {
- cerr << "All elements not unique!" << endl;
- exit(EXIT_FAILURE);
- }
- bubbleSort(nums);
- // To check all values between 1 and n
- while (true) {
- if (nums[0] != 1 || nums[n*n-1] != n*n) {
- cerr << "Not all numbers 1 to n!" << endl;
- exit(EXIT_FAILURE);
- }
- for (i= (n*n)-1; i>0; i--) {
- if (nums[i] - nums[i-1] != 1) {
- cerr << "Not all numbers 1 to n!" << endl;
- exit(EXIT_FAILURE);
- }
- }
- break;
- }
- // Convert to matrix:
- int magic[n][n];
- int k = 0;
- for (i=0; i<n; i++) {
- for (j=0; j<n; j++) {
- magic[i][j] = nums[k];
- k++;
- }
- }
- cout << "You entered the " << n << " by " << n << " matrix: " << endl << endl;
- for (i=0; i<n; i++) {
- for (j=0; j<n; j++) {
- cout << magic[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl;
- cout << checkMagic(magic, n);
- return 0;
- }
- int testUnique(vector<int> v)
- {
- int i, j, n;
- n = v.size();
- for (i=0; i<n-1; i++) {
- for (j=i+1; j<n; j++) {
- if (v[i] == v[j])
- return -1;
- }
- }
- return 0;
- }
- void bubbleSort(vector<int> &v)
- {
- int i, j, n, tmp;
- n = v.size();
- for (i=0; i<n; i++) {
- for (j=0; j<i; j++) {
- if (v[i] < v[j]) {
- tmp = v[i];
- v[i] = v[j];
- v[j] = tmp;
- }
- }
- }
- }
- int checkMagic(int magic[][n], int a)
- {
- int i, j, sum, test;
- sum = test = 0;
- // Get initial value to check all sums against
- for (i=0; i<a; i++)
- test += magic[i][1];
- // Test rows against "test"
- for (i=1; i<a; i++) {
- for (j=0; j<a; j++)
- sum += magic[i][j];
- if (sum != test)
- return 1;
- }
- sum = 0;
- }
- // Test columns against "test"
- for (i=0; i<a; i++) {
- for (j=0; j<a; j++)
- sum += magic[j][i];
- if (sum != test)
- return 1;
- }
- sum = 0;
- }
- // Test diagonals
- for (i=0; i<a; i++) {
- for(j=i; j<a; j++) {
- sum += magic[j][i];
- if (sum != test)
- return 1;
- }
- sum = 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement