Quantcast

Maximum PC

It is currently Sat Aug 23, 2014 5:05 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Magic Square Program
PostPosted: Sun Dec 12, 2004 2:32 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
We are suppose to create a magic square program using C++ for bonus points on a test. I thought I figured it out but it is stumping me right now.

Info on what a magic square is:
http://mathworld.wolfram.com/MagicSquare.html

What I have:
Code:
#include <string>               // String header file
#include <iostream>                // Input Output stream header file
#include <fstream>               // Allows you to read and write from a file
#include <iomanip>

using namespace std;            // Standard included statement



int main ()                         // Main function
{                                   // Opens main function

   int size;
   int goal;
   int magic [100][100];
   int r, c;
   int   num = 1;
   int   taken[250] = {0};
   int col = 0;
   int row = 0;
   int not_magic = 1;
   int loop = 1;
   int old_row, old_col;


   while (size%2 == 0)
   {
      cout << "Please enter an odd number for the size of the magic square you want:  ";
      cin >> size;
   }


   goal = size *  ( size*size + 1) / 2 ;
   
   col = size/2;


   
   

   while(loop!=size*size){


      magic[row][col] = num;
      old_row=row;
      old_col=col;
      num++;
      row --;
      col++;

   

      if(magic[row][col]>0){
         row=old_row;
         col=old_col;
         row++;
      }


      if(row<0)
         row = size -1;
      if(col>size-1)
         col=0;

      loop++;
   }
   

   



   for(r=0;r<size;r++){
                for(c=0;c<size;c++){
         
                        cout<<magic[r][c]<<" ";
                }
         cout<<setw(3)<<endl;
      
   }





   return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 12, 2004 3:07 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Nevermind, I got it. Had the test after the assignment when it should be before heh.
Code:
me = dumbass;


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 12, 2004 3:07 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Col will not start out what you want it to be. If you enter an odd integer and divide it by 2, you will truncate the mantissa in integer division.

You say col = size/2, but if size is always odd, you will always get an inaccurate answer.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 12, 2004 3:32 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
No, I want to start in the top middle always...

3/2 = 1

1 is the middle of the array since it starts at 0
Say I put in 7 and divide by 2 and get 3. That puts me in the middle again.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 12, 2004 6:48 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Code:
///////////////////////////////////////////////////////////////////////////////
//  Joseph Kocurek
//  CMPS 1043 - Computer Science I
//  Program #6
//  Reads from a file and calculates greatests sales and who should get the bonus
///////////////////////////////////////////////////////////////////////////////

#include <string>               // String header file
#include <iostream>                // Input Output stream header file
#include <fstream>               // Allows you to read and write from a file


using namespace std;            // Standard included statement

struct employee{
        string first_name;
        string last_name;
        double salary;
};



int main ()                         // Main function
{                                   // Opens main function

       ofstream outfile;
       ofstream ofile;
       ifstream ifile;
      int x = 0, y;

      employee data[100];

      ifile.open("emp_sales.txt",ios::in);
      outfile.open("output.txt",ios::app);

      while(!ifile.eof()){

         ifile>>data[x].first_name;
         ifile>>data[x].last_name;
         ifile>>data[x].salary;
         
         x++;



      }

      ifile.close();

      



      for(y=0 ; y<=x ; y++){
         cout<<data[x].first_name;
         cout<<data[x].last_name;
         cout<<data[x].salary;
         cout<<endl<<endl;

      }

       

      

      outfile.close();
      system("PAUSE");

      return 0;

}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 12, 2004 6:53 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
The text file contents

Joe Smith 13943.45
Bob Jones 15998.88
Sally Jesse 30100.50
Sue Thomas 18888.34
Bill Makul 28898.33


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group