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. - #include <iostream>
-
using namespace std;
-
const int ROWS=4;
-
const int COLUMNS=4;
-
const int size=16;
-
void createArithmeticSeq (int list[], int size);
-
void matricize (int list[], int matrix [ROWS][COLUMNS],int ROWS);
-
void printMatrix (int matrix[][COLUMNS], int ROWS);
-
void reverseDiagonal (int matrix[][COLUMNS], int ROWS);
-
void magicCheak (int list[],int matrix[][COLUMNS],int size,int ROWS);
-
int main ()
-
{
-
int list [size];
-
int matrix [ROWS][COLUMNS];
-
-
-
createArithmeticSeq (list, size);
-
matricize (list, matrix , ROWS);
-
printMatrix (matrix, ROWS);
-
reverseDiagonal (matrix, ROWS);
-
printMatrix (matrix, ROWS);
-
magicCheak (list,matrix, size, ROWS);
-
-
-
return 0;
-
-
}
-
-
//first funcatio
-
void createArithmeticSeq (int list[], int size)
-
-
{
-
int first, diff;
-
int i;
-
-
cout<<"Enter the number that the sequence begin and the differance "<<endl;
-
cin>>first;
-
cin>>diff;
-
-
for (i=0; i<size;i++)
-
{
-
-
list[i]=first;
-
first=first+diff;
-
cout<<list[i]<<" ";
-
}
-
-
cout<<endl;
-
}
-
-
//second funcation
-
void matricize (int list[], int matrix [][COLUMNS],int ROWS)
-
-
{
-
-
int i;
-
int j;
-
-
for(j=0;j<16;j++)
-
for (i=0;i<ROWS;i++)
-
matrix[i][j]=list[j];
-
-
}
-
//funcation 3
-
void printMatrix (int matrix[][COLUMNS], int ROWS)
-
{
-
-
-
-
-
int row;
-
int col;
-
for (row=0;row<ROWS;row++)
-
{
-
for(col=0;col<COLUMNS;col++)
-
cout<<matrix [row][col]<<" ";
-
cout<<endl;
-
}
-
-
cout<<endl;
-
}
-
-
-
//fourth funcatin
-
void reverseDiagonal (int matrix[][COLUMNS], int ROWS)
-
-
{
-
int row;
-
int temp;
-
-
for(row=0;row<ROWS/2;row++)
-
{
-
temp=matrix[row][row];
-
matrix[row][row]=matrix[ROWS-1-row][ROWS-1-row];
-
matrix[ROWS-1-row][ROWS-1-row]=temp;
-
}
-
-
for (row=0;row<ROWS/2;row++)
-
{
-
temp=matrix[row][ROWS-1-row];
-
matrix[row][ROWS-1-row]=matrix[ROWS-1-row][row];
-
matrix[ROWS-1-row][row]=temp;
-
-
}
-
-
-
-
}
-
// funcatin 5
-
void magicCheak (int list[],int matrix[][COLUMNS],int size,int ROWS)
-
-
{
-
int i;
-
-
for (i=0;i<16;i++)
-
cout<<list[i]<<" ";
-
-
-
-
}
I tried this code for funcation two: - for(j = 0; j < COLUMNS; j++)
-
for(i = 0; i < ROWS; i++
-
matrix[i][j] = list[i++]
funcation four came out rihgt but the matrix was garzy numbers
so can anyone please help? thank you
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.
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.
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.
Thank you soooooo much you were a great help
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
by: jyck91 |
last post by:
can any one tell me?? what should i do before i strating wirtitng the
magic square programe
|
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...
|
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;
| |
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...
|
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...
|
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 *...
|
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:
...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |