Click here to Skip to main content
15,923,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't know what I am missing from my program, but all the diagonals are not equal, and I am receiving zeroes in most of the diagonals.

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

ofstream outfile;

int main()
{
	outfile.open("output.txt");
	

	int magicnumber[19][19], n; //n = size of array
	//magicnumber = user input for calculation

	outfile << "Welcome to  the Magic Square Program!" << endl << endl;
	cout << "Welcome to  the Magic Square Program!" << endl << endl;

	cout << "Enter an odd digit: ";
	cin >> magicnumber[19][19];
	outfile << "Number user entered: " << magicnumber[19][19] << endl;

	//this loop allows the user to only input odd digits
	if (magicnumber[19][19] % 2 == 0){ //if modulus by two equals no remainder
		do{							//run loop	
			cout << "Please input odd integers only: " << endl;
			cin >> magicnumber[19][19];
		} while (magicnumber[19][19] % 2 == 0); //post loop conditions
	}

	cout << "Magic Box#: " << " > " << magicnumber[19][19] << " <" << endl << endl;
	outfile << "Magic Box#: " << " > " << magicnumber[19][19] << " <" << endl << endl;	
	
	cout << "Now enter the size of the array: ";
	cin >> n;

	//this segment of loops and if statements
	//will move, place, and relocate
	//numbers and calculate numbers within the square
		for (int x = 0; x < n; x++)
		{
			for (int y = 0; y < n; y++){
				magicnumber[x][y] = 0;
			}
			int row = 0;
			int col = (n / 2);

			for (int num = 1; num < n; num++){
				if (row < 0)
					row = n - 1;

				if (col > n - 1)
					col = 0;


				if (magicnumber[row][col] == 0){
					magicnumber[row][col] = num;
				}
				row--;
				col++;
			}
		}
		//this segment will orgnanize the integers
		//into the magic square
		for (int row = 0; row < n; row++){
			for (int col = 0; col < n; col++){
				cout << setw(3) << magicnumber[row][col];
				outfile << setw(3) << magicnumber[row][col];
			}
			cout << endl;
			outfile << endl;
		
		}
	system("pause");
	return 0;
}
Posted
Comments
Sergey Alexandrovich Kryukov 28-Feb-15 21:00pm    
Did you use the debugger? The algorithm itself needs thinking, but checking up the implementation of existing algorithm using the debugger?.. not really.
—SA
Trevon Harris 1-Mar-15 2:34am    
How do you use the debugger, we never learned how to use the debugger. I wish we knew how to use it because debugging programs is just as important as writing them.
Andreas Gieriet 1-Mar-15 5:32am    
Go and teach yourself on debugging. There are plenty of tutorials (lookup in Google, e.g. C++ Visual Studio debugger tutorial (or choose Eclipse or any another system/IDE if you do not work in Visual Studio).

And as I mentioned in an earlier of your threads: Stand on your own feet! - you will never excel if you only do what others tell you to do and if you don't venture yourself into the unknown!
Cheers
Andi
PS: If for whatever reason you do not manage to use the Debugger, go and do some kind of "printf" debugging: add simple output (e.g. to cout/cerr) to see where the control flow goes through and what the relevant values are...

1 solution

Harris,there are some mistakes in your logic.I just corrected your code.try this


// ex2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
 
ofstream outfile;
 
int main()
{
	outfile.open("output.txt");
	
 
	int magicnumber[19][19] ={0}, n; //n = size of array
	//magicnumber = user input for calculation
	int lastX=0,lastY=0;

	outfile << "Welcome to  the Magic Square Program!" << endl << endl;
	cout << "Welcome to  the Magic Square Program!" << endl << endl;
 
	cout << "Enter an odd digit: ";
	cin >> magicnumber[19][19];
	outfile << "Number user entered: " << magicnumber[19][19] << endl;
 
	//this loop allows the user to only input odd digits
	if (magicnumber[19][19] % 2 == 0){ //if modulus by two equals no remainder
		do{							//run loop	
			cout << "Please input odd integers only: " << endl;
			cin >> magicnumber[19][19];
		} while (magicnumber[19][19] % 2 == 0); //post loop conditions
	}
 
	cout << "Magic Box#: " << " > " << magicnumber[19][19] << " <" << endl << endl;
	outfile << "Magic Box#: " << " > " << magicnumber[19][19] << " <" << endl << endl;	
	
	cout << "Now enter the size of the array: ";
	cin >> n;
 
	//this segment of loops and if statements
	//will move, place, and relocate
	//numbers and calculate numbers within the square
	//for (int x= 0,y = 0; y < n; y++){
	//	magicnumber[x][y] = 0;
	//}
		/*for (int x = 0; x < n; x++)
		{*/

			int row = 0;
			int col = (n / 2);
 
			for (int num = 1; num <= n*n; num++){
				if (row < 0)
					row = n - 1;
 
				if (col > n - 1)
					col = 0;
 

				if (magicnumber[row][col] == 0){
					magicnumber[row][col] = num;
				}
				else
				{
					magicnumber[lastX + 1][lastY ] = num;
					row = lastX + 1;
					col = lastY;
				}

				lastX = row;
				lastY = col;
				row--;
				col++;
			}
		//}
		//this segment will orgnanize the integers
		//into the magic square
		for (int row = 0; row < n; row++){
			for (int col = 0; col < n; col++){
				cout << setw(3) << magicnumber[row][col];
				outfile << setw(3) << magicnumber[row][col];
			}
			cout << endl;
			outfile << endl;
		
		}
	system("pause");
	return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900