Dev - C++ - cuadro magico, quiero que se repita el cuadro al preguntar con while pero no funciona

 
Vista:

cuadro magico, quiero que se repita el cuadro al preguntar con while pero no funciona

Publicado por angelica (1 intervención) el 24/04/2020 00:08:06
#include <stdio.h>
#include <stdlib.h>
#define CM(x, N) ((x)<0 ? N+(x)%N : ((x)>=N ? (x)%N :(x)))/*/MI CONSTANTE EN ESTE EJERCICIO SERA CM
DIFINE ME AYUDA A QUE TENGA ESE VALOR EN TODO EL PROGRAMA Y NO ESTAR ECRIBIENDOLO TODAS LAS VECES*/
void main()
{

srand(time(NULL));///PARA QUE NO SE NOS VUELVA A REPETIR LO ALEATORIO
int tab[500][500];///SOLO PUSE 20 POR PONER UNA CANTIDAD MAXIMA
int i,j,n,m,grado,suma,G,su;
int band=1;
while(band==1)///CENTINELA
{
printf("\nINGRESE GRADO DEL CUADRO MAGICO:\n");///EN ESTE CASO SERA NUESTRO "N"
scanf("%d",&grado);
if(grado%2==0)
printf("ERROR, EL GRADO DEBE DE SER UN NUMERO IMPAR\n");///AVISO DE QUE DEBES INGRESAR UN NUMERO IMPAR
else{
for(i=1;i<=grado;i++)
{
for(j=1;j<=grado;j++)
{
tab[i][j]=0;///PARA QUE EMPIEZE EN 0
/* tab[i][j]=rand()%8+1;*/
}
printf("\n");
}
int j=0;
G=1;
for(i=grado/2;G<=grado*grado;)///PARA QUE ME SALGA EL UNO Y SIGUIENTE NUMERO DOS LUGARES POSTERIORES
{
if(!tab[i][j]) {
tab[i][j]= G++;
i=CM(i+1,grado);
j=CM(j-1,grado); }
else{
i=CM(i-1,grado);
j=CM(j+2,grado);

}
}

for(j=0;j<grado;j++)///MOSTRAR TABLA
{
printf("\n\n");
for(i=0;i<grado;i++){
printf("|%8d\t|",tab[i][j]);
}
}
suma=((pow(grado,2))*(pow(grado,2)+1))/(2*grado);///OPERACION DE SUMA
printf("\n\n Suma = %d",suma);
///PREGUNTA PARA EL USUARIO
printf("\n\t\tDesea volver a intentar?\n\t\t SI <- Presione 1.\n\t\t NO <- Presione 0.\n");
scanf("%d",&band);
}
}
return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 417
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

cuadro magico, quiero que se repita el cuadro al preguntar con while pero no funciona

Publicado por Thanatos (199 intervenciones) el 24/04/2020 11:56:26
Si vas a usar las funciones time() y pow(), necesitas incluir sus headers: time.h y math.h.

En la parte del programa que llena la tabla con ceros, los valores iniciales de los índices i y j deberían ser 0, en lugar de 1. Debido a eso, esta condición está provocando un bucle infinito:

1
if(!tab[i][j])
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar