hello, i am new to C, or C++. I know that there is a rule here stating that an improvement need to be made (starting the program)before getting help.

i have to make a program (Magic square) that asks for a number to start. Well i was told by my teacher that i cannot use vectores. Only If, do while or for statements are accepted.

I dont have an idea how to get started on this program. Can someone explain to me how should i go about creating this small program please? How and what statements should i use. Thank you

## 8 Replies - 40180 Views - Last Post: 28 September 2011 - 03:16 AM

##
**Replies To:** Magic Square

### #2

## Re: Magic Square

Posted 16 April 2007 - 12:29 PM

No problem...have you written any programs before? Like "Hello World"? the program entry point will be the same type of thing:

int main(void) { int num; cout<<"Please enter a number:"<<endl; cin>>num; //rest of code here return 0; }

### #3

## Re: Magic Square

Posted 16 April 2007 - 12:35 PM

yes, i have writen simple codes like this. But how do i go about printing the numbers in the magic square. I am not allowed to use vectores. He was telling me that i need to use the function (goto x,y).

### #4

## Re: Magic Square

Posted 16 April 2007 - 12:39 PM

vyiruskiller, on 16 Apr, 2007 - 12:26 PM, said:

hello, i am new to C, or C++. I know that there is a rule here stating that an improvement need to be made (starting the program)before getting help.

i have to make a program (Magic square) that asks for a number to start. Well i was told by my teacher that i cannot use vectores. Only If, do while or for statements are accepted.

I dont have an idea how to get started on this program. Can someone explain to me how should i go about creating this small program please? How and what statements should i use. Thank you

i have to make a program (Magic square) that asks for a number to start. Well i was told by my teacher that i cannot use vectores. Only If, do while or for statements are accepted.

I dont have an idea how to get started on this program. Can someone explain to me how should i go about creating this small program please? How and what statements should i use. Thank you

Well, I'm not really sure exactly what your problem is, but:

First you need to prompt the user for the number of rows and columns in the table (square). Since by definition the length (columns) and width (rows) of a square (table) are equal, you only need to read in one number.

Once you have the dimensions of the square, you can write for loops that will read in the numbers, sum each row, sum each column, and sum each diagonal. You must also determine if all of the sums are equal.

### #5

## Re: Magic Square

Posted 16 April 2007 - 04:42 PM

Well lets see. A Magic square is a square NxN of number arranged (from 1 to N^2) on a grid so that if you add up any row, collumn, or diagonal the answer will be the same.

┌───┬───┬───┐

│ 4 │ 9 │ 2 │

├───┼───┼───┤

│ 3 │ 5 │ 7 │ == 15

├───┼───┼───┤

│ 8 │ 1 │ 6 │

└───┴───┴───┘

Well lets see... that looks like a 2-dimentional array. No need to use vectors there.

There is a formula for calculating what the "magic sum" will be.

Now as the dimentions go up, the number of possible solutions goes up, and the chance of finding one goes down. For the odd dimentions there is a nice algorithm that will find a subset of the solutions easily. However for even magic squares the problem becomes more complex. There are formula for 4x4 and 6x6, even 8x8 and larger, but the algorithms don't work from one to the next. There are even algorithms for solving certain classes of even magic squares. but there does not exitst a general - none-brute-force approch...

How to start the program... Well start by making a model of the data. Add some functions to sum rows, collums, and diagonals. Learn the basic odd-algorithm and code that. Once you have gone this far I am sure you can work out how to try brute force approches... these should not JUST try random attempts, rather they should find the magic sum and permutations of size N of number < N^2 which eual the sum, and try to arrange these.

┌───┬───┬───┐

│ 4 │ 9 │ 2 │

├───┼───┼───┤

│ 3 │ 5 │ 7 │ == 15

├───┼───┼───┤

│ 8 │ 1 │ 6 │

└───┴───┴───┘

Well lets see... that looks like a 2-dimentional array. No need to use vectors there.

There is a formula for calculating what the "magic sum" will be.

Now as the dimentions go up, the number of possible solutions goes up, and the chance of finding one goes down. For the odd dimentions there is a nice algorithm that will find a subset of the solutions easily. However for even magic squares the problem becomes more complex. There are formula for 4x4 and 6x6, even 8x8 and larger, but the algorithms don't work from one to the next. There are even algorithms for solving certain classes of even magic squares. but there does not exitst a general - none-brute-force approch...

How to start the program... Well start by making a model of the data. Add some functions to sum rows, collums, and diagonals. Learn the basic odd-algorithm and code that. Once you have gone this far I am sure you can work out how to try brute force approches... these should not JUST try random attempts, rather they should find the magic sum and permutations of size N of number < N^2 which eual the sum, and try to arrange these.

### #6

## Re: Magic Square

Posted 17 April 2007 - 03:46 PM

THANK YOU VERY MUCH. NOW I UNDERSTAND HOW CAN I GO ABOUT MAKING THAT PROGRAM.

NickDMax, on 16 Apr, 2007 - 04:42 PM, said:

Well lets see. A Magic square is a square NxN of number arranged (from 1 to N^2) on a grid so that if you add up any row, collumn, or diagonal the answer will be the same.

+---+---+---+

| 4 | 9 | 2 |

+---+---+---+

| 3 | 5 | 7 | == 15

+---+---+---+

| 8 | 1 | 6 |

+---+---+---+

Well lets see... that looks like a 2-dimentional array. No need to use vectors there.

There is a formula for calculating what the "magic sum" will be.

Now as the dimentions go up, the number of possible solutions goes up, and the chance of finding one goes down. For the odd dimentions there is a nice algorithm that will find a subset of the solutions easily. However for even magic squares the problem becomes more complex. There are formula for 4x4 and 6x6, even 8x8 and larger, but the algorithms don't work from one to the next. There are even algorithms for solving certain classes of even magic squares. but there does not exitst a general - none-brute-force approch...

How to start the program... Well start by making a model of the data. Add some functions to sum rows, collums, and diagonals. Learn the basic odd-algorithm and code that. Once you have gone this far I am sure you can work out how to try brute force approches... these should not JUST try random attempts, rather they should find the magic sum and permutations of size N of number < N^2 which eual the sum, and try to arrange these.

+---+---+---+

| 4 | 9 | 2 |

+---+---+---+

| 3 | 5 | 7 | == 15

+---+---+---+

| 8 | 1 | 6 |

+---+---+---+

Well lets see... that looks like a 2-dimentional array. No need to use vectors there.

There is a formula for calculating what the "magic sum" will be.

Now as the dimentions go up, the number of possible solutions goes up, and the chance of finding one goes down. For the odd dimentions there is a nice algorithm that will find a subset of the solutions easily. However for even magic squares the problem becomes more complex. There are formula for 4x4 and 6x6, even 8x8 and larger, but the algorithms don't work from one to the next. There are even algorithms for solving certain classes of even magic squares. but there does not exitst a general - none-brute-force approch...

How to start the program... Well start by making a model of the data. Add some functions to sum rows, collums, and diagonals. Learn the basic odd-algorithm and code that. Once you have gone this far I am sure you can work out how to try brute force approches... these should not JUST try random attempts, rather they should find the magic sum and permutations of size N of number < N^2 which eual the sum, and try to arrange these.

### #10

## Re: Magic Square

Posted 23 June 2007 - 09:56 AM

NickDMax, on 16 Apr, 2007 - 04:42 PM, said:

Well lets see. A Magic square is a square NxN of number arranged (from 1 to N^2) on a grid so that if you add up any row, collumn, or diagonal the answer will be the same.

┌───┬───┬───┐

│ 4 │ 9 │ 2 │

├───┼───┼───┤

│ 3 │ 5 │ 7 │ == 15

├───┼───┼───┤

│ 8 │ 1 │ 6 │

└───┴───┴───┘

Well lets see... that looks like a 2-dimentional array. No need to use vectors there.

There is a formula for calculating what the "magic sum" will be.

Now as the dimentions go up, the number of possible solutions goes up, and the chance of finding one goes down. For the odd dimentions there is a nice algorithm that will find a subset of the solutions easily. However for even magic squares the problem becomes more complex. There are formula for 4x4 and 6x6, even 8x8 and larger, but the algorithms don't work from one to the next. There are even algorithms for solving certain classes of even magic squares. but there does not exitst a general - none-brute-force approch...

How to start the program... Well start by making a model of the data. Add some functions to sum rows, collums, and diagonals. Learn the basic odd-algorithm and code that. Once you have gone this far I am sure you can work out how to try brute force approches... these should not JUST try random attempts, rather they should find the magic sum and permutations of size N of number < N^2 which eual the sum, and try to arrange these.

┌───┬───┬───┐

│ 4 │ 9 │ 2 │

├───┼───┼───┤

│ 3 │ 5 │ 7 │ == 15

├───┼───┼───┤

│ 8 │ 1 │ 6 │

└───┴───┴───┘

Well lets see... that looks like a 2-dimentional array. No need to use vectors there.

There is a formula for calculating what the "magic sum" will be.

Now as the dimentions go up, the number of possible solutions goes up, and the chance of finding one goes down. For the odd dimentions there is a nice algorithm that will find a subset of the solutions easily. However for even magic squares the problem becomes more complex. There are formula for 4x4 and 6x6, even 8x8 and larger, but the algorithms don't work from one to the next. There are even algorithms for solving certain classes of even magic squares. but there does not exitst a general - none-brute-force approch...

How to start the program... Well start by making a model of the data. Add some functions to sum rows, collums, and diagonals. Learn the basic odd-algorithm and code that. Once you have gone this far I am sure you can work out how to try brute force approches... these should not JUST try random attempts, rather they should find the magic sum and permutations of size N of number < N^2 which eual the sum, and try to arrange these.

### #11

## Re: Magic Square

Posted 28 September 2011 - 02:55 AM

#include <stdio.h> int main (void) { int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16, row1, row2, row3, row4, col1, col2, col3, col4, diag1, diag2; #define sum 34; printf("Enter the numbers 1 to 16 in any order.\n"); scanf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", &num1, &num2, &num3, &num4, &num5, &num6, &num7, &num8, &num9, &num10, &num11, &num12, &num13, &num14, &num15, &num16); printf("%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d", num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16); row1= num1+num2+num3+num4; row2= num5+num6+num7+num8; row3= num9+num10+num11+num12; row4= num13+num14+num15+num16; col1= num1+num5+num9+num13; col2= num2+num6+num10+num14; col3= num3+num7+num11+num15; col4= num4+num8+num13+num16; diag1= num1+num6+num11+num16; diag2= num4+num7+num10+num13; printf("Row sums: %d, %d, %d, %d \n", row1, row2, row3, row4); printf("Column sums: %d, %d, %d, %d \n", col1, col2, col3, col4); printf("Diagonal sums: %d, %d \n", diag1, diag2); if(row1=sum && row2=sum && row3=sum && row4=sum && col1=sum && col2=sum && col3=sum && col4=sum && diag1=sum && diag2=sum) printf("Congratulations, these numbers form a magic square!"); else printf("Im sorry this is not a magic square."); getch (0); return (0); }

here is my answer to your question tho however i do have to ask another question because the program above wont run even tho it should i dont see why it wont.

The only issue is in the if statement i think i dont see why the statement wont allow me to use sum=34 or allow me to define sum though when i defined sum it changed the problem it was giving me where as before with the sum=34 it said i had to use an lvalue and now with sum defined as 34 it is saying im missing a ')'.

in regards to it not letting me set sum=34 i dont understand because it will allow to use row1...etc and they are the same thing essentially are they not?

I'm just beginning college and have only a basic understanding of these things.

### #12

## Re: Magic Square

Posted 28 September 2011 - 03:16 AM

i think i understand my issue now that iv sat and pondered for the last few hours lol. instead of setting sum to a number it needs to be able to check all the totals and see if they are the same rather then only checking for one answer but in that same realization i have tried to use a different variable and not give it a value at all letting it check to make sure all the sums are the same rather then just a single number however that didnt work either it just started saying i needed to use an lvalue again, im not sure if its how i set it or if there is a fundamental flaw in the logic?

Page 1 of 1