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 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.
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 <stdio.h>
#define MAX 20
int x;
int size_request(void); //asks the user to enter the size x size square.
|
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...
|
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 java.util.*;
public class MagicSquare
{
public static void isMagic (int b)
{
int sum = 0;
|
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 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...
|
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...
|
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
|
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?
|
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. ...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |