# Maximum PC

 It is currently Sun Apr 26, 2015 8:26 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 6 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Magic Square ProgramPosted: Sun Dec 12, 2004 2:32 pm
 Clawhammer

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

 Post subject: Posted: Sun Dec 12, 2004 3:07 pm
 Clawhammer

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

 Post subject: Posted: Sun Dec 12, 2004 3:07 pm
 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

 Post subject: Posted: Sun Dec 12, 2004 3:32 pm
 Clawhammer

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

 Post subject: Posted: Sun Dec 12, 2004 6:48 pm
 Clawhammer

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

 Post subject: Posted: Sun Dec 12, 2004 6:53 pm
 Clawhammer

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 6 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets