answersLogoWhite

0


Best Answer

#include<stdio.h>

unsigned sum_row (unsigned* sq, const unsigned width, const unsigned row) {

unsigned sum, col;

sum = 0;

for (col=0; col<width; ++col)

sum += sq[row*width+col];

return sum;

}

unsigned sum_col (unsigned* sq, const unsigned width, const unsigned col) {

unsigned sum, row;

sum = 0;

for (row=0; row<width; ++row)

sum += sq[row*width+col];

return sum;

}

unsigned sum_diag (unsigned* sq, const unsigned width) {

unsigned sum, row, col;

sum = 0;

for (row=0, col=0; row<width; ++row, ++col)

sum += sq[row*width+col];

return sum;

}

unsigned sum_anti (unsigned* sq, const unsigned width) {

unsigned sum, row, col;

sum = 0;

for (row=0, col=width-1; row<width; ++row, --col)

sum += sq[row*width+col];

return sum;

}

bool is_magic (unsigned* sq, const unsigned width) {

unsigned magic, row, col;

magic = sum_row (sq, width, 0);

for (row=1; row<width; ++row)

if (magic!=sum_row(sq, width, row))

return false;

for (col=0; col<width; ++col)

if (magic!=sum_col(sq, width, col))

return false;

if (magic!=sum_diag(sq, width))

return false;

if (magic!=sum_anti(sq, width))

return false;

return true;

}

int main () {

const unsigned width = 3;

unsigned a[width][width] {{2,7,6},{9,5,1},{4,3,8}};

unsigned row, col;

printf ("Square:\n\n");

for (row=0; row<width; ++row) {

for (col=0; col<width; ++col) {

printf ("%d ", a[row][col]);

}

printf ("\n");

}

printf ("\n");

if (is_magic((unsigned*)&a, width))

printf ("The square is magic with a magic constant of %d\n", sum_row((unsigned*)&a, 3,0));

else

printf ("The square is not magic\n");

return 0;

}

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program to find magic numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write Square program using vb?

write a vb program to find the magic square


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


How to write a C program to find largest 2 numbers using pointers?

program to find maximum of two numbers using pointers


How do you write a VBnet program to find the prime numbers between 100 to 200?

VBnet program to find the prime numbers between 100 to 200?


Write a Shell program to find the smallest number from a set of numbers?

k


Write a program to find the product of two numbers using Halving and Doubling method?

i need this answer


Write a C program to find the square of two numbers?

Please visit http://talentsealed.blogspot.com/2009/10/to-find-sqaure-of-numbers-using-c.htmlfor the answer.


Write a C program to find the sum of all prime numbers?

Since there is an infinite set of prime numbers the answer would be infinity.


Write a java program to find sum of even and odd numbers in given array?

for(int i = 1; i &lt; 100; i+=2) { System.out.println(i); }


Write a Program in c to find palindrome numbers between any two numbers?

please somebody should help me answer this question..I have a project on it even! It seems almost impossible at the moment.


Write a program to print first 100 alternative prime numbers?

This would require some computer knowledge. It can make it easier to find out the prime numbers without figuring it out in your head.


How do you find sum of 100 numbers using fortran programme?

The following is for F95 and later (due to the use of intrinsic SUM ): My assumptions: -Your numbers are integers -Your numbers are stored in an array -The numbers you are describing are 0-100 program findSum !I assumed integer, replace this with your data type integer, dimension(100) :: numbers integer :: sumOfNumbers !We populate an array with our numbers !Replace this with your numbers do i=1,(size(numbers)+1) numbers = i end do !We find the sum of those numbers sumOfNumbers = sum(numbers) !We write out the sum to prompt write(*,*) 'Sum is: ', sumOfNumbers end program findSum