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

Info icon This 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;
Image of page 1

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

View Full Document Right Arrow Icon
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern