473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

magic square

11
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 3386
Ganon11
3,652 Expert 2GB
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
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 Expert 2GB
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
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
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
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
by: jyck91 | last post by:
can any one tell me?? what should i do before i strating wirtitng the magic square programe
8
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...

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.