Thread: how do i insert a row into a matrix?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    how do i insert a row into a matrix?

    I start with a matrix which has 10 columns and just one row, and I would like to add a seccond row below that one if the elements in the first row satisfy a certain property, add a third if the elements of the seccond one satisty a certain propery and so on.

    Of course, I don't want to lose the elements in any of the rows after inserting a new one.

    How do I do this?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You could declare a new matrix with sufficient space for the new row, copy the first row, assign your second row, and then copy your 3rd+ rows over. If you're not already aware of memcpy, it's a good way to copy contiguous sections of memory. (I'm assuming of course you're using arrays to implement the matrix).

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Have you thought about making a large number of rows in your array. Then just adding data to it, as before. Your output (print or to file), would still show only the rows that are filled:

    Code:
    if(strlen(name)) {
      //print this row
      //this is a filled row
    }
    //or
    
    for(i=0;i<FilledRowsVariable; i++)
      //print this row
    You may want to keep an int variable with the number of rows that are filled.

    It's not all that trivial to keep mallocing or reallocating memory. Just free'ing all the memory you've requested, is a chore.
    Last edited by Adak; 08-28-2010 at 10:30 AM.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    I'm sorry but I can't build a matrix with a large enough number of rows, because the rows should grow dynamically

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  6. #6
    Registered User
    Join Date
    Nov 2001
    Location
    Mexico City
    Posts
    33
    Switch to APL (A Programming Language).
    Dyalog.com

    Gustavo.
    If you want to be happy one hour: take a nap
    if you want to be happy one day: go fishing
    If you want to be happy a year: inherit a fortune
    if you want to be happy for a life time: HELP SOMEBODY
    chinisse say.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hey Gustaff, how come you wound up working in APL instead of the more common Python or Ruby?

    What's do you like about APL? Any strong points you've seen?

    Budala, did you check out the realloc link that Bayint posted, yet? Any problems so far?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  2. background ascii color
    By dvsumosize in forum C Programming
    Replies: 0
    Last Post: 11-09-2009, 11:19 AM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  4. Animation not working....
    By aquinn in forum C Programming
    Replies: 7
    Last Post: 02-19-2005, 05:37 AM
  5. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM

Tags for this Thread