Sei sulla pagina 1di 14

CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

Saturnina Fabian Nisperos,Ph.D.

 Paola Francisco
 Blein Alcon
 Rheydel Bartolome
( BSCS 2 – A )
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

THIS IS A FUNCTION wherein its job is to print the values of the array.
Note: it is void because no value is going to be returned;

void printTable (int **box,int size);

int main (){


int size;

This part of the codes’ job is to set a new size that will be inputed by the user for the pointer array

cout << "Enter the elements for row & column: ";
cin >> size;

This part of the codes’ job is to set a new size that will be inputed by the user for the pointer array

int** box = new int*[size];


for(int i = 0; i < size; ++i)
box[i] = new int[size];

Its job is to input values on the pointer array and in order to continuously.
//input, we used a simple for-loop in order to achieve that.
cout << "Enter Digits: \n ";
for ( int i = 0; i < size; i++)
for ( int ii = 0; ii < size; ii++)
{ cin >>box[i][ii]; }

Calling the function print in order to print the called variables.

printTable ( box, size);


}
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

Now this is the Function printTable where you will see its structure and
what it does.
void printTable (int **box,int size){

Its job is to display the recorded values of the array box by again using
for-loop and in order to form that box a condition is inserted below
which if z / the size is equal to zero then a space will be designated
after.

cout << " \n \tMATRIX TABLE:\t \n ";


int x,y,z;
for ( x = 0; x < size; x++)
for ( y = 0; y < size; y++){

cout << box [x][y] <<"\t";


z = z + 1;
if(z % size == 0)
cout << " \n";
}
}

SCREENSHOTS / TEST

TEST 1-3:
The test was a success because obviously, the
values that was inserted was correctly displayed
on the right parts.
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

1st Function: its job is to print the values of the "box" array
2nd Function: its job is to check if the values in the "box" array is a magic square

void print (int **box, int size);


bool magicsquarechecker (int **box, int size,int value);

int main (){


int size;

cout << "Enter the elements: ";


cin >> size;

its job is to get the value that is needed in order to identify


if the variable "box" is a magic square or not.

int value = size * ( size * size + 1 ) / 2;

its job is to set a new size that will be input by the user for the pointer array
int** box = new int*[size];
for(int i = 0; i < size; ++i)
box[i] = new int[size];

its job is to input values on the pointer array and in order to contiously
//input, we used a simple for-loop in order to achieve that.

cout << "Enter Digits:\n ";


for ( int i = 0; i < size; i++)
for ( int ii = 0; ii < size; ii++)
{
cin >>box[i][ii];
}

//Calling the function print in order to print the called variables.

print ( box, size);


CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

//Again, calling a function but this time it is the function magicsquarechecker; it is


inside a condition in order to see if the returned value of the bool is true or not.
the purpose or process of the function will be explained below. If true, then it will
display you formed it, but if false then no.

if ( magicsquarechecker (box, size,value)==true){


cout <<"\n MAGIC SQUARE VALUE:"<<value <<"\n";
cout << "\n Congratulations, you formed a Magic Square! \n ";
}
else
cout << "\n You did not formed a Magic Square,BETTER LUCK NEXT TIME! \n ";

void print (int **box,int size){

cout << " \n MATRIX TABLE: \t \n ";

for ( int i = 0; i < size; i++)


for ( int ii = 0; ii < size; ii++){

cout <<box [i][ii]<< "\t";


int space = space + 1;
if(space % size == 0)
cout << " \n";}
}

bool magicsquarechecker (int **box,int size,int value){


int seconddiagonal=0, diagonal=0, row[size]={0}, column[size]={0};
bool found;

This part is a compiled for-loops in the different variables. We used for-loop for because
there are more than 1 part of it that is why it is needed to check every part of it. And as
you can see the variable row and column is an array because the amount of rows and columns are
not the same in every different number of tables (eg. 3x3 & 7x7 are not the same). While the
amount of diagonals are the same no matter how large or small the table is.

for ( int h = 0; h < size; h++){


diagonal= box[h][h] + diagonal;
}

int gg = size-1;
for ( int g = 0; g < size; g++){
seconddiagonal= box[gg][g] + seconddiagonal;
gg = gg - 1;
}

for ( int z = 0; z < size; z++)


CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

for ( int y = 0; y < size; y++)


{
row[z]= box [z][y] + row[z];
column[z]=box [y][z] + column[z];
}

now this part is where the function will check if every rows,columns and the two diagonals
are equal to the calculated value. If it is then the bool found will be true but if not then
it will fall to false. After the loop the program will finally return the final value of the
bool and it will go the the main function

for ( int x = 0; x < size; x++)


{
if ( row[x] == value && column[x] == value && diagonal == value && seconddiagonal
== value )
found=true;

else
found=false;
}
return found;
}
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

SCREENSHOTS / TEST

TEST 1&3: checking if the values


in the box is a magic was a
success therefore test 1 confirms
the working function of the magic
square checker.
TEST 2: another success
encounter in which the values
that was inputed are not magic
square and again, the checker did
confirmed that it was not.
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

// 1st Function: its job is to print the values of the "box" array
// 3rd Function: its job is to form a magic square depending on the value of the variable "size"
// 2nd Function: its job is to check if the values in the "box" array is a magic square
void print (int **box,int size);
int magicsquare (int element,int *x,int *y,int **box,int *d);
bool magicsquarechecker (int **box,int size,int value);

int main (){


int size , ptr1=0, digits=2 ;

cout << "Enter the element: ";


cin >> size;

its job is to get the value that is needed in order to identify if the variable "box" is a magic square or not.

int value = size * ( size * size + 1 ) / 2;

This if condition specifies that if the size input is odd number then it will continue on but if it is even then it
will display error.
if (size % 2 != 0){

int element= size - 1;


int ptr2 = size / 2;

The variables below are set to pointers in order for it to change even outside main

int *y = &ptr1;
int *x = &ptr2;
int *d = &digits;

its job is to set a new size that will be input by the user for the pointer array
int** box = new int*[size];
for(int i = 0; i < size; ++i)
box[i] = new int[size];
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

its job is to set the values of box to 0

for ( int i = 0; i < size ; i++)


for (int ii=0; ii < size ; ii++)
{ box[i][ii]=0; }

Before going to the magic square we already initiated the first value which is 1 to the designated part of the
array.

box [*y][*x]=1;

its job is to form the magic square by using for-loop we are able to have
the step by step process in an easier way. The variable d is the current amount or value
of the number that will be input in the variable "box"

for ( int i = 1; i< size*size ; i++){


magicsquare (element, x, y, box, d);
*d=*d+1;
}
Again, calling a function but this time it is the function magicsquarechecker; it is inside a condition in order to
see if the returned value of the bool is true or not. the purpose or process of the function will be explained
below. If true, then it will display you formed it, but if false then it will display did not formed magic square.

if ( magicsquarechecker (box, size,value)==true)


cout << "\n Congratulations, you formed a Magic Square! \n ";
else
cout << "\n You did not formed a Magic Square,BETTER LUCK NEXT TIME! \n ";

print ( box,size);
}

else
cout << "INVALID INPUT \n";
}
- END OF FUNCTION MAIN -
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

int magicsquare (int element,int *x,int *y,int **box,int *d){

Summary of its job is it is first checking the position of the previous value which is the x and y, after that it
will follow the indicated step which is right then left. After that it will go to another condition to see if that part
of the box is equal to 0 and if it is then the digit or d will be equal to that part of the box but if not then the
bottom part of the previous position of the box will be equal to the digit or d, so that is basically it.

if ( *y < element && *y != 0 ){


if ( *x < element && *x != 0){
*y = *y - 1;
*x = *x + 1;

if (box [*y][*x]==0)

return box [*y][*x]= *d;

else {
*y = *y + 2;
*x = *x - 1;
return box [*y][*x]= *d;
}}

else if (*x == element){


*y = *y - 1;
*x = 0;

if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = *y + 2;
*x = *x + 2;
return box [*y][*x]= *d;
}}

else if (*x == 0){


*y = *y - 1;
*x = *x + 1;

if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = *y + 2;
*x = 0;
return box [*y][*x]= *d;
}}}

else if (*y == element ){


if ( *x < element && *x != 0){
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

*y = *y - 1;
*x = *x + 1;

if (box [*y][*x]==0)

return box [*y][*x]= *d;

else {
*y = *y - 1;
*x = *x - 1;
return box [*y][*x]= *d;
}}
else if (*x == element){
*y = *y-1;
*x = 0;

if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = *y - 1;
*x = element;
return box [*y][*x]= *d;
}}

else if (*x == 0){


*y = *y - 1;
*x = *x + 1;

if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = *y - 1;
*x = *x - 1;
return box [*y][*x]= *d;
}}}

else if ( *y == 0 ){
if ( *x < element && *x != 0){
*y = element;
*x = *x + 1;

if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = *y - 1;
*x = *x - 1;
return box [*y][*x]= *d;
}}

else if (*x == element){


CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

int x2 = *x;
int y2 = *y;
*y = element;
*x = 0;

if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = y2+1;
*x = x2;
return box [*y][*x]= *d;
}}

else if (*x == 0){


*y = element;
*x = *x + 1;

if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = *y - 1;
*x = 0;
return box [*y][*x]= *d;
}}}}

bool magicsquarechecker (int **box,int size,int value){


int seconddiagonal=0, diagonal=0, row[size]={0}, column[size]={0};
bool found;

This part is a compiled for-loops in the different variables. We used for-loop for because there are more
than 1 part of it that is why it is needed to check every part of it. And as you can see the variable row and
column is an array because the amount of rows and columns are not the same in every different number of
tables (eg. 3x3 & 7x7 are not the same). While the amount of diagonals are the same no matter how large
or small the table is.

for ( int h = 0; h < size; h++){


diagonal= box[h][h] + diagonal;
}

int gg = size-1;
for ( int g = 0; g < size; g++){
seconddiagonal= box[gg][g] + seconddiagonal;
gg = gg - 1;
}

for ( int z = 0; z < size; z++)


for ( int y = 0; y < size; y++)
{
row[z]= box [z][y] + row[z];
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

column[z]= box [y][z] + column[z];


}

Now this part is where the function will check if every rows, columns and the two diagonals are equal to the
calculated value. If it is then the bool found will be true but if not then it will fall to false. After the loop the
program will finally return the final value of the bool and it will go the main function
for ( int x = 0; x < size; x++)
{
if ( row[x] == value && column[x] == value && diagonal == value &&
seconddiagonal == value )
found=true;

else
found=false;
}
return found;
}

void print (int **box,int size){

It job is to display the recorded values of the array box by again using for-loop and in order to form that box
a condition is inserted below which if z / the size is equal to zero then a space will be designated after.
cout << " \n MATRIX TABLE: \t \n ";
int x,y,z;
for ( y = 0; y < size; y++)
for ( x = 0; x < size; x++){

cout <<box [y][x]<< "\t";


z = z + 1;
if(z % size == 0)
cout << " \n";}
}
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019

SCREENSHOTS / TEST

TEST 1: Getting the Magic Square of the


size of 3; it was a success since the
magicsquareChecker confirmed it was a
Magic Square.

TEST 2: Getting the Magic Square of the


size of 4; it was a success since the error
display showed up because size of 4 is
not an odd number.

TEST 3: Getting the Magic Square of the


size of 7; it was a success since 7 is an
odd so it goes into the program, and the
table was checked and it was confirmed

Potrebbero piacerti anche