HW2 - { j=0; i--; } else { i--; j++; } if(square[i][j] !=...

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); void again(); v int main() { start: char yesno; int **square; int r=0, n, i, j, count; while(cout<<"\nEnter 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; } { i++; } { i=n-1; j++; }
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: { 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; again: cout << "\nWould you like to run again? (Y/N): "; cin >> yesno; if(yesno == 'Y' || yesno == 'y') goto start; else if(yesno == 'N' || yesno == 'n') goto quit; else goto again; quit: cout << endl; } void again() { } void output(int **square, int n) { int i; int j; for(i=0; i<n; i++) { cout << endl; for(j=0; j<n; j++) { cout << " " << setw(3) << square[i][j]; } cout << 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 - { j=0; i--; } else { i--; j++; } if(square[i][j] !=...

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