HW2 - else if(i == 0&& j == n-1 i else if(i ==...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<iostream> #include<iomanip> #include<limits> using namespace std; void output(int **square, int n); void zeroit(int **square, int n); v int main() { int **square; int r=0, n, i, j, count; while(cout<<"Enter an odd number of rows/columns for the magic square: " && (!(std::cin>>n) || !(n%2))) { std::cin.clear(); std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); cout<<endl<<"INVALID INPUT! PLEASE TRY AGAIN!\n"<<endl; } square = new(nothrow) int*[n]; for(i=0; i<=n; i++) { square[i] = new int[n]; } zeroit(square,n); for(count=1;count<=(n*n);count++) { if(count==1) { i=0; j=n/2;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } else if(i == 0 && j == n-1) { i++; } else if(i == 0 && j != n-1) { i=n-1; j++; } else if(j == n-1 && i != 0) { j=0; i--; } else { i--; j++; } if(square[i][j] != 0) { if(i==n-1) i=0; i=i+2; if(j==0) j=n-1; j--; } square[i][j] = count; } output(square,n); delete *square; } void output(int **square, int n) { int i; int j; for(i=0; i<n; i++) { for(j=0; j<n; j++) { cout << " " << setw(3) << square[i][j]; } cout << endl << endl; } } void zeroit(int **square, int n) { int i; int j; for(i=0; i<n; i++) { for(j=0; j<n; j++) { square[i][j]=0; } } }...
View Full Document

This note was uploaded on 02/16/2010 for the course COMPE 260 taught by Professor Lovegren during the Spring '10 term at San Diego State.

Page1 / 2

HW2 - else if(i == 0&& j == n-1 i else if(i ==...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online