Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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;
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
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]; }
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.
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
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.
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.
int gg = size-1;
for ( int g = 0; g < size; g++){
seconddiagonal= box[gg][g] + seconddiagonal;
gg = gg - 1;
}
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
else
found=false;
}
return found;
}
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019
SCREENSHOTS / TEST
// 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);
its job is to get the value that is needed in order to identify if the variable "box" is a magic square or not.
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){
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
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"
print ( box,size);
}
else
cout << "INVALID INPUT \n";
}
- END OF FUNCTION MAIN -
CMSC 131: LABORATORY EXERCISE 3 : ACTIVTY A , B & C 09/19/2019
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 (box [*y][*x]==0)
else {
*y = *y + 2;
*x = *x - 1;
return box [*y][*x]= *d;
}}
if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = *y + 2;
*x = *x + 2;
return box [*y][*x]= *d;
}}
if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = *y + 2;
*x = 0;
return box [*y][*x]= *d;
}}}
*y = *y - 1;
*x = *x + 1;
if (box [*y][*x]==0)
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;
}}
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;
}}
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;
}}
if (box [*y][*x]==0)
return box [*y][*x]= *d;
else {
*y = *y - 1;
*x = 0;
return box [*y][*x]= *d;
}}}}
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.
int gg = size-1;
for ( int g = 0; g < size; g++){
seconddiagonal= box[gg][g] + seconddiagonal;
gg = gg - 1;
}
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;
}
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++){
SCREENSHOTS / TEST