What's new

C & C++ Please paki help po, ano po ung explanation about sa code na to

RheyViews

Forum Veteran
Elite
Joined
Oct 8, 2016
Posts
2,025
Solutions
2
Reaction
871
Points
678
Age
22
sa pag gawa po ito ng magic square using c language, diko talaga alam pano kaya naghanap ako sa google, nakakita ako ng code tas kailangan ko ng explaination , pwede paki edit po tas lagyan ng comment, salamat po sa sasagot god bless po
C:
#include <stdio.h>
 
void magicsq(int, int [][10]);
 
int main( )
{
    int size;
    int a[10][10];
 
    printf("Enter the size: ");
    scanf("%d", &size);
    if (size % 2 == 0)
    {
        printf("Magic square works for an odd numbered size\n");
    }
    else
    {
        magicsq(size, a);
    }
    return 0;
}
 
void magicsq(int size, int a[][10])
{
    int sqr = size * size;
    int i = 0, j = size / 2, k;
 
    for (k = 1; k <= sqr; ++k)
    {
        a[i][j] = k;
        i--;
        j++;
 
        if (k % size == 0)
        {
            i += 2;
            --j;
        }
        else
        {
            if (j == size)
            {
                j -= size;
            }
            else if (i < 0)
            {
                i += size;
            }
        }
    }
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("%d  ", a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}
 

Similar threads

Back
Top