magic sq probs

This is a discussion on magic sq probs within the C++ Programming forums, part of the General Programming Boards category; hi , can someone please look at the source of my frustration:? I am having probs with my magic sq ...

  1. #1
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    Unhappy magic sq probs

    hi , can someone please look at the source of my frustration:?
    I am having probs with my magic sq program...
    it is taking 6 numbers from an external file and determining whether they fit the criteria and if so calculating the sum of the magic square it makes..
    not sure if i am supposed to make ONE lareg square with the biggest working odd number or a bunch of little ones...
    eventually i will assign fuctions to different prts of the program but ai am stuck at this part-
    anyway i can't even get one to print out please look:
    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<cmath> 
    using namespace std; 
    const int SIZE = 17; 
    int square[SIZE][SIZE]={0};
    const int RANGE = (SIZE*SIZE); 
    ofstream outfile("C:\\1107OFZZZ.txt"); 
    int main ()
    {
    
    if(!outfile){ 
    cerr << "Cannot open output file" << endl; 
    return 1; 
    } 
    int count=0;
    int cols=0;
    int row=0;
    
    //build the magic square
    ifstream infile("C:\\1107IF.txt"); 
    if(!infile){ 
    cerr << "Cannot open input file" << endl; 
    
    } 
    
    outfile << "\n Magic Criteria: Write a program which prints out a magic square as well as its magic sum, for an odd integer s greater than 1 but less than 17.\n\n"; 
    while (infile >> count){
    bool f = true;
    if (bool f= true){
    outfile<< "\nMagic Number is "<< count << "\n" << endl;
    if (count > 1 && count < 17 && count%2 != 0){
    
    //make square here:
    //----------------------------------------------------------------
    cols++;
    if(row == -1 && cols == SIZE){
    
    row+=2;
    cols--;
    }
    else if (row == -1)
    row=SIZE-1;
    else if(cols == SIZE)
    cols = 0;
    else if (square[row][cols] != 0){
    
    row+=2;
    cols--;
    }
    
    
    square[row][cols] = count; 
    while (count < RANGE){
    //print magic square
    for(row=0;row<SIZE;row++){
    
    for(cols=0;cols<SIZE;cols++){
    outfile <<square[row][cols]<< setw(3);
    outfile <<"\n";
    
    }//while
    }//for
    }//for
    //----------------------------------------------------------------------end of square
    int sum = 0;
    for (cols = 0; cols < count ; cols++) // down column 2
    sum += square[0][cols];
    outfile << "Sum of Magic Number" << count <<" = " << sum <<"\n" << endl;
    outfile << "+-------------------------------------------------+" << "\n\n";
    }//if
    
    else {outfile << "Number does not fit magic Criteria\n"<< endl;
    outfile << "+-------------------------------------------------+" << "\n\n";
    bool f= false;
    
    
    }//if
    
    }//bool
    }//while
    
    return 0;
    }


    &#91;code]&#91;/code]tagged by Salem
    Didn't help - its not indented to start with

  2. #2
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35
    okay, i am starting from scratch again....
    here's my specifics for the square:
    Write a program which prints out a magic square as well as its "magic sum", for an odd integer s greater than 1 but less than 17. Your program should "trap" incorrect input and issue the appropriate error message. Create a formatted input file with the following data: 1 19 12 3 9 15 .
    It is never said whether one large square should be made or multi
    little ones. I don't even care at this point, I'd just like to understand how to make ONE.
    I can get the numbers from the ext file in...decide whether they meet MSquare criteria, but that's it.
    I've tried to write my own code to build the square..follow others algorithms..which are confusing because there's so many...
    and i can't even get ONE source to compile.
    (I am already a week begind with this and can't even get myself thru the basics)...I will need to assign functions to the code eventually, but i figured i better be able to run it first.
    I really WANT to understand it, but I am just getting more and more frustrated.
    Any pointers would be appreciated....
    i am trying to have the code do this:
    *Place the number 1 in the middle column of row 1.
    *From this starting position (original position) go up one cell and to the
    right one cell and place the next integer in that cell. If moving up forces
    you out of bounds, wrap around and continue from the corresponding
    cell in the bottom row.
    *Do the same thing if you go out of bounds when moving right.
    *If the cell you are moving to has already been filled, drop down one cell
    from your original position and place the integer there instead.
    *Do this until all the cells are filled.
    PLEASE help...I don't want anyone to write it for me,
    but I am so stuck frustrated.\
    Thanks alot...
    this is the last workable thing I've gotten to...
    (and the sum doesn't even work!)
    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<cmath> 
    using namespace std; 
    
    const int SIZE = 17; 
    int square[SIZE][SIZE]={0};
    
    const int RANGE = (SIZE*SIZE); 
    
    ofstream outfile("A:\\1107OFZZZ.txt"); 
    
    int main ()
    {
     
        if(!outfile){ 
            cerr << "Cannot open output file" << endl; 
            return 1; 
        } 
    
    
    
    
     int count=0;
     int cols=0;
     int row=0;
     
    
    //build the magic square
    ifstream infile("A:\\1107IF.txt"); 
        if(!infile){ 
            cerr << "Cannot open input file" << endl; 
            
        } 
    
    	outfile << "\n Magic Criteria: Write a program which prints out a magic square as well as its magic sum, for an odd integer s greater than 1 but less than 17.\n\n"; 
    
     while (infile >> count){
    		bool f = true;
    		if (bool f= true){
    		outfile<< "\nMagic Number :  "<< count << "\n" << endl;
    		if (count > 1 && count < 17 && count%2 != 0){
    					
    					outfile << "make sqare here" << "\n\n";
    						int sum = 0;
    						for (cols = 0; cols < count ; cols++)       // down column 2
    						sum += square[0][cols];
    						outfile << "Sum of Magic Number" << count <<" = " << sum <<"\n" << endl;
    						outfile << "+-------------------------------------------------+" << "\n\n";
    			}//if
    		
    					else {outfile << "Number does not fit magic Criteria\n"<< endl;
    					outfile << "+-------------------------------------------------+" << "\n\n";
    					bool f= false;
    
    						
    						
    			}//if
     
    		}//bool
     }//while
    
      
     return 0;
    }
    Michele

  3. #3
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    Thumbs up thx

    THANK YOU...
    I am getting very close to my final verison of the program..
    i have a couple of ?? though.
    I just bought Visual C++.net (the book)
    and cannot understand the arguments at main :
    Code:
     int argc, char *argv[]
    also i was also wondering if one large square could be made?
    thanks for your help..
    getting there....
    \Michele

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows crashing Magic Square
    By KoshiB in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2006, 10:02 PM
  2. help on magic square
    By katway in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 06:44 PM
  3. Magic Squares!
    By ZAM777 in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2004, 04:34 PM
  4. magic square whoes
    By caws in forum C Programming
    Replies: 9
    Last Post: 03-30-2003, 10:36 PM
  5. Help with magic square program
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-15-2002, 06:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21