logo
down
shadow

How to fill a part of a multidimensional array in C++?


How to fill a part of a multidimensional array in C++?

By : Metamorpher
Date : November 29 2020, 09:01 AM
To fix this issue You can't initialize a multidimensional array like that. If you want to do so do it like this:
code :
#include<iostream>
using namespace std;

int main()
{

    int m[4][4][3][3] = {1,0,1, //1
             1,1,1,
             1,0,1,

             1,1,1,     //2
             0,1,0,
             1,1,1,

             1,0,1,     //3
             1,1,1,
             1,0,1,

            // ...
            // ...

             1,0,1,     //16
             1,1,1,
             1,0,1};


    for(int i=0;i<4;++i)
        for(int j=0;j<4;++j) {
            for(int p=0;p<3;++p) {
                for(int q=0;q<3;++q)
                    std::cout<<m[i][j][p][q];
                std::cout<<std::endl;
            }
            std::cout<<std::endl;
        }
}


Share : facebook icon twitter icon
what's the safe way to fill multidimensional array using std::fill?

what's the safe way to fill multidimensional array using std::fill?


By : user2377327
Date : March 29 2020, 07:55 AM
This might help you Here's what I'm using: , The simple way to initialize to 0 the array is in the definition:
code :
char flags[26][80] = {};
char flags[26][80];
std::fill( &flags[0][0], &flags[0][0] + sizeof(flags) /* / sizeof(flags[0][0]) */, 0 );
memset( &flags[0][0], 0, sizeof(flags) );
const std::size_t rows = 26;
const std::size_t cols = 80;
struct Aggregate {
   char array[rows][cols];
};
class Constructor {
public:
   Constructor() {
      std::fill( &array[0][0], &array[rows][0], 0 ); // [1]
      // memset( array, 0, sizeof(array) );
   }
private:
   char array[rows][cols];
};
int main() {
   Aggregate a = {};
   Constructor b;
}
How to fill multidimensional array of specific objects in Java with fill() method?

How to fill multidimensional array of specific objects in Java with fill() method?


By : tuffyguy
Date : March 29 2020, 07:55 AM
I wish this help you Answer to your question: Yes, there is a difference (see JavaDoc).
Your first version puts one object instance into every single array element of a row. So a change to this instance is visible in every element in the same row of the array. You'll have i ClassB instances in total.
code :
for (ClassB[] classB_1Array : classB_2Array) {
    ClassB instance = new ClassB();
    Arrays.fill(classB_1Array, instance);
}
import java.util.Arrays;

public class ArrayFiller {
    public static void main(String[] args) {
        // your first version:
        Person[][] yourFirstVersion = new Person[2][2];
        for (Person[] array : yourFirstVersion) {
            Arrays.fill(array, new Person("Mike"));
        }
        System.out.println(Arrays.deepToString(yourFirstVersion));
        yourFirstVersion[0][1].setName("Paul");
        System.out.println(Arrays.deepToString(yourFirstVersion));
        System.out.println("-----");
        // equivalent: my version:
        Person[][] myVersion = new Person[2][2];
        for (Person[] array : myVersion) {
            Person person = new Person("John");
            Arrays.fill(array, person);
        }
        System.out.println(Arrays.deepToString(myVersion));
        myVersion[0][1].setName("Thomas");
        System.out.println(Arrays.deepToString(myVersion));
        System.out.println("-----");
        // your second version
        Person[][] yourSecondVersion = new Person[2][2];
        for (int i = 0; i < yourSecondVersion.length; i++) {
            for (int j = 0; j < yourSecondVersion[i].length; j++) {
                yourSecondVersion[i][j] = new Person("Max");
            }
        }
        System.out.println(Arrays.deepToString(yourSecondVersion));
        yourSecondVersion[0][1].setName("Chris");
        System.out.println(Arrays.deepToString(yourSecondVersion));
    }

    private static class Person {
        private String name;
        public Person(String name) {
            System.out.println("Constructor called for " + name);
            this.name = name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return name;
        }
    }
}
Constructor called for Mike
Constructor called for Mike
[[Mike, Mike], [Mike, Mike]]
[[Paul, Paul], [Mike, Mike]]
-----
Constructor called for John
Constructor called for John
[[John, John], [John, John]]
[[Thomas, Thomas], [John, John]]
-----
Constructor called for Max
Constructor called for Max
Constructor called for Max
Constructor called for Max
[[Max, Max], [Max, Max]]
[[Max, Chris], [Max, Max]]
Multidimensional array fill

Multidimensional array fill


By : Jerry Hsu
Date : March 29 2020, 07:55 AM
it fixes the issue Assuming you're given a starting position and you want to then fill all neighboring values up/down, left/right that contain the same value, you can do something like this:
code :
var map = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 2, 2, 2, 2, 2, 2, 0, 0],
    [0, 2, 0, 0, 0, 0, 2, 0, 0],
    [0, 2, 0, 2, 0, 0, 2, 0, 0],
    [0, 2, 0, 0, 2, 0, 2, 0, 0],
    [0, 0, 2, 0, 0, 0, 2, 0, 0],
    [0, 0, 0, 2, 2, 2, 2, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0]
];

function fill(data, x, y, newValue) {
    // get target value
    var target = data[x][y];

    function flow(x,y) {
        // bounds check what we were passed
        if (x >= 0 && x < data.length && y >= 0 && y < data[x].length) {
            if (data[x][y] === target) {
                data[x][y] = newValue;
                flow(x-1, y);    // check up
                flow(x+1, y);    // check down
                flow(x, y-1);    // check left
                flow(x, y+1);    // check right
            }
        }
    }

    flow(x,y);
}

fill(map, 2, 2, 1);
function fill(data, x, y, newValue) {
    // get target value
    var target = data[x][y];
    // maintain list of cells to process
    // put the starting cell in the queue
    var queue = [{x:x, y:y}], item;

    while (queue.length) {
        item = queue.shift();
        x = item.x;
        y = item.y;
        if (data[x][y] === target) {
            data[x][y] = newValue;
            // up
            if (x > 0) {
                queue.push({x:x-1, y:y})
            }
            // down
            if (x + 1 < data.length) {
                queue.push({x:x+1, y:y})
            }
            // left
            if (y > 0) {
                queue.push({x:x, y:y-1});
            }
            // right
            if (y + 1 < data[x].length) {
                queue.push({x:x, y:y+1});
            }
        }
    }
}
Fill multidimensional array by row

Fill multidimensional array by row


By : Stef
Date : March 29 2020, 07:55 AM
like below fixes the issue The way you've written your data, you need to fill your arrays across the deepest dimension first, and then across shallower dimensions. This is the opposite of the way R normally fills matrices/arrays.
It also needs to be said that this is slightly different from simply filling by row. To use your 3D array as an illustration of this, you've indicated it requires 4 z-slices, and the innermost "subarrays" have length 4. This means you need to fill across z-slices first, then across columns, then across rows. This is not merely filling by row, but by deepest dimension to shallowest dimension (or greatest to least, if you prefer). Admittedly, this concept is often referred to as "by row" or "row-major order", but I don't care for those terms, since they're too 2D, and they're also misleading IMO, since rows are considered to be the shallowest dimension.
code :
array(1:16,16);
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
t(array(1:16,c(8,2))); ## alternatives: matrix(1:16,2,byrow=T), aperm(array(1:16,c(8,2)))
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    1    2    3    4    5    6    7    8
## [2,]    9   10   11   12   13   14   15   16
aperm(array(1:16,c(4,2,2))); ## same as aperm(array(1:16,c(4,2,2)),3:1)
## , , 1
##
##      [,1] [,2]
## [1,]    1    5
## [2,]    9   13
##
## , , 2
##
##      [,1] [,2]
## [1,]    2    6
## [2,]   10   14
##
## , , 3
##
##      [,1] [,2]
## [1,]    3    7
## [2,]   11   15
##
## , , 4
##
##      [,1] [,2]
## [1,]    4    8
## [2,]   12   16
##
aperm(array(1:16,c(2,2,2,2))); ## same as aperm(array(1:16,c(4,2,2)),4:1)
## , , 1, 1
##
##      [,1] [,2]
## [1,]    1    5
## [2,]    9   13
##
## , , 2, 1
##
##      [,1] [,2]
## [1,]    3    7
## [2,]   11   15
##
## , , 1, 2
##
##      [,1] [,2]
## [1,]    2    6
## [2,]   10   14
##
## , , 2, 2
##
##      [,1] [,2]
## [1,]    4    8
## [2,]   12   16
##
Fill multidimensional array

Fill multidimensional array


By : Misba Inamdar
Date : March 29 2020, 07:55 AM
I wish this helpful for you I was wondering how to fill a multi-dimensional array in Excel VBA. A 1d array can be filled as follows: , You can do this:
Related Posts Related Posts :
  • How to avoid littering header files with std::
  • OpenGL GL_UNPACK_ALIGNMENT
  • Why is this program not working? (Program to copy contents of file to another)
  • Difference between DBus and other Interprocess Communications method
  • C++ SFML Game programming linkage error
  • How do GTK and Qt integrate with Linux in comparison to how they integrate with Windows and OS X?
  • Complexity analysis of loop with limited looping time
  • Boost test fails with enum classes inside namespaces
  • is this a function declaration?
  • Socket recv() one byte at a time
  • C++ Glibc Detected error. Double Free or corruption
  • OpenCV - How to write IplImage array in Mat form?
  • Qt event when anything changed on the window/screen + Screenshot
  • OpenGL Linker error, linking with uncompiled shader
  • Find 4 specific corner pixels and use them with warp perspective
  • Cin Execution Not Working (program.exe < filewithdata.txt)
  • cudaMallocManaged causes Access Violation
  • How to correctly read a value from stdin into a variable
  • Binary Search Tree Forgetting Every Node I Add
  • C++ Return Value from function not same as that value in function
  • Microsoft Visual Studio C++, OpenCV animation
  • C++ function pointer syntax. Why does (*) work but * not?
  • C++: Template class binary operator overloading - seg fault?
  • Passing buffer between two terminals (Named Pipe)
  • C++ parse sub-string to integer
  • Create stereo context for modern opengl
  • Memory leak, when using GLM
  • Get Control under cursor
  • Is this proper behavior? std::map iterator invalidation
  • 2D isometric engine - Math problems - Cube selection - diamond shape map
  • Open second window in Qt
  • Stack Overflow error with Vectors
  • Pure Virtual Friend Class
  • Object initialization syntax in C++
  • Variable undefined error
  • C++ tolower/toupper char pointer
  • Overhead with std::function
  • Is there a way to make a loop that get user input but doesn't stop if the user doesn't input anything?
  • OpenCV in cmake-based project: checking for contrib modules
  • shared_ptr vs unique_ptr uses in classes and children
  • MFC dialog Border padding changed after switching from VS2010 to 2012 or later
  • String rotator in C++ (bitwise rotation)
  • Custom sort vector of pair based on their values
  • Pointer to const overloaded member function
  • how to avoid this for-loop mess in c++?
  • Can the state of a standard C++ iostream manipulator be polled?
  • How to make sure a data type is as large as it needs to be in C++
  • Why is my first ofstream output in my else block missing the fill character?
  • Returning static/ normal arrays in recursion/another function
  • c++, why use const std::string & parameterName?
  • expression did not evaluate to a constant in C++ VS
  • Owner object that takes pre-created values ? Wrong design?
  • Cannot use Macro in a C++ constructor?
  • vector is loosing mat4 information
  • Invalid declarator before with map of struct?
  • std::initializer_list to return member variables returns incorrect values
  • Unable to use Boost + Qt in Mac
  • Passing template function and overload as function argument
  • non standard extension warning when searching in a vector of unique_ptrs
  • Qt Windows x64 build succeeds in IDE but not on the command line
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co