How can i open the magic square in the text file????

J

jyck91

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;
int magic[SIZE][SIZE], row_sum[SIZE],col_sum[SIZE];
char option[1];
fp = fopen("Magic_square.txt","w");
do {
printf("Enter a magic number (3,5,7,9 or 11):
");fprintf(fp,"Enter a magic number (3,5,7,9 or 11): \n");
scanf("%d", &n);
for (row=0;row<n;row++)
for (column=0;column<n;column++)
magic[row][column] = 0;
row = 0;
column = (n - 1) / 2;
for (i=1;i<=n*n;i++) {
magic[row][column] = i;
if ((row == 0) && (column == 0))
row = 1;
else if (row == 0) {
row = n - 1;
column--;
}
else if (column == 0) {
row--;
column = n - 1;
}
else if (magic[row-1][column-1] == 0) {
row--;
column--;
}
else
row++;
}
for (i=0;i<n;i++)
row_sum = 0;
for (j=0;j<n;j++)
col_sum[j] = 0;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
row_sum += magic[j];
for (j=0;j<n;j++)
for (i=0;i<n;i++)
col_sum[j] += magic[j];

for (row=0;row<n;row++) {
for (column=0;column<n;column++)
printf("======");fprintf(fp,"======");
printf("=\n"); fprintf(fp,"=\n");
for (column=0;column<n;column++)
printf("|%4d ",magic[row][column]); fprintf(fp,"|%4d
",magic[row][column]);
printf("|%4d\n",row_sum[row]); fprintf(fp,"|%4d
\n",row_sum[row]);
}
for (column=0;column<n;column++)
printf("======"); fprintf(fp,"======");
printf("=\n"); fprintf(fp,"=\n");
for (column=0;column<n;column++)
printf(" %4d ",col_sum[column]); fprintf(fp," %4d
",col_sum[column]);
printf("\n"); fprintf(fp,"\n");
printf("Try again (Y or N)? "); fprintf(fp,"Try again (Y or
N)? ");
scanf("%s",option);
} while ((strcmp(option,"Y") == 0) || (strcmp(option,"y") == 0));
fclose(fp);
system("PAUSE");
}

but i want to modify it and open in a text file which 25*25(maximum)
 
J

jyck91

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;
int magic[SIZE][SIZE], row_sum[SIZE],col_sum[SIZE];
char option[1];
fp = fopen("Magic_square.txt","w");
do {
printf("Enter a magic number (3,5,7,9 or 11):
");fprintf(fp,"Enter a magic number (3,5,7,9 or 11): \n");
scanf("%d", &n);
for (row=0;row<n;row++)
for (column=0;column<n;column++)
magic[row][column] = 0;
row = 0;
column = (n - 1) / 2;
for (i=1;i<=n*n;i++) {
magic[row][column] = i;
if ((row == 0) && (column == 0))
row = 1;
else if (row == 0) {
row = n - 1;
column--;
}
else if (column == 0) {
row--;
column = n - 1;
}
else if (magic[row-1][column-1] == 0) {
row--;
column--;
}
else
row++;
}
for (i=0;i<n;i++)
row_sum = 0;
for (j=0;j<n;j++)
col_sum[j] = 0;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
row_sum += magic[j];
for (j=0;j<n;j++)
for (i=0;i<n;i++)
col_sum[j] += magic[j];

for (row=0;row<n;row++) {
for (column=0;column<n;column++)
printf("======");fprintf(fp,"======");
printf("=\n"); fprintf(fp,"=\n");
for (column=0;column<n;column++)
printf("|%4d ",magic[row][column]); fprintf(fp,"|%4d
",magic[row][column]);
printf("|%4d\n",row_sum[row]); fprintf(fp,"|%4d
\n",row_sum[row]);
}
for (column=0;column<n;column++)
printf("======"); fprintf(fp,"======");
printf("=\n"); fprintf(fp,"=\n");
for (column=0;column<n;column++)
printf(" %4d ",col_sum[column]); fprintf(fp," %4d
",col_sum[column]);
printf("\n"); fprintf(fp,"\n");
printf("Try again (Y or N)? "); fprintf(fp,"Try again (Y or
N)? ");
scanf("%s",option);
} while ((strcmp(option,"Y") == 0) || (strcmp(option,"y") == 0));
fclose(fp);
system("PAUSE");

}

but i want to modify it and open in a text file which 25*25(maximum)


I mean -- show the magic square in the text file
 
M

Mark Bluemel

i have done the magic square: ....
for (row=0;row<n;row++) {
for (column=0;column<n;column++)
printf("======");fprintf(fp,"======");

All your loops of this form are broken. Code like this would work (for
some value of work) :-

for (row=0;row<n;row++) {
for (column=0;column<n;column++)
printf("======"),fprintf(fp,"======");

I'll leave it to you to work out why and decide if there may be a better
solution (hint: there almost certainly is).
 
A

Army1987

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;
int magic[SIZE][SIZE], row_sum[SIZE],col_sum[SIZE];
char option[1];
fp = fopen("Magic_square.txt","w");
You should check whether it succeeds.
do {
printf("Enter a magic number (3,5,7,9 or 11):
");fprintf(fp,"Enter a magic number (3,5,7,9 or 11): \n");
scanf("%d", &n);
You should check whether it succeeds.
for (row=0;row<n;row++)
for (column=0;column<n;column++)
magic[row][column] = 0;
row = 0;
column = (n - 1) / 2;
for (i=1;i<=n*n;i++) {
magic[row][column] = i;
if ((row == 0) && (column == 0))
row = 1;
else if (row == 0) {
row = n - 1;
column--;
}
else if (column == 0) {
row--;
column = n - 1;
}
else if (magic[row-1][column-1] == 0) {
row--;
column--;
}
else
row++;
The else if chain is clear enough even if you don't accumulate
indentation like that.
}
for (i=0;i<n;i++)
row_sum = 0;
for (j=0;j<n;j++)
col_sum[j] = 0;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
row_sum += magic[j];
for (j=0;j<n;j++)
for (i=0;i<n;i++)
col_sum[j] += magic[j];

for (row=0;row<n;row++) {
for (column=0;column<n;column++)
printf("======");fprintf(fp,"======");
printf("=\n"); fprintf(fp,"=\n");
for (column=0;column<n;column++)
printf("|%4d ",magic[row][column]); fprintf(fp,"|%4d
",magic[row][column]);
printf("|%4d\n",row_sum[row]); fprintf(fp,"|%4d
\n",row_sum[row]);
}
for (column=0;column<n;column++)
printf("======"); fprintf(fp,"======");
printf("=\n"); fprintf(fp,"=\n");
for (column=0;column<n;column++)
printf(" %4d ",col_sum[column]); fprintf(fp," %4d
",col_sum[column]);
printf("\n"); fprintf(fp,"\n");
printf("Try again (Y or N)? "); fprintf(fp,"Try again (Y or
N)? ");

Use fflush(stdout), or else the prompt could not be visible until
the next '\n' is displayed.
scanf("%s",option);
option has one member. In the best case this scanf will write two
chars (e.g. 'Y' and '\0'). Use scanf("%c", option);
You don't even have to declare option as an array (but if you
don't you'll have to use &option). Or, more simply, use getchar().
(strcmp(option,"y") == 0));
Couldn't you just check one character?
option[0] == 'Y' || option[1] == 'y'
fclose(fp);
You appeared to write on that file the exact same stuff you wrote
on stdout. Why? If a user wants the output go to a file, he can
(on most systems) simply redirect the output via the command line
(i.e.: ./a.out > Magic_square.txt).
system("PAUSE");
What does this do? My system doesn't have a command called PAUSE.
If it does what I think, it is almost straightforward to do that
in C, without needing external programs.
}

but i want to modify it and open in a text file which 25*25(maximum)
Narrow those "%4d" fields, shorten those "=====", if this isn't
enough what do you want to do? Either count characters displayed
and wrap lines with putchar('\n') when needed, or...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top