Print Magic square

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 4
Reputation: maxilt is an unknown quantity at this point 
Solved Threads: 0
maxilt maxilt is offline Offline
Newbie Poster

Print Magic square

 
0
  #1
Apr 29th, 2006
I work on the program to function a Magic square, below as my working program, but I don't know how to print out the magic square using print function
any one can help?

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. const int n = 30;
  5. int MagicSquareConstruct(int MagicSquare[n][n]);
  6. int main()
  7. {
  8. int entrySize;
  9. int MagicSquare[n][n];
  10. cout<< "Please enter an integer n: ";
  11. cin>>entrySize;
  12. cout<< MagicSquareConstruct(MagicSquare) << endl;
  13. }
  14.  
  15. int MagicSquareConstruct(int MagicSquare[n][n])
  16. {
  17. int newRow,
  18. newCol;
  19.  
  20. // Set the indices for the middle of the bottom i
  21. int i =0 ;
  22. int j= n / 2;
  23.  
  24. // Fill each element of the array using the magic array
  25. for ( int value = 1; value <= n*n; value++ )
  26. {
  27. MagicSquare[i][j] = value;
  28. // Find the next cell, wrapping around if necessary.
  29. newRow = (i + 1) % n;
  30. newCol = (j + 1) % n;
  31. // If the cell is empty, remember those indices for the
  32. // next assignment.
  33. if ( MagicSquare[newRow][newCol] == 0 )
  34. {
  35. i = newRow;
  36. j = newCol;
  37. }
  38. else
  39. {
  40. // The cell was full. Use the cell above the previous one.
  41. i = (i - 1 + n) % n;
  42. }
  43. }
  44. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Print Magic square

 
0
  #2
Apr 29th, 2006
by "Print function" I guess you mean the method to output to the screen or console window? the typical way to output the contents of a 2D array involve a nested loop, eg,
  1. for(int x(0); x!=total_rows; ++x)
  2. {
  3. for(int y(0); x!=total_columns; ++y)
  4. std::cout << arrays[x][y];
  5. std::cout << endl;
  6. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 4
Reputation: maxilt is an unknown quantity at this point 
Solved Threads: 0
maxilt maxilt is offline Offline
Newbie Poster

Re: Print Magic square

 
0
  #3
Apr 30th, 2006
Originally Posted by Bench
by "Print function" I guess you mean the method to output to the screen or console window? the typical way to output the contents of a 2D array involve a nested loop, eg,
  1. for(int x(0); x!=total_rows; ++x)
  2. {
  3. for(int y(0); x!=total_columns; ++y)
  4. std::cout << arrays[x][y];
  5. std::cout << endl;
  6. }

can five me more detail how to insert the print function into my coding? I try to put in but the loop become the inifitive loop
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Print Magic square

 
0
  #4
Apr 30th, 2006
What print function are you referring to specifically? do you mean the C function printf()? You shouldn't be using printf with C++ - that's what cout is for.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC