473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ 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 3403
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
4725
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 <stdio.h> #define MAX 20 int x; int size_request(void); //asks the user to enter the size x size square.
1
10526
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 row, column and diagonal is same. The rule is - Start with 1 in the middle of the first row; then go up and left , assigning nos. in increasing...
4
1815
by: jyck91 | last post by:
can any one tell me?? what should i do before i strating wirtitng the magic square programe
8
3707
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 java.util.*; public class MagicSquare { public static void isMagic (int b) { int sum = 0;
2
2564
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
2205
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 value in the square. Then conceptually I pour water on the structure and wish to see where the water collects in "lakes" on the structure. What...
1
3902
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 ... where each cell in the square is a solid structure to the height specified by the value in that cell. Then conceptually I pour water on top of the...
1
4163
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 * 6 * ************************ that is in grid
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: www.faust.fr.bw.schule.de/mhb/backtrack/mag6en.htm I've found some information on the siamese method and is this related to the backtracking algorithm?
0
7711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6039
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5367
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3497
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.