Created
October 11, 2018 10:06
-
-
Save ske2004/5a1db30fa361f3282bc54d636afb15c0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Example program | |
#include <iostream> | |
#include <cmath> | |
#include <ctime> | |
#define SIZE 3 | |
int parker[SIZE][SIZE]; | |
int sumsY[SIZE]; | |
int sumsX[SIZE]; | |
int sumsDiag[2]; | |
void fillSquare() | |
{ | |
for (int i = 0; i < SIZE; i++) | |
for (int j = 0; j < SIZE; j++) | |
{ | |
parker[i][j] = rand()%64; | |
} | |
} | |
void squareAll() | |
{ | |
for (int i = 0; i < SIZE; i++) | |
for (int j = 0; j < SIZE; j++) | |
{ | |
parker[i][j] = pow(parker[i][j], 2); | |
} | |
} | |
void printSquare() | |
{ | |
for (int i = 0; i < SIZE; i++) | |
{ | |
for (int j = 0; j < SIZE; j++) | |
{ | |
std::cout << parker[i][j] << " "; | |
} | |
std::cout << std::endl << std::endl; | |
} | |
} | |
void sumParker() | |
{ | |
for (int i = 0; i < SIZE; i++) | |
{ | |
for (int j = 0; j < SIZE; j++) | |
{ | |
sumsY[i] += parker[i][j]; | |
sumsX[j] += parker[i][j]; | |
} | |
} | |
for (int i = 0; i < SIZE; i++) | |
{ | |
sumsDiag[0] += parker[i][i]; | |
sumsDiag[1] += parker[i][(SIZE-1)-i]; | |
} | |
} | |
void printSums() | |
{ | |
for (int i = 0; i < SIZE; i++) | |
std::cout << sumsY[i] << std::endl; | |
std::cout << std::endl; | |
for (int i = 0; i < SIZE; i++) | |
std::cout << sumsX[i] << std::endl; | |
std::cout << std::endl; | |
std::cout << sumsDiag[0] <<std::endl << std::endl; | |
std::cout << sumsDiag[1] <<std::endl; | |
} | |
bool checkSums() | |
{ | |
int lastNumber = 0; | |
for (int i = 0; i < SIZE; i++) | |
{ | |
if (lastNumber == sumsY[i]) | |
lastNumber = sumsY[i]; | |
else | |
return false; | |
} | |
for (int i = 0; i < SIZE; i++) | |
{ | |
if (lastNumber == sumsX[i]) | |
lastNumber = sumsX[i]; | |
else | |
return false; | |
} | |
if (lastNumber == sumsDiag[0]) | |
lastNumber = sumsDiag[0]; | |
else | |
return false; | |
if (lastNumber == sumsDiag[1]) | |
lastNumber = sumsDiag[1]; | |
else | |
return false; | |
return true; | |
} | |
int main() | |
{ | |
srand(time(0)); | |
fillSquare(); | |
squareAll(); | |
sumParker(); | |
while(!checkSums()) | |
{ | |
fillSquare(); | |
squareAll(); | |
sumParker(); | |
} | |
printSquare(); | |
printSums(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment