Magic Box

This is a discussion on Magic Box within the C Programming forums, part of the General Programming Boards category; hi i am trying to create the magic box of 3*3 using the random function here is my code..... Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    21

    Magic Box

    hi

    i am trying to create the magic box of 3*3 using the random function

    here is my code.....
    Code:
    # include<stdio.h>
    # include<conio.h>
    main()
    {
      int i,a[9];
      for(i=1;i<=9;i++)
      {
      a[i]=rand();
      }
      while (true)
      {
    	if((a[0]+a[1]+a[2]==15)&&(a[3]+a[4]+a[5]==15)&&(a[6]+a[7]+a[8]==15)&&(a[0]+a[4]+a[8]==15))
    	{
    		for(i=0;i<9;i++)
    		{
    		printf("%d ",a[i]);
    		printf("\n");
    		}
    	}
    	else
    	continue;
    
      }
    }
    how to use the random function of c to put the values in the array?? and when i execute it says unidentified true symbol.....wat should i do???

    in case the condition is not satisfied then how should i continue so that it generates random again????
    thanks
    Last edited by Salem; 08-18-2007 at 11:43 AM. Reason: Code tags go AROUND the code, don't just put them anywhere to make the popup go away

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,772
    Please use code tags and properly indent your code. A few problems with your code are immediately apparent:

    1. Generally, main() returns an int, but your code relies on default int and yet does not return an int.

    2. You loop from i=1 to i=9, and then use i as the index for an array of 9 elements. This leads to an array index out of bounds error.

    3. rand() is from <stdlib.h>, but that header is not included. Also, the pseudorandom number generator is not seeded with srand().

    4. true is a C++ keyword. You either define it yourself, or you use 1 (or some other non-zero, but 1 is probably best), or use "for (;" instead of "while (1)" for your deliberate infinite loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    21
    thanks a lot for the reply....


    sorry that i forgot the indents.......is this logic correct????

    any other ideas for the magic box????

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well if you used
    int a[3][3];

    it would be a lot simpler to write a function which summed each row and column of the array to check for whether it met the conditions for a magic square.
    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
    Aug 2007
    Posts
    21
    Quote Originally Posted by Salem View Post
    Well if you used
    int a[3][3];

    it would be a lot simpler to write a function which summed each row and column of the array to check for whether it met the conditions for a magic square.
    i actually didnt get wat u said????

    could u tell me a bit???

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,772
    Salem's point is that since you are trying to represent a square matrix, a two dimensional array would be more suitable. Read the tutorial on arrays for more information.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 04:08 AM
  2. Replies: 9
    Last Post: 02-13-2008, 01:59 PM
  3. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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