#include <iostream>
using namespace std;
void fillMagicSquare(int newarray[][n], int size);
int main()
{
int n = 1;
cout << "Please enter the size of the matrix, it must be an odd number: " << endl;
while (true)
{
cin >> n;
int magic_square[n][n];
if (n%2==0)
{
cout << "You must choose an odd number! Choose Again: " << endl;
}
else
{
fillmagicsquare(arra, n);
}
}
}
void fillMagicSquare(int newarray[][n], int size)
{
size = n2;
int nsqr = n2 * n2;
int i=0, j=n2/2;
}
#include <iostream>
using namespace std;
int getDim();
int n2;
int main()
{
int i;
int j;
int n = getDim(); // trying to get a value to 'n' that is the user provided odd number from the getDim function, but it always comes back 0.
cout << "You chose a "<< n << "x" << n << " matrix" << endl; //this line is to test if the value for 'n' came back correctly.
/* below is where I declared the matrix using the coordinates 'n' that the user provided.
int matrix[n][n]; I believe I get a compiler error from this line. Does an array have to be filled with a constant?
!! THE CODE COMPILES WITHOUT ERROR IF I REMOVE THE BELOW LOOP. !!
The reason being because I have not declared my matrix array, I have not declared it because I do not
know how to create an array with dynamic dimensions yet. Something my teacher decided to not teach but expect us to know
*/
for (i = 0; i<n; i++)
for(j = 0; j<25; j++)
matrix [i][j] = 0;
}
int getDim()
{
int n = 0;
cout << "Please enter the size of the matrix, it must be an odd number: " << endl;
while (true)
{
cin >> n;
if (n%2==0)
{
cout << "You must choose an odd number! Choose Again: " << endl;
}
else
{
n = n2;
return (n2);
}
}
}
/* n is the length of each side (the dimensions) and r and c are just placeholders for the for loops.
*/
int r;
int c;
for (r = 0; r <= n; r++)
for (c = 0; c <= n; c++)
cout << matrix[r][c] << "/t" ;
#include <iostream>
using namespace std;
int getDim();
int n2;
int main()
{
int zerorow;
int zerocol;
int n = getDim();
cout << "You chose a "<< n << "x" << n << " matrix" << endl;
int matrix[45][45];
for (zerorow = 0; zerorow < n; zerorow++)
for(zerocol = 0; zerocol < n; zerocol++)
matrix [zerorow][zerocol] = 0;
int row = 1;
int col = (n+1)/2;
int row2;
int col2;
int k = 1;
int nsq = n*n;
matrix[row-1][col-1] = k;
for(k=2 ; k <= nsq ; k++)
{
row2 = row - 1;
col2 = col + 1;
if (row2 == 0 && col2 == (n+1))
{
row2 = row + 1;
col2 = n;
row = row2;
col = col2;
matrix[row-1][col-1]=k;
}
else
{
if(row2 == 0)
{
row2= n;
}
if(col2 == (n+1))
{
col2 = 1;
}
if(matrix[row2-1][col2-1] == 0)
{
row = row2;
col = col2;
matrix[row-1][col-1] = k;
}
else
{
row2 = row + 1;
row2 = col;
if(row2 == (n+1))
{
row2 = 1;
}
row = row2;
col = col2;
matrix[row-1][col-1] = k;
}
}
}
return 0;
}
int getDim()
{
int n = 0;
cout << "Please enter the size of the matrix, it must be an odd number: " << endl;
while (true)
{
cin >> n;
if (n%2==0)
{
cout << "You must choose an odd number! Choose Again: " << endl;
}
else
{
n2 = n;
return (n2);
}
}
}
Title | # Comments | Views | Activity |
---|---|---|---|
C++ finding a sting in a char* string from a text file | 3 | 92 | |
How to creat good software interface ? | 1 | 57 | |
Insert several multiple frames into a single mainframe | 3 | 46 | |
Using popen() and gunzip() to open file in HTTPServer | 6 | 12 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
12 Experts available now in Live!