|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Hello! I'm new to programming, and currently having trouble with a C program. In essence, I am suppose to make a magic square program, and in order to do so I thought that I would have to create a 3x3 matrix, then randomize the numbers in each slot, without any overlapping, and keep looping that until every row, column and diagnals equals one another. I'm sure there are other more efficient ways, but this is all I could think of. So far I have
Code:
#include <stdio.h>
#include<stdlib.h>
#define N 3
int main(void)
{
srand((unsigned)time(NULL));
int b[N][N], int i, int j;
for( ){
/* need to randomize numbers 1-9 here in matrix but I don't know how*/
if(b[0][0]+b[1][0]+b[2][0]=b[0][1]+b[1][1]+b[2][1]=b[0][2]+b[1][2]+b[2][2]=b[0][0]+b[0][1]+b[0][2]=b[1][0]+b[1][1]+b[1][2]=b[2][0]+b[2][1]+b[2][2]=b[0][0]+b[1][1]+b[2][2]=b[2][0]+b[1][1]+b[0][2])
printf("/* how do you print the current matrix?*/");
break;
else continue;}
}
% ./a.out 4 3 8 9 5 1 2 7 6 Please help me! |
| Sponsored Links |
|
|
|
|||||
|
kkelly I want to warn you that this method may not work. there are a few combinations of randomly set numbers that will make your puzzle impossible to solve. if this is a problem you may want to investigate what those are or think of another way to randomize your numbers.
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Floating Point random number generator | Paradine | PHP Tutorials | 0 | 08-26-2007 02:09 PM |
| Python 2D array question | annannienann | Python | 3 | 04-23-2007 04:36 PM |
| Number Cloning | Ronin | Java Help | 3 | 12-21-2006 07:23 AM |
| I need to generate a random number in PHP | dirkfirst | PHP Forum | 7 | 07-05-2006 09:58 PM |
| Xav | ........ | 1276.19 |
| MeTh0Dz|Reb0rn | ........ | 1048.58 |
| marwex89 | ........ | 869.98 |
| morefood2001 | ........ | 868.04 |
| John | ........ | 865.15 |
| WingedPanther | ........ | 761.06 |
| Brandon W | ........ | 684.87 |
| chili5 | ........ | 294.12 |
| Steve.L | ........ | 216.18 |
| dargueta | ........ | 192.86 |
Goal: 100,000 Posts
Complete: 81%