Google Answers Logo
View Question
 
Q: C++ Program Modification ( No Answer,   2 Comments )
Question  
Subject: C++ Program Modification
Category: Computers > Programming
Asked by: forsight-ga
List Price: $10.00
Posted: 30 Jul 2004 19:51 PDT
Expires: 29 Aug 2004 19:51 PDT
Question ID: 381568
Revise the code below to overload the stream extraction operator
(i.e., >>) and the stream insertion operator (i.e., <<).
In the case of the stream extraction operator, output should be to the
screen and to a file.

#include <iostream>
#include <iomanip>

using namespace std;

int ReadSquareSize();
void OddMagicSquare(int** matrix, int n);
void DoublyEvenMagicSquare(int** matrix, int n);
void SinglyEvenMagicSquare(int** matrix, int n);
void MagicSquare(int** matrix, int n);
int** CreateMatrix(int n);
void FreeMatrix(int** matrix, int n);

int main(int argc, char* argv[])
{
  int i,n;
  n = ReadSquareSize();

  int** matrix = CreateMatrix(n);

  MagicSquare(matrix, n);  

  // print the square
  for(int i=0; i<n ;i++)
  {
      for(int j=0; j<n ; j++)
      {
      cout<<setiosflags(ios::left) << setw (5) << matrix[i][j];        
      }
      cout<<endl;
  }
  
  FreeMatrix(matrix,n);
    
  return 0;
}


int ReadSquareSize()
{
    int x; 
    cout<<"Enter a positive integer square size of 3 or more: ";
    while(1)
    {
        cin>>x;
        if(x>=3)
        {
            return x;
        }
        else 
        {
            cout<<"Enter a positive integer square size of 3 or more: ";
        }
    }
    cout<<endl;
}

void MagicSquare(int** matrix,int n)
{
  if (n%2==1)        //n is Odd
    OddMagicSquare(matrix, n);
  else          //n is even
    if (n%4==0)    //doubly even order
      DoublyEvenMagicSquare(matrix, n);
    else      //singly even order
      SinglyEvenMagicSquare(matrix, n);
}

void OddMagicSquare(int** matrix, int n)
{
  int nsqr = n * n;
  int i=0, j=n/2;     // start position

  for (int k=1; k<=nsqr; ++k) 
  {
    matrix[i][j] = k;

    i--;
    j++;

    if (k%n == 0) 
    { 
      i += 2; 
      --j; 
    }
    else 
    {
      if (j==n) 
        j -= n;
      else if (i<0) 
        i += n;
    }
  }
}

void DoublyEvenMagicSquare(int** matrix, int n)
{
  int i, j;

  int** I = CreateMatrix(n);
  int** J = CreateMatrix(n);


  //prepare I, J
  int index=1;
  for (i=0; i<n; i++)
    for (j=0; j<n; j++)
    {
      I[i][j]=((i+1)%4)/2;
      J[j][i]=((i+1)%4)/2;
      matrix[i][j]=index;
      index++;
    }

  for (i=0; i<n; i++)
    for (j=0; j<n; j++)
    {
      if (I[i][j]==J[i][j])
        matrix[i][j]=n*n+1-matrix[i][j];
    }

  FreeMatrix(I,n);
  FreeMatrix(J,n);
    
}

void SinglyEvenMagicSquare(int** matrix, int n)
{
  int i, j, k, index=0;

  int p=n/2;

  int** M = CreateMatrix(p);
  
  MagicSquare(M, p);
  
  for (i=0; i<p; i++)
    for (j=0; j<p; j++)
    {
      matrix[i][j]=M[i][j];
      matrix[i+p][j]=M[i][j]+3*p*p;
      matrix[i][j+p]=M[i][j]+2*p*p;
      matrix[i+p][j+p]=M[i][j]+p*p;
    }

  if (n==2)
    return;  

  int* I = new int[p];
  int* J = new int[n];

  for (i=0; i<p; i++)
    I[i]=i+1;

  k=(n-2)/4;
  
  for (i=1; i<=k; i++)
    J[index++] = i;

  for (i=n-k+2; i<=n; i++)
    J[index++] = i;

  int temp;
  for (i=1; i<=p; i++)
    for (j=1; j<=index; j++)
    {
      temp=matrix[i-1][J[j-1]-1];
      matrix[i-1][J[j-1]-1]=matrix[i+p-1][J[j-1]-1];
      matrix[i+p-1][J[j-1]-1]=temp;
    }

  //j=1, i
  //i=k+1, k+1+p
  i=k; 
  j=0;
  temp=matrix[i][j]; matrix[i][j]=matrix[i+p][j]; matrix[i+p][j]=temp;

  j=i;
  temp=matrix[i+p][j]; matrix[i+p][j]=matrix[i][j]; matrix[i][j]=temp;

  FreeMatrix(M,p);
  delete[] I;
  delete[] J;
  
}


int** CreateMatrix(int n)
{
  int i;
  int** matrix = new int* [n];
  for(i=0;i<n;i++)
  {
        matrix[i] = new int[n];
  }
  return matrix;
}


void FreeMatrix(int** matrix, int n)
{
  for(int i=0; i<n ;i++)
  {
      delete[] matrix[i];
  }
  free(matrix);
}

Request for Question Clarification by studboy-ga on 31 Jul 2004 09:22 PDT
Hi forsight-ga

You mean overloading it to output to both file and stream?
(What's the file name?)
Or overloading it for in/outputing something else?

Also currently it does not compile due to an implicit
declaation of free--what's your compiler?

Thanks!

Clarification of Question by forsight-ga on 01 Aug 2004 00:16 PDT
I an such a newbie at this it's not funny but, I am using MS Visual
Studio .Net 2003. I haven't done any upgrades, so whatever compiler
comes out of the box is the compiler I am using.

Yes, I am asking to output to file and stream.  File Magic Square
under root directory C:\
Answer  
There is no answer at this time.

Comments  
Subject: Re: C++ Program Modification
From: kamalg-ga on 26 Aug 2004 03:22 PDT
 
Here is a simple solutions.  

Move the printing logic to a separate function say print.
void print(ostream& opStream)
{ 
 // print the square
  for(int i=0; i<n ;i++)
  {
      for(int j=0; j<n ; j++)
      {
      opStream<<setiosflags(ios::left) << setw (5) << matrix[i][j];        
      }
      opStream<<endl;
  }
}

then from your main method call print twice, one to log data to
console and one to log data to file.

// This will print data to console
print(cout);

ofstream outFile("c:/File Magic Square");
print(outFile);
Subject: Re: C++ Program Modification
From: kamalg-ga on 26 Aug 2004 03:24 PDT
 
you would need to pass n and the matrix as argument to the print method.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy