| | |
Print Magic square
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2006
Posts: 4
Reputation:
Solved Threads: 0
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 functionany one can help?
C Syntax (Toggle Plain Text)
#include<iostream> #include<iomanip> using namespace std; const int n = 30; int MagicSquareConstruct(int MagicSquare[n][n]); int main() { int entrySize; int MagicSquare[n][n]; cout<< "Please enter an integer n: "; cin>>entrySize; cout<< MagicSquareConstruct(MagicSquare) << endl; } int MagicSquareConstruct(int MagicSquare[n][n]) { int newRow, newCol; // Set the indices for the middle of the bottom i int i =0 ; int j= n / 2; // Fill each element of the array using the magic array for ( int value = 1; value <= n*n; value++ ) { MagicSquare[i][j] = value; // Find the next cell, wrapping around if necessary. newRow = (i + 1) % n; newCol = (j + 1) % n; // If the cell is empty, remember those indices for the // next assignment. if ( MagicSquare[newRow][newCol] == 0 ) { i = newRow; j = newCol; } else { // The cell was full. Use the cell above the previous one. i = (i - 1 + n) % n; } } }
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,
C Syntax (Toggle Plain Text)
for(int x(0); x!=total_rows; ++x) { for(int y(0); x!=total_columns; ++y) std::cout << arrays[x][y]; std::cout << endl; }
•
•
Join Date: Apr 2006
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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,C Syntax (Toggle Plain Text)
for(int x(0); x!=total_rows; ++x) { for(int y(0); x!=total_columns; ++y) std::cout << arrays[x][y]; std::cout << endl; }
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
![]() |
Similar Threads
- Magic Square (Java)
- can anyone help me with writing a magic square program? (C)
- Vector of vectors & magic square issues (C++)
- Reversing Integer, Magic Squares, and LCM problems (Java)
Other Threads in the C Forum
- Previous Thread: Pretty Printer for Assemby Language (written in C)
- Next Thread: Help for Air Lines Reservition System
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi





