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

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

#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;

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

View Full Document
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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern