Jump to content

Matrix in C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Maze

Maze

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
This is my code:


#include <stdio.h>

#include <stdlib.h>

#include <time.h>


int MyArray[3][3];

int RandomNum;

void checkit(void);

int main()


{

    srand(time(NULL));

	int i,j;

	

    checkit();

    

	for (i=0; i<3; i++)

	{

		for (j=0; j<3; j++)

		{


		printf("The Matrix is:%d\n ",MyArray[i][j]);

	}

	}

	

	system("pause");

	return 0;

}


int i,j;


	void checkit(void)

		{

			for (i=0; i<3; i++)

			{

				for (j=0; j<3; j++)

				{

                    RandomNum = rand() % 9 + 1;

					if (MyArray[i][j]==RandomNum)

						continue;

					MyArray[i][j]=RandomNum;	

                    }

                    }

                    return;

		}



When writing Random numbers in Matrix, how i can control that every number of the matrix become different. For example there must not be two 9 in my matrix. But i could not do it with above code. How to do it?

Thanks for help

#2
Maze

Maze

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Any answer?

#3
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
Hi Maze. :)

As I understand it you want randomly assigned numbers in your matrix but no duplicates. To do this you could check if the number is already in your matrix every time you pick a new random number. The problem with this is it could potentially continue for a long time, although in practise it works well. If you go down this root, use a balancing binary-tree to store previously-used random numbers.

The other option I can see is saying: "I've got 9 squares that need filling; I can fill them with the numbers 1 to 9!" This method is better in the sense that it can't continue for a long time but will always fill the matrix with the numbers 1 to 9! This algorithm is outlined in Wikipedia as the Fisher-Yates shuffle (also known as the Knuth shuffle after Donald E Knuth).

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Instead of doing a simple random number, check if the number is present. If it is, increment and check again, otherwise, add it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts

WingedPanther said:

Instead of doing a simple random number, check if the number is present. If it is, increment and check again, otherwise, add it.

Although I haven't proved it, I expect that will tend to result in clumps of numbers being present which wouldn't be acceptable if you want truly random numbers. Granted we're dealing with pseudo-randomness anyway, but the grouping would probably not even seem fully random any more.

With that said, the algorithm is quite acceptable for many purposes. Just don't use it for cryptography. :laugh:

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The best way to do it, in my opinion, is to have something like a linked list of valid numbers. When you pull one (at random), you remove it from the list and decrement the range of numbers you're returning.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts

WingedPanther said:

The best way to do it, in my opinion, is to have something like a linked list of valid numbers. When you pull one (at random), you remove it from the list and decrement the range of numbers you're returning.

That's an optimally efficient implementation of the Fisher-Yates shuffle I was talking about. :)

#8
Maze

Maze

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Thanks you all for nice quotes. But why my code is not doing that job. Where is my error point?

#9
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
Because your algorithm doesn't stop duplicates at all. All you do is assign a random number to each element of the matrix. Many of these number may in fact be the same! (In fact there's a 0.999063343 chance that the array will contain duplicates!)

Firstly, here's the code cleaned up a bit:


#include <stdio.h>

#include <stdlib.h>

#include <time.h>


int MyArray[3][3];

void checkit(void);


int main(void)

{

	srand(time(NULL));

	

	checkit();

	

	int i, j;

	for (i=0; i<3; i++)

	{

		for (j=0; j<3; j++)

		{

			printf("The Matrix is: %d\n", MyArray[i][j]);

		}

	}

	

	return 0;

}


void checkit(void)

{

	int i, j, RandomNum;

	for (i=0; i<3; i++)

	{

		for (j=0; j<3; j++)

		{

			RandomNum = rand() % 9 + 1;

			MyArray[i][j]=RandomNum;	

		}

	}

	

}


Try not to define the variable i and j outside a function since they don't need to be. It makes code harder to understand for most people. Writing them inside each function they use has a myriad of benefits which will become apparent as you try more things (thread-safety, modularization, even performance sometimes).

With that said, let's look at an algorithm to give each matrix element a random, but distinct, number. Consider instead if we arbitrarily filled the entire array with the numbers 1 to 9.

Quote

1 2 3
4 5 6
7 8 9

Not very random, eh? But now we can simply loop through all elements of the array and swap it with any other random element. For example, on the first element we might swap it with sixth element:

Quote

6 2 3
4 5 1
7 8 9

And so on for each element.

Going through each element in this way should ensure quite good pseudo randomness. Here's a modified version of your program to demonstrate:


#include <stdio.h>

#include <stdlib.h>

#include <time.h>


int MyArray[3][3];

void checkit(void);


int main(void)

{

	srand(time(NULL));

	

	checkit();

	

	int i, j;

	for (i=0; i<3; i++)

	{

		for (j=0; j<3; j++)

		{

			printf("The Matrix is: %d\n", MyArray[i][j]);

		}

	}

	

	return 0;

}


void checkit(void)

{

	int i, j, RandomNum;

	

	//Fill each element with the numbers 1 to 9 in order

	for (i=0; i<3; i++)

	{

		for (j=0; j<3; j++)

		{

			MyArray[i][j] = 3*i+j+1;	

		}

	}

	

	int d_i, d_j, temp;

	

	//Swap each element with a random other element

	for (i=0; i<3; i++)

	{

		for (j=0; j<3; j++)

		{

			d_i = rand()%3; //MyArray[d_i][d_j] is the element we will swap with

			d_j = rand()%3;

			

			temp = MyArray[i][j];

			MyArray[i][j] = MyArray[d_i][d_j];

			MyArray[d_i][d_j] = temp;

		}

	}

	

}


Consider trying some of the other algorithms we discussed. They are good practice.

#10
Maze

Maze

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
PythonPower thanks for help. I will try these code when go home in my pc with visual studio 2008:)

So +1 reputation :)