Thread: Question to do with matrices

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Question to do with matrices

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    int mat[3][3] = { { 0 } };
    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)
    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.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Does that mean using data structures such as Linked lists?

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    Quote Originally Posted by Wiretron
    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)

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    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.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Wiretron
    Ok thanks, I'm going to holding a matrix with possibly up to 1,000,000^2 elements,
    You'll need a lot of memory and a rather big harddisk for that. something like 4 tera bytes.
    K.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Hmm lol, maybe not that elements then, but the amount of elements will be substantial, what sort of data structure would be ideal?

  12. #12
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    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)

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Yeah it's sparse.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Posts
    1
    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.

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  3. Multiplying matrices
    By Star Lancer in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 06:07 AM
  4. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM