473,484 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

magic square

11 New Member
Hi,
I am working in a program caals magic square and it must be done only with loops and user definied funcations,I will tell you about my code and where my problem, the main is only calls the other funcations,The first funcation makes a squence which the user input where it begins and the differents between easch elements the size of this one dimensional array is 16. the second funcation puts the elements of the one dimensional arry into a two dimensional size 4*4.funcation 3 only print, funcation 4 reverses both the diagonals, funcation 5 which I had a problem with it is that takes the one dimensional array and output it's sum but my problem it only takes the sum of the last four elements and it addes them 16 time, of course the program is not finished because the sum of the one dimensional array divided by four is the magic number,and then I must cheak the sum of the rows and columns to see if itis equal to the magic number.here is my code.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. const int ROWS=4;
  4. const int COLUMNS=4;
  5. const int size=16;
  6. void createArithmeticSeq (int list[], int size);
  7. void matricize (int list[], int matrix [ROWS][COLUMNS],int ROWS);
  8. void printMatrix (int matrix[][COLUMNS], int ROWS);
  9. void reverseDiagonal (int matrix[][COLUMNS], int ROWS);
  10. void magicCheak (int list[],int matrix[][COLUMNS],int size,int ROWS);
  11. int main ()
  12. {
  13.     int list [size];
  14.     int matrix [ROWS][COLUMNS];
  15.  
  16.  
  17.      createArithmeticSeq (list, size);
  18.      matricize (list, matrix , ROWS);
  19.      printMatrix (matrix, ROWS);
  20.      reverseDiagonal (matrix, ROWS);
  21.      printMatrix (matrix, ROWS);
  22.      magicCheak (list,matrix, size, ROWS);
  23.  
  24.  
  25.     return 0;
  26.  
  27. }
  28.  
  29. //first funcatio
  30. void createArithmeticSeq (int list[], int size)
  31.  
  32. {
  33.     int first, diff;
  34.     int i;
  35.  
  36.     cout<<"Enter the number that the sequence begin and the differance "<<endl;
  37.     cin>>first;
  38.     cin>>diff;
  39.  
  40.     for (i=0; i<size;i++)
  41.     {
  42.  
  43.         list[i]=first;
  44.         first=first+diff;
  45.         cout<<list[i]<<" ";
  46.     }
  47.  
  48. cout<<endl;
  49. }
  50.  
  51. //second funcation
  52. void matricize (int list[], int matrix [][COLUMNS],int ROWS)
  53.  
  54. {
  55.  
  56.     int i;
  57.     int j;
  58.  
  59.       for(j=0;j<16;j++)
  60.         for (i=0;i<ROWS;i++)
  61.         matrix[i][j]=list[j];
  62.  
  63. }
  64. //funcation    3
  65. void printMatrix (int matrix[][COLUMNS], int ROWS)
  66. {            
  67.  
  68.  
  69.  
  70.  
  71.     int row;
  72.     int col;
  73.     for (row=0;row<ROWS;row++)
  74.     {
  75.         for(col=0;col<COLUMNS;col++)
  76.             cout<<matrix [row][col]<<" ";
  77.     cout<<endl;
  78.     }
  79.  
  80.     cout<<endl;
  81. }
  82.  
  83.  
  84. //fourth funcatin
  85. void reverseDiagonal (int matrix[][COLUMNS], int ROWS)
  86.  
  87. {
  88.     int row;
  89.     int temp;
  90.  
  91.     for(row=0;row<ROWS/2;row++)
  92.     {
  93.         temp=matrix[row][row];
  94.         matrix[row][row]=matrix[ROWS-1-row][ROWS-1-row];
  95.         matrix[ROWS-1-row][ROWS-1-row]=temp;
  96.     }
  97.  
  98.     for (row=0;row<ROWS/2;row++)
  99.     {
  100.         temp=matrix[row][ROWS-1-row];
  101.         matrix[row][ROWS-1-row]=matrix[ROWS-1-row][row];
  102.         matrix[ROWS-1-row][row]=temp;
  103.  
  104.     }
  105.  
  106.  
  107.  
  108. }
  109. // funcatin 5
  110. void magicCheak (int list[],int matrix[][COLUMNS],int size,int ROWS)
  111.  
  112. {
  113.     int i;
  114.  
  115.     for (i=0;i<16;i++)
  116.         cout<<list[i]<<" ";
  117.  
  118.  
  119.  
  120. }
I tried this code for funcation two:
Expand|Select|Wrap|Line Numbers
  1. for(j = 0; j < COLUMNS; j++)
  2.     for(i = 0; i < ROWS; i++
  3.         matrix[i][j] = list[i++]
funcation four came out rihgt but the matrix was garzy numbers


so can anyone please help? thank you
Dec 1 '07 #1
4 3391
Ganon11
3,652 Recognized Expert Specialist
You will need to use a third variable in your matricize() function. Try making the for loops determine the row/column, and have a third int variable for determining which spot in list gets used - it will be incremented in every execution of the loop.
Dec 1 '07 #2
inferi9
11 New Member
I tried anothe variable (k) which goes all the way to 16 but I have two problem:

1_ I do not know where to put this loop and i tried everywhere without any good

2_ the matrix will be only contains the last number which is the last number in the one dimenstional array.
Dec 1 '07 #3
Ganon11
3,652 Recognized Expert Specialist
You do not need a third loop. Your two loops will properly assign the element to a new position in the matrix. All you need to do is use a third variable (k), starting at the beginning of the list (k = 0;). Every time you assign a number in list to the matrix, increment the k variable.
Dec 1 '07 #4
inferi9
11 New Member
Thank you soooooo much you were a great help
Dec 1 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
4720
by: winnerpl | last post by:
Hey guys I'm trying to get a magic square but I'm stuck with it printing out only 1's and 0's in random places. Hope you experts can can provide some input on what I'm doing wrong. #include...
1
10516
by: shaveta | last post by:
pls guide me to write a program in c to generate a magic square of size n*n , where n is odd A magic square is an n*n matrix of integer from 1 to n^2, where n is odd, such that the sum of every...
4
1811
by: jyck91 | last post by:
can any one tell me?? what should i do before i strating wirtitng the magic square programe
8
3702
KoreyAusTex
by: KoreyAusTex | last post by:
I am pretty new at programming and need some feedback as to why this program is not working??? It was a pretty big undertaking for me but I can't seem to figure out what is wrong: import...
2
2560
by: jyck91 | last post by:
i have done the magic square: #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 13 main() { FILE *fp; int i, j, n, row, column;
5
2198
by: magic man | last post by:
I need help ... I have very rudimentary VB skills. I am working on a topographical model of a magic square. I consider each cell in the square to be a solid structure to the height specified by the...
1
3893
by: magic man | last post by:
I am 50 years old ...and am working physical models of the math structure called a magic square .. for my own interest. My present problem is this. I have a topograhical model for the square...
1
4156
by: manju01 | last post by:
in the below program we can generate magic square of size 3-160 but i want to print the output like for magic size n ************************ * * * * * * 5 * 8 * 7 *...
2
968
by: j_depp_99 | last post by:
I would like to know if the 6x6 magic square can be done using the backtracking algorithm and if so how long would it take. I found a link online that does this pretty quick though: ...
0
6945
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7100
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7163
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5400
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4835
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4524
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3042
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3035
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
584
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.