Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-12-2007, 08:33 AM
siren siren is offline
Newbie
 
Join Date: Apr 2007
Posts: 10
Credits: 0
Rep Power: 0
siren is on a distinguished road
Default HELP! 3x3 array random number 1-9 no repeat

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;}
}
and simply want the result to be something like

% ./a.out
4 3 8
9 5 1
2 7 6

Please help me!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 09-13-2007, 06:23 AM
kkelly's Avatar   
kkelly kkelly is offline
Learning Programmer
 
Join Date: Sep 2007
Posts: 50
Credits: 0
Rep Power: 4
kkelly is on a distinguished road
Default

That would require a nested for loop.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-13-2007, 07:23 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 54
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

It's right what kkelly is saying. You need a nested loop, and it's fairly easy to make, and use. You can use it to both print you matrix, and put in random values.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int GetRandom(int Max)
{
	/* Returns a value between 0 and Max-1 */
	return (rand() % Max);
}

int main()
{
	int X, Y;
	int MaximumValue = 10;
	int X_Axis = 3, Y_Axis = 3;
	int Matrix[X_Axis][Y_Axis];
	
	/* Use the time to get random values */
	srand((unsigned int)time(NULL));
	
	/* Fill the array with random values */
	for(X = 0; X < X_Axis; X++)
		for(Y = 0; Y < Y_Axis; Y++)
			Matrix[X][Y] = GetRandom(MaximumValue);
	
	/* Print all the values to the user */
	for(X = 0; X < X_Axis; X++)
	{
		for(Y = 0; Y < Y_Axis; Y++)
			printf("%d ", Matrix[X][Y]);
		putchar(10);
	}
	
	return 0;
}
__________________

To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
-
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.
-
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts.

I'm always up for a chat, so feel free to contact me...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-25-2007, 02:03 PM
felixme86's Avatar   
felixme86 felixme86 is offline
Newbie
 
Join Date: Sep 2007
Location: Seattle WA
Posts: 11
Credits: 0
Rep Power: 0
felixme86 is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 11:11 PM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 81%

Ads