4x4 grid in C

This is a discussion on 4x4 grid in C within the C Programming forums, part of the General Programming Boards category; Dear all, i am just starting to learn about C.. anyone can help with my question will be appreciated. How ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Question 4x4 grid in C

    Dear all,
    i am just starting to learn about C.. anyone can help with my question will be appreciated.

    How to build a 4x4 grid with the following condition :
    sum of each row equal 34;
    sum of each column equal 34;
    no number can be repeated;

    i dont like to hard coded it... i prefer to use loop to have the program work...

    Please help.

    Thanks in advance,

    Roberto.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    At least show some effort, nobody's gonna write the whole thing out for you
    how would you do it if you were to solve this problem manually?

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Well there problem has a name and it is called magic squares. You are asked to write a program that will produce a magic square array of 4. A magic square of n has in each row - col - diag sum(n) = (n^3 + n) / 2 , in our case n = 4 which means sum(4) = 34.
    The answer after a quick search is that for numbers like 4, 6, 8 e.g, for even numbers there is no specific algorithm that produces a magic square. There is a simple technique though that can solve your problem. Just start diging somewhere and you will find it, give us a try of your code and we will help you.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    4 X 4 grid

    Thank you for the explanation..
    I am able to generate the number using rand(), my question is that what to do so that it can check the random number so that the same number is not appear twice?

    Thanks



    Quote Originally Posted by Bokarinho View Post
    Well there problem has a name and it is called magic squares. You are asked to write a program that will produce a magic square array of 4. A magic square of n has in each row - col - diag sum(n) = (n^3 + n) / 2 , in our case n = 4 which means sum(4) = 34.
    The answer after a quick search is that for numbers like 4, 6, 8 e.g, for even numbers there is no specific algorithm that produces a magic square. There is a simple technique though that can solve your problem. Just start diging somewhere and you will find it, give us a try of your code and we will help you.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    do
    {
        int go=0;
        num = rand();
        if (num == prev_num)
            go = 1;
    
    } while (go);

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    re : 4 x 4 grid in C

    thanks for the explanation... my question is where to put the code?

    Here is my code :
    Code:
    #include <stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    
    #define SIZE 4
    #define TOTALVALUE 34
    
    int random(void);
    int checkArray(int array[SIZE][SIZE]);
    void generateArray(int array[SIZE][SIZE]);
    const int MAX = 17;
    
    int main (void)
    {
    	int array[SIZE][SIZE];
    	int i;
    	int j;
    	
    	  
    	for(i=0; i < SIZE; i++)
    	{
    		for(j = 0; j < SIZE; j++)
    		{
    			array[i][j] = random();
    			
    			//int number;
    			//number = rand();
    			//printf("%i\t",(number % (MAX-1)) +2);
    			printf("%i\t", array[i][j]); 	
    		}printf("\n");
    	}
    	fflush(stdin);
    	   
    return 0;
    }
    
    int random(void)
    {
        /*int i;
    	int a;
    	
        for (i = 0; i < 4; i++)
    	{
    		for(a = 0; a < 4; a++)
    		{*/
    	 
           		int number;
    
           		number = rand() % (MAX-1) +2;
           			   
        return number;
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More Windows Trouble
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 04-12-2005, 10:02 AM
  2. Cleaning up a Grid of Memory
    By Epo in forum C++ Programming
    Replies: 9
    Last Post: 02-25-2005, 09:23 AM
  3. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 04:38 PM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. I need help making a grid!
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 09-02-2002, 05:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21