944,044 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 11895
  • C RSS
Apr 29th, 2006
0

Print Magic square

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maxilt is offline Offline
4 posts
since Apr 2006
Apr 29th, 2006
0

Re: Print Magic square

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. }
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Apr 30th, 2006
0

Re: Print Magic square

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maxilt is offline Offline
4 posts
since Apr 2006
Apr 30th, 2006
0

Re: Print Magic square

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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Pretty Printer for Assemby Language (written in C)
Next Thread in C Forum Timeline: Help for Air Lines Reservition System





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC