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[MAX][MAX];
int size_request(vo id); //asks the user to enter the size x size square.
void fill_square(int[][], int); //fills up the magic square with the
appropriate numbers.
void print_square(in t[], int); //prints out the magic square.
int main(void)
{
int roww;
int size = size_request();
x[size][size];
fill_square(x, size);
for(roww = 0; roww < size; roww++)
print_square(x[roww], size);
return 0;
}
int size_request(vo id)
{
int size;
do
{
printf("Enter a positive, odd integer in the range from 1 to 15:
");
scanf("%d", &size);
} while((size % 2 != 1) ||(size > 15) || (size < 1));
printf("\n");
return size;
}
void fill_square(int x[][MAX], int size)
{
int count;
int row = size - 1;
int col = row / 2;
int maxnum = size * size;
for(count = 1; count < maxnum; count++)
{
x [row][col] = 1;
if(x [row-2][col+1] == 0)
{
row = row - 2;
col = col + 1;
if (col == size)
col = col - size;
else if (row < 0)
row = row + size;
}
else
{
row --;
if (row < 0)
row = row + size;
}
}
return;
}
void print_square(in t x[], int size)
{
int colw;
for(colw = 0; colw < size; colw++)
printf("%4d", x[colw] * x[colw]);
printf("\n");
return;
}
Thanks in advance guys. 4 4725
On Sun, 31 Oct 2004 21:07:29 -0500, "winnerpl" <wi******@hotma il.com>
wrote in comp.lang.c: 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.
OK, first of all you are writing a program in some language that sort
of resembles C, but is not actually C.
#include <stdio.h> #define MAX 20
int x[MAX][MAX];
int size_request(vo id); //asks the user to enter the size x size square. void fill_square(int[][], int); //fills up the magic square with the appropriate numbers.
Note, don't ever use // comments in C or C++ code posted to usenet.
Line wrapping by your news software has moved "appropriat e numbers."
to the next line, where it is nothing but a syntax error when somebody
tries to compiler your code. Use /* */ comments, and line breaking
can't mess them up.
In any case, the prototype above is not valid C, it is a syntax error.
There is no such thing as a function array parameter with more than
one empty '[ ]' pair. If your compiler accepts this, it is either not
a C compiler or you are not invoking it to operate in standard C
conforming mode.
void print_square(in t[], int); //prints out the magic square.
int main(void) {
int roww; int size = size_request(); x[size][size];
What do you think that this line does? You have already defined the
array at file scope. It doesn't change the size of the array. All it
does is evaluate the value of the int in the array at x[size][size],
which has the value 0, and then throws away that 0 value.
fill_square(x, size);
for(roww = 0; roww < size; roww++) print_square(x[roww], size);
return 0; }
int size_request(vo id) { int size; do { printf("Enter a positive, odd integer in the range from 1 to 15: "); scanf("%d", &size); } while((size % 2 != 1) ||(size > 15) || (size < 1));
printf("\n");
return size; }
void fill_square(int x[][MAX], int size) { int count; int row = size - 1; int col = row / 2; int maxnum = size * size;
Let's see, if 'size' is 1, a value accepted by your size_request()
function, then 'row', 'col' are both 0 and 'maxnum' is 1.
for(count = 1; count < maxnum; count++)
In that case, this loop has a real problem, it will never execute at
all because 'count' starts at 1 and is never less than 'maxnum' if
'size' is 0.
{ x [row][col] = 1;
The line above is the only line that ever writes a value into the
array. The only value it ever writes is 1, so it is hard to see how
the array can have any values other than 0 (what it was initialized
with) or 1 in it. Perhaps you meant to write:
x [row][col] = count;
if(x [row-2][col+1] == 0)
Using 'row-2' as a subscript will produce undefined behavior if 'size'
is 1, although since this code won't execute when 'size' is 1, I don't
suppose that matters much.
{ row = row - 2;
Why not 'row -= 2'?
col = col + 1;
Why not 'col += 1' or just '++col'?
if (col == size) col = col - size;
The line above could be replaced with 'col = 0'.
else if (row < 0) row = row + size; } else { row --; if (row < 0) row = row + size; } } return; }
void print_square(in t x[], int size) { int colw;
for(colw = 0; colw < size; colw++) printf("%4d", x[colw] * x[colw]);
printf("\n"); return; } Thanks in advance guys.
Don't they teach people how to debug anymore? Have you single stepped
through your 'fill_square()' function to see what it does?
I made the following changes to your code:
1. Change prototype to 'void fill_square(int[MAX][MAX], int);' and
changed the function definition to match.
2. Eliminated the do-nothing 'x[size][size];' statement from main().
3. Changed 'x [row][col] = 1;' in fill_square() to 'x [row][col] =
count;'.
4. Added line right after that:
printf("x [%d][%d] = %d\n", row, col, count);
Now when I run your program and enter '3' for size, I get this output:
Enter a positive, odd integer in the range from 1 to 15: 3
x [2][1] = 1
x [0][2] = 2
x [-2][0] = 3
x [-1][1] = 4
x [0][2] = 5
x [-2][0] = 6
x [-1][1] = 7
x [0][2] = 8
0 0 64
0 0 0
0 1 0
....so it appears that you are using the expression [row-2] when row is
0, and you are accessing memory outside the array. You need to work
on your subscript expressions.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Jack Klein" <ja*******@spam cop.net> wrote: void fill_square(int[][], int); ... the prototype above is not valid C,
Agreed.
it is a syntax error.
I guess you could argue your case here, given that the (outer) array
element's type can be deduced as incomplete from the syntax alone. [But a
part of me thinks you're stretching it with this statement. :-]
There is no such thing as a function array parameter with more than one empty '[ ]' pair. If your compiler accepts this, it is either not a C compiler or you are not invoking it to operate in standard C conforming mode.
% dir /B decl.*
decl.c
% type decl.c
void blah(int [][]);
% gcc -c -ansi -pedantic decl.c
decl.c:1: warning: array type has incomplete element type
% dir /B decl.*
decl.c
decl.o
%
A conforming implementation _is_ allowed to accept the code, so long as it
emits the required diagnostic.
--
Peter
Jack Klein <ja*******@spam cop.net> writes: On Sun, 31 Oct 2004 21:07:29 -0500, "winnerpl" <wi******@hotma il.com> wrote in comp.lang.c:
void fill_square(int[][], int);
[some snippage]
In any case, the prototype above is not valid C, it is a syntax error. There is no such thing as a function array parameter with more than one empty '[ ]' pair. If your compiler accepts this, it is either not a C compiler or you are not invoking it to operate in standard C conforming mode.
Both a prototype and a function definition with an 'int[][]' type were
accepted by 'gcc -(ansi|std=c99) -pedantic' -- some warnings, but no
errors, and certainly no syntax errors. Is gcc in error here?
Or does the standard actually allow this?
Incidentally, the meaning assigned seems to be the same as though
it were like this:
int f( int (*)[] ); /* or int f( int [][] ); */
int f( int (*x)[] ){ /* or int f( int x[][] ){ */
return (*x)[0];
}
extern int a[];
int main(){
return f( &a ) == 0;
}
Of course, I'm making no claim that code like this is sensible.
Only asking if it's legal.
On 01 Nov 2004 20:41:54 -0800
Tim Rentsch <tx*@alumnus.ca ltech.edu> wrote: Jack Klein <ja*******@spam cop.net> writes:
On Sun, 31 Oct 2004 21:07:29 -0500, "winnerpl" <wi******@hotma il.com> wrote in comp.lang.c:
void fill_square(int[][], int);
[some snippage]
In any case, the prototype above is not valid C, it is a syntax error. There is no such thing as a function array parameter with more than one empty '[ ]' pair. If your compiler accepts this, it is either not a C compiler or you are not invoking it to operate in standard C conforming mode.
Both a prototype and a function definition with an 'int[][]' type were accepted by 'gcc -(ansi|std=c99) -pedantic' -- some warnings, but no errors, and certainly no syntax errors. Is gcc in error here? Or does the standard actually allow this?
<snip>
The C standard does not discriminate between warnings and diagnostics,
and once a compiler has produced the required diagnostic it is allowed
to produce a program. So yes, gcc is allowed to do this since it has
produced a diagnostic.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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: inferi9 |
last post by:
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...
|
by: sanaanand2008 |
last post by:
can u pls help me out write the prgm to check whether the entered matrix is a magic square or not?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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: 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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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.
| |