Thread: Duplicating arrays

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    Duplicating arrays

    Hi!!

    This is my first post on the forum, because I've been having trouble trying to do this. How can I from an array that's [i][j] duplicate it's column and line values to a [2*i][2*j]?
    For example : [2,4];[5,6] to [2,2,4,4];[2,2,4,4];[5,5,6,6];[5,5,6,6]
    And how can I do the inverse, remove alternatively column and line.
    For example: [1,2,3,4];[5,6,7,8];[9,10,11,12];[13,14,15,16] to [1,3];[9,11]


    Thanks!

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    16
    Just do newArray[i][j] = oldArray[i/2][j/2] for each i, j.

    ___________
    Visit my project: Online symbolic derivative calculator

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by TomasRiker View Post
    Just do newArray[i][j] = oldArray[i/2][j/2] for each i, j.
    That covers the second case.
    For the first case you want to assign each value to the four locations:
    [i*2][j*2]
    [i*2+1][j*2]
    [i*2][j*2+1]
    [i*2+1][j*2+1]
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    2
    Quote Originally Posted by TomasRiker View Post
    Just do newArray[i][j] = oldArray[i/2][j/2] for each i, j.
    First case is working, as for the second case tried and it doesn't work and I can't understand why it puts the new array all with 1's plus it alters the first row of the original one.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by interneft View Post
    First case is working, as for the second case tried and it doesn't work and I can't understand why it puts the new array all with 1's plus it alters the first row of the original one.
    Actually, my mistake, the other guy's suggestion was also for the first case, and is probably the simpler way of doing it.
    For the second case you can just turn the /2 into *2.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. duplicating and reversing a string?
    By Serenity in forum C Programming
    Replies: 13
    Last Post: 10-26-2010, 01:12 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. duplicating the desktop ContextMenu
    By Mastadex in forum Windows Programming
    Replies: 1
    Last Post: 07-07-2007, 02:10 AM
  4. Duplicating Visual Studio 2005's docking system
    By zort15 in forum C# Programming
    Replies: 1
    Last Post: 05-09-2007, 03:04 PM
  5. Duplicating specific binary data in array
    By chris.lloyd in forum C Programming
    Replies: 1
    Last Post: 10-21-2006, 09:54 PM