When defining a matrix of ints (as in an array of an array), is there a way of predefining every element as zero, or do I have to do it manually using 2 for loops?
Also the size of the matrix will be different everytime i declare it.
Cheers
When defining a matrix of ints (as in an array of an array), is there a way of predefining every element as zero, or do I have to do it manually using 2 for loops?
Also the size of the matrix will be different everytime i declare it.
Cheers
Last edited by Wiretron; 12-19-2006 at 09:58 AM.
Though some fussy compilers complain at the lack of initialisers, C states that anything which isn't specified is set to zero (of the correct type)Code:int mat[3][3] = { { 0 } };
So chars would be '\0', floats and doubles would be 0.0f and 0.0 respectively and pointers would be NULL.
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.
Also is there a maximum number of elements an int matrix can hold, I declared an int matrix of size [1000][1000] and the program crashes.
Well 1000x1000x4 (typical int size) is 4MB.
Which is a hell of a lot to go on the stack at one go.
The default for VC is either 0.5 or 1MB IIRC.
You can increase this if you want.
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.
And of course it is always possible to start using dynamic memory, where there is no such limitation
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Does that mean using data structures such as Linked lists?
no, they mean using malloc() to allocate memory on the heap (which is larger and it tells you if the memory allocation fails)Originally Posted by Wiretron
And you don't need to allocate all matrix at once, you can allocate one row at once and still the get original way to access matrix members... So your matrix can be bigger than biggest non-segmentable memory chunk supported by the current system
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Ok thanks, I'm going to holding a matrix with possibly up to 1,000,000^2 elements, what are the best methods to holding the matrix, speed of execution, (as in how quickly I can store it and access elements) is a key issue too.
You'll need a lot of memory and a rather big harddisk for that. something like 4 tera bytes.Originally Posted by Wiretron
K.
Hmm lol, maybe not that elements then, but the amount of elements will be substantial, what sort of data structure would be ideal?
Is your matrix sparse? (ie. many entries are 0). There are methods for efficient data structures and computations with sparse matrices, just google it (oh the sweet irony, as google relies on *huge* sparse matrix computations to make accomodate your searching needs)
Yeah it's sparse.
One more tip:
U can also define the array as static or global directly to initialize all elements to 0.
ya, but ur problem do needs a dynamic array.
One idea would be to use a tree or summat, eg. divide the matrix into four quadrants, and so on. Access would be a little slower (log n) but on the upside you use only a couple times as much space as is needed.
Or a hash table. More work, but could give even better space usage, down to < 1.25x the number of actual elements used.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}