Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <sstream>
  5. #include <climits>
  6.  
  7. using namespace std;
  8.  
  9. int testUnique(vector<int>);
  10. void bubbleSort(vector<int>&);
  11. int checkMagic(int, int);
  12.  
  13. int main()
  14. {
  15.     int i, j, length, tmp, n;
  16.     char buff[1000];
  17.     stringstream s (ios_base::in);
  18.  
  19.     cout << "N?" << endl;
  20.  
  21. // Gets the side length of the square from the user
  22. // and makes sure only one whole number greater than
  23. // zero was entered
  24.    
  25.     cin.get(buff,1000);
  26.  
  27.     s.clear();
  28.     s.str(buff);
  29.  
  30.     length = 0;
  31.         while(!s.eof() ) {
  32.             s >> n;    
  33.             ++length;
  34.        
  35.             if (s.fail()) {
  36.                 s.clear();
  37.                 s.ignore();
  38.                 continue;
  39.             }          
  40.         }
  41.    
  42.     if (length != 1 || n%1 != 0 || n<1) {
  43.         cerr << "Non-valid dimension." << endl;
  44.         exit(EXIT_FAILURE);
  45.     }
  46.  
  47. // Reads in the numbers of the matrix row-wise
  48.  
  49.     cout << "Enter your numbers:" << endl;
  50.    
  51.     cin.clear();   
  52.     cin.ignore();
  53.  
  54.     cin.get(buff, 1000);
  55.  
  56.     s.clear();
  57.     s.str(buff);
  58.  
  59.     vector<int> nums(n*n); 
  60.    
  61.     i = 0;
  62.     while(!s.eof() ) {
  63.         s >> nums[i];
  64.         ++i;
  65.    
  66.         if (s.fail()) {
  67.                 s.clear();
  68.                 s.ignore();
  69.                 continue;
  70.             }  
  71.     }
  72.  
  73. // Test for n^2 numbers:
  74.  
  75.     if (i != n*n) {
  76.         cerr << "Not enough elements!" << endl;
  77.         exit(EXIT_FAILURE);
  78.     }
  79.  
  80.     if (testUnique(nums) != 0) {
  81.         cerr << "All elements not unique!" << endl;
  82.         exit(EXIT_FAILURE);
  83.     }
  84.  
  85.     bubbleSort(nums);
  86.  
  87. //  To check all values between 1 and n
  88.  
  89.     while (true) {
  90.    
  91.         if (nums[0] != 1 || nums[n*n-1] != n*n) {
  92.             cerr << "Not all numbers 1 to n!" << endl;
  93.             exit(EXIT_FAILURE);
  94.         }
  95.  
  96.         for (i= (n*n)-1; i>0; i--) {
  97.             if (nums[i] - nums[i-1] != 1) {
  98.                 cerr << "Not all numbers 1 to n!" << endl;
  99.                 exit(EXIT_FAILURE);
  100.             }
  101.         }
  102.         break;
  103.     }
  104.  
  105. // Convert to matrix:
  106.  
  107.     int magic[n][n];
  108.     int k = 0;
  109.  
  110.     for (i=0; i<n; i++) {
  111.         for (j=0; j<n; j++) {
  112.            
  113.             magic[i][j] = nums[k];
  114.             k++;
  115.         }
  116.     }
  117.  
  118.     cout << "You entered the " << n << " by " << n << " matrix: " << endl << endl;
  119.  
  120.         for (i=0; i<n; i++) {
  121.             for (j=0; j<n; j++) {
  122.                
  123.                 cout << magic[i][j] << " ";
  124.             }
  125.             cout << endl;
  126.         }
  127.  
  128.  
  129.     cout << endl;
  130.  
  131.     cout << checkMagic(magic, n);
  132.  
  133.     return 0;
  134. }
  135.  
  136. int testUnique(vector<int> v)
  137. {
  138.     int i, j, n;
  139.     n = v.size();
  140.  
  141.     for (i=0; i<n-1; i++) {
  142.         for (j=i+1; j<n; j++) {
  143.            
  144.             if (v[i] == v[j])
  145.             return -1;     
  146.         }
  147.     }
  148.     return 0;
  149. }
  150.  
  151. void bubbleSort(vector<int> &v)
  152. {
  153.     int i, j, n, tmp;
  154.     n = v.size();
  155.  
  156.     for (i=0; i<n; i++) {
  157.         for (j=0; j<i; j++) {
  158.  
  159.             if (v[i] < v[j]) {
  160.                 tmp = v[i];
  161.                 v[i] = v[j];
  162.                 v[j] = tmp;
  163.             }
  164.         }
  165.     }
  166. }
  167.  
  168. int checkMagic(int magic[][n], int a)
  169. {
  170.     int i, j, sum, test;
  171.     sum = test = 0;
  172.  
  173. // Get initial value to check all sums against
  174.     for (i=0; i<a; i++)
  175.         test += magic[i][1];
  176.  
  177. // Test rows against "test"
  178.     for (i=1; i<a; i++) {
  179.         for (j=0; j<a; j++)
  180.             sum += magic[i][j];
  181.        
  182.             if (sum != test)
  183.                 return 1;
  184.         }
  185.  
  186.         sum = 0;
  187.     }
  188.  
  189. // Test columns against "test"
  190.     for (i=0; i<a; i++) {
  191.         for (j=0; j<a; j++)
  192.             sum += magic[j][i];
  193.        
  194.             if (sum != test)
  195.                 return 1;
  196.         }
  197.  
  198.         sum = 0;
  199.     }
  200.  
  201. // Test diagonals
  202.     for (i=0; i<a; i++) {
  203.         for(j=i; j<a; j++) {
  204.             sum += magic[j][i];
  205.        
  206.             if (sum != test)
  207.                 return 1;
  208.         }
  209.  
  210.         sum = 0;
  211.     }
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement