How to resolve the algorithm Magic squares of doubly even order step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Magic squares of doubly even order step by step in the C programming language

Table of Contents

Problem Statement

A magic square is an   N×N  square matrix whose numbers consist of consecutive numbers arranged so that the sum of each row and column,   and   both diagonals are equal to the same sum   (which is called the magic number or magic constant).
A magic square of doubly even order has a size that is a multiple of four   (e.g.     4, 8, 12). This means that the subsquares also have an even size, which plays a role in the construction.

Create a magic square of   8 × 8.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Magic squares of doubly even order step by step in the C programming language

This C program generates and prints a doubly even magic square of a given order.

Input:

  • The program takes an integer n as input from the command line, representing the order of the magic square.

Algorithm:

  • The program uses a bit pattern bits = 38505 to determine the placement of numbers in the magic square.
  • It calculates the size of the square as size = n * n.
  • The mult variable is set to n / 4, which is used to determine the bit position in the bit pattern for each element in the square.
  • The program creates a 2D array result of size n x n to store the elements of the magic square.
  • It iterates through each row and column, calculating the bit position bitPos based on the current row and column indices.
  • Using the bit pattern, it checks if (bits & (1 << bitPos)) is non-zero. If it is, the element is set to i + 1, where i is the current index. Otherwise, it is set to size - i.
  • This bit pattern ensures that the elements in each row, column, and the two main diagonals sum up to the same magic constant.

Output:

  • The program prints the generated magic square on the console.
  • It displays the order of the magic square and the magic constant, which is (rows * rows + 1) * rows / 2.
  • The magic square is printed with a consistent width for each element, determined based on the number of digits in the square's size.

Additional Functions:

  • numDigits(int n): Calculates the number of digits in a given integer n.
  • printMagicSquare(int** square, int rows): Prints the magic square in a user-friendly format.

Usage:

  • To use this program, run it from the command line and provide an integer argument specifying the order of the magic square.
  • For example, to generate a magic square of order 4, run:
    ./doublyEvenMagicSquare 4
    

Source code in the c programming language

#include<stdlib.h>
#include<ctype.h>
#include<stdio.h>

int** doublyEvenMagicSquare(int n) {
	if (n < 4 || n % 4 != 0)
		return NULL;

	int bits = 38505;
	int size = n * n;
	int mult = n / 4,i,r,c,bitPos;

	int** result = (int**)malloc(n*sizeof(int*));
	
	for(i=0;i<n;i++)
		result[i] = (int*)malloc(n*sizeof(int));

	for (r = 0, i = 0; r < n; r++) {
		for (c = 0; c < n; c++, i++) {
			bitPos = c / mult + (r / mult) * 4;
			result[r][c] = (bits & (1 << bitPos)) != 0 ? i + 1 : size - i;
		}
	}
	return result;
}

int numDigits(int n){
	int count = 1;
	
	while(n>=10){
		n /= 10;
		count++;
	}
	
	return count;
}

void printMagicSquare(int** square,int rows){
	int i,j,baseWidth = numDigits(rows*rows) + 3;
	
	printf("Doubly Magic Square of Order : %d and Magic Constant : %d\n\n",rows,(rows * rows + 1) * rows / 2);
	
	for(i=0;i<rows;i++){
		for(j=0;j<rows;j++){
			printf("%*s%d",baseWidth - numDigits(square[i][j]),"",square[i][j]);
		}
		printf("\n");
	}
}

int main(int argC,char* argV[])
{
	int n;
	
	if(argC!=2||isdigit(argV[1][0])==0)
		printf("Usage : %s <integer specifying rows in magic square>",argV[0]);
	else{
		n = atoi(argV[1]);
		printMagicSquare(doublyEvenMagicSquare(n),n);
	}
	return 0;
}


  

You may also check:How to resolve the algorithm Symmetric difference step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Brat programming language
You may also check:How to resolve the algorithm Sleep step by step in the Zoomscript programming language
You may also check:How to resolve the algorithm Generator/Exponential step by step in the Icon and Unicon programming language