By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i] = 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[i] += magic[i][j];
for (j=0;j<n;j++)
for (i=0;i<n;i++)
col_sum[j] += magic[i][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)

Aug 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 8 2 , 9 09 , jyc...@gmail.com wrote:
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[i] = 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[i] += magic[i][j];
for (j=0;j<n;j++)
for (i=0;i<n;i++)
col_sum[j] += magic[i][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

Aug 2 '07 #2

P: n/a
jy****@gmail.com wrote:
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).
Aug 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.