answersLogoWhite

0


Best Answer

type_of_data array_name[number_of_elements_ in_array];

For instance,

int myArray[10];

It creates array of type int, and array consists of 10 elements.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

If you understand the concept of one-dimensional arrays, then there's really nothing mysterious about a multi-dimensional array. A two-dimensional array is nothing more than a one-dimensional array of one-dimensional arrays while a three-dimensional array is nothing more than a one-dimensional array of two-dimensional arrays. The concept can be extended to any number of dimensions but ultimately boils down to a one-dimensional array within a one-dimensional array.

A two-dimensional array is often thought of as being a table of rows and columns, where each row is a one-dimensional array.

A three-dimensional array can be thought of as being a cuboid, where each "layer" of the cuboid is a table.

Although it is difficult to imagine four-dimensional objects within a three-dimensional world, it becomes much easier if we think of four-dimensional arrays as being a one-dimensional array of cuboids (e.g., a single row of cuboids). Or we can think of them as being a table of tables, or indeed a cuboid of one-dimensional arrays.

This concept can be extended to any number of dimensions, such that a five-dimensional array can be thought of as being a table of cuboids or a cuboid of tables, or a one-dimensional array of four-dimensional arrays, or a four-dimensional array of one-dimensional arrays. How we imagine these structures basically boils down to what those structures actually represent and what makes the most sense to us, but every dimension (other than the last) can be reduced to a one-dimensional array of elements, where every element is itself a one-dimensional array. The final dimension's elements are always the actual values that make up the array.

When we think of an array we typically think of a contiguous block of memory divided into one or more elements of equal size. But with multi-dimensional arrays we are not restricted to contiguous memory. That is, each one-dimensional array within the multi-dimensional array must itself be contiguous, but it needn't be contiguous with any other one-dimensional array in the multi-dimensional array. In the case of static multi-dimensional arrays the memory must be contiguous throughout, but multi-dimensional arrays can be both static and dynamic at the same time, or they can be entirely dynamic.

For instance, a static array of variable-length strings is a multi-dimensional array (a two-dimensional array) that can be allocated as a static one-dimensional array of char pointers, where each char pointer refers to a dynamically-allocated one-dimensional array of type char. We can still treat the array as if it were a two-dimensional array in order to access the individual characters of each row, but the rows needn't be the same length and needn't be contiguous. Only the pointers themselves need to be allocated contiguously, but they can physically point anywhere. Indeed, any two pointers could point to the exact same string if necessary.

Mixing static and dynamic allocations in order to create multi-dimensional arrays is not unusual, but note that you cannot have a dynamic array of static arrays. Static arrays must be allocated at compile time thus you cannot have a variable number of static allocations. However, you can have a static multi-dimensional arrays of pointers, where each pointer can subsequently be dynamically allocated at runtime to refer to either a one-dimensional or a multi-dimensional array.

So far we have discussed arrays as a general concept, but we haven't looked at how we go about instantiating arrays in C++. This is because there are several ways to do so.

C++ inherits from C and therefore supports C-style arrays. It is important to note, however, that a C-style array is not the same as an array in C. For instance, in C, the following would be perfectly acceptable:

void foo (const size_t size)

{

int x[size];

// ...operate upon x.

}

C++ would not allow this because x is implicitly static, but size is not a constant value. It is obviously a constant within the context of the foo() function (it is declared constant), but it is not constant in the global sense. More specifically, it is a constant type rather than a constant value. In C++, static arrays must always be instantiated with a constant value or a literal constant -- never a constant type. Constant types can only be used to initialise dynamic arrays, thus the above function would need to be re-written for C++ as follows:

void foo (const size_t size)

{

int* x = new int[size];

// ...operate upon x.

delete [] x;

}

Note that we must explicitly delete the allocation when we are finished with it lest we violate the basic guarantee (thou shalt not leak resources). Although it could easily be argued that the C version is much simpler, C++ regards this as an inconsistency within the C language and strictly forbids it.

To allocate a static array in C++ we must supply a constant value or literal constant for the dimension, thus the following are all acceptable:

const size_t size = 42;

int x[100];

int y[size];

int z[100+size];

In these examples size is a constant value (42) as well as a constant type, while 100 is a literal constant.

For multi-dimensional arrays, we simply tag on as many subscripts as we need for each dimension. Thus a two-dimensional array of 3 rows and 4 columns can be allocated as follows:

int table[3][4];

However, since each row is a one-dimensional array of length 4, we can also define each row using an alias:

using row = int[4];

row table[3];

We wouldn't normally do this as it hides an important implementation detail (table looks like a one-dimensional array when it's really a two-dimensional array), but this clearly demonstrates how a multi-dimensional array can be imagined as an array of arrays. Our table is an array of rows, where each row is an array of 4 elements.

This concept can be extend further:

using row = int[5];

using table = row[4];

table matrix[3];

This is really no different to the following:

int matrix[3][4][5];

Having declared our array, how do we access the elements within it? The obvious answer is to use the array subscript operator, []. That is, the first element of the second row of the third table can be found at matrix[2][1][0]. However, to aid in our understanding, we must examine what the subscript operator actually does.

The subscript operator returns a reference to an element within the array using a zero-based index. Since every element is the same size, it is trivial to calculate the starting address of any element given the starting address of the array and the zero-based offset of the element. We obtain the starting address from the name of the array itself. That is, the name of an array is also an alias for its own memory address. Since the first element of the array resides at that same address, then we can assert that for any array, a, the following must hold true:

a == &a[0];

Knowing this, for any array a and integer j within the range of a, the following must also hold true:

a[j] j[a];

It may seem odd that a[j] == j[a], however this is extremely low-level - we would not use this in production code (not if we want our code to remain readable, at any rate). More importantly, such assertions are only true of C-style arrays. When we come to C++ arrays, such assertions will not hold.

These assertions demonstrate the underlying pointer arithmetic that goes on Behind the Scenes whenever we use the subscript operator. The subscript operator is, in fact, nothing more than syntactic sugar-coating - it makes our code that much easier to read. For instance, imagine if we had to refer to element j of array a using *(a+j) rather than the much simpler a[j].

Like many operators, the subscript operator allows chaining. This is fortunate because, without chaining, we wouldn't be able to use the subscript operator in multi-dimensional arrays. Remember that the subscript operator returns a reference to an indexed element within the array. With multi-dimensional arrays, however, we are actually returning a reference to another array. Going back to our earlier example, matrix[2][1][0] can therefore be broken down as follows:

matrix[2] returns a reference to a two-dimensional array (e.g., the third table array of matrix)

matrix[2][1] returns a reference to a one-dimensional array (e.g., the second row array in the third table of matrix)

matrix[2][1][0] returns a reference to an element (e.g., the first element of the second row of the third table of matrix)

As well as C-style static arrays, C++ also supports static array objects as part of the Standard Template Library. These were first introduced as part of Technical Report 1 in C++98 (std::tr1::array) but became part of the standard itself in C++11 (std::array). These work exactly the same as a C-style static array and performance is exactly the same throughout. Aside from providing some useful member methods such as size(), fill() and swap(), the main difference is that a std::array object can also be treated just as if it were a std::tuple object (which cannot be done with C-style arrays). The only other difference worth noting is that the std::array::at() member method perform a bounds check whereas the subscript operator overload does not (C-style arrays perform no bound-checks at all).

Note that std::array is not intended to replace C-style static arrays since the STL has supported C-style arrays from the very beginning. However std::array includes features that are not found in C-style arrays, and the only way to achieve these features without std::array would be to copy the array into a sequence container that provided those features along with the need to copy any changes back to the original array. With std::array you gain all the benefits of a C-style array with all the benefits of an STL-sequence container within a single object.

The std::array object can also be used for multi-dimensional arrays. To create a three-dimensional static array similar to our matrix array, we could use the following:

std::array<std::array<std::array<int, 5>, 4>, 3> matrix;

Or we could use aliases as we did with our C-style matrix:

using row = std::array<int, 5>;

using table = std::array<row, 4>;

std::array<table, 3> matrix;

Both methods produce a static matrix not unlike int matrix[3][4][5].

Dynamic, multi-dimensional, C-style arrays are simply not worth the effort to be honest. C++ already provides the std::vector object specifically to deal with dynamic arrays and C-style dynamic arrays offer no advantages whatsoever. Indeed, C-style arrays are more a hindrance. To understand why, let's go back to the function we introduced earlier:

void foo (const size_t size)

{

int* x = new int[size];

// ...operate upon x.

delete [] x;

}

The first thing to notice is that we must maintain a pointer to our dynamic array and we must release the resource when we are finished with it. If the array must exist beyond the scope of a function, then the best solution is to place the array in a class-wrapper, where the constructor instantiates the array and the destructor releases it:

class foo

{

private:

int* m_data;

public:

foo (const size_t size): m_data (new int[size]) {}

~foo() { delete [] m_data; }

};

As soon as we do this we can see that such a class can easily be generalised as a class template:

template<typename T>

class foo

{

private:

T* m_data;

public:

foo (const size_t size): m_data (new T[size]) {}

~foo() { delete [] m_data; }

};

foo::size() would be a useful member method to have so we might as well store that along with our pointer:

template<typename T>

class foo

{

private:

T* m_data;

size_t size;

public:

foo (const size_t size): m_data (new T[size]), m_size (size) {}

~foo() { delete [] m_data; }

size_t size() const { return m_size; }

};

Being able to resize the array, adding and removing elements from the array, copying and assigning, overloading the subscript operator and so on are all useful things to have in our wrapper class, but think about what we're actually doing here. We're re-inventing the wheel!

Everything we've done and everything we plan to do is already available in std::vector. While std::vector includes a lot of functionality that you may not need in every situation, is there any justification in reproducing only the functionality that you do need? A lightweight std::vector if you will? In the vast majority of cases the answer would be no. The only justification would be in extreme memory-limited environments which would naturally require a non-conformant version of C++. However, implementations intended for such environments will include lightweight STL-like equivalents that provide the absolute minimum functionality required. So again, the answer is no - there is no justification. As a learning exercise it can be useful to improve your understanding of how STL actually works, but when writing production code you must throw away your lessons and use the tried and tested libraries that already exist. Don't re-invent wheels just because you can.

With that out of the way, how do we use std::vector to create multi-dimensional arrays? It will come as no surprise to you that we do it in the same way we created static arrays using std::array:

std::vector<std::vector<std::vector<int>>> matrix;

Note that we do not specify any bounds for the dimensions. We can clearly see there are three dimensions, but at this stage our matrix is empty; it has no elements. If we know what the initial elements will be we could supply them through an initialisation list. For example:

std::vector<std::vector<std::vector<int>>> matrix = {

{{00, 01, 02, 03, 04}, // table 0

{05, 06, 07, 08, 09},

{10, 11, 12, 13, 14},

{15, 16, 17, 18, 19}},

{{20, 21, 22, 23,24}, // table 1

{25, 26, 27, 28, 29},

{30, 31, 32, 33, 34},

{35, 36, 37, 38, 39}},

{{40, 41, 42, 43,44}, // table 2

{45, 46, 47, 48, 49},

{50, 51, 52, 53, 54},

{55, 56, 57, 58, 59;

Here we've effectively instantiated matrix[3][4][5] as a dynamic array. Note that there are 3 tables, where each table has 4 rows of 5 values.

Another way to initialise an array such as this is to use nested loops:

int value = 0;

std::vector<std::vector<std::vector<int>>> matrix;

for (size_t table=0; table<3; ++table)

{

std::cout << "Table "<< table << std::endl;

matrix.push_back(std::vector<std::vector<int>>());

for (size_t row=0; row<4; ++row)

{

matrix[table].push_back(std::vector<int>());

for (size_t col=0; col<5; ++col)

{

matrix[table][row].push_back (value++);

std::cout << matrix[table][row][col] << '\t';

}

std::cout << std::endl;

}

std::cout << std::endl;

}

std::cout << std::endl;

Output:

Table 0

0 1 2 3 4

5 6 7 8 9

10 11 12 13 14

15 16 17 18 19

Table 1

20 21 22 23 24

25 26 27 28 29

30 31 32 33 34

35 36 37 38 39

Table 2

40 41 42 43 44

45 46 47 48 49

50 51 52 53 54

55 56 57 58 59

Being a dynamic array, we are no longer restricted to having the same number of rows and columns within each table as we would be with a static array. This makes it possible to create triangular tables, for instance:

int value = 0;

std::vector<std::vector<std::vector<int>>> matrix;

for (size_t table=0; table<5; ++table)

{

std::cout << "Table "<< table << std::endl;

matrix.push_back(std::vector<std::vector<int>>());

for (size_t row=0; row<5; ++row)

{

matrix[table].push_back(std::vector<int>());

for (size_t col=0; col<=row; ++col)

{

matrix[table][row].push_back (value++);

std::cout << matrix[table][row][col] << '\t';

}

std::cout << std::endl;

}

std::cout << std::endl;

}

std::cout << std::endl;

Output:

Table 0

0

1 2

3 4 5

6 7 8 9

10 11 12 13 14

Table 1

15

16 17

18 19 20

21 22 23 24

25 26 27 28 29

Table 2

30

31 32

33 34 35

36 37 38 39

40 41 42 43 44

Table 3

45

46 47

48 49 50

51 52 53 54

55 56 57 58 59

Table 4

60

61 62

63 64 65

66 67 68 69

70 71 72 73 74

Since every row has a different number of elements within each table, we must be careful when using the subscript operator. However, like std::array, std::vector provides bounds-checking via the std::vector::at() member method. We can also use the std::vector::size() member method to determine the size of each vector. For example, matrix[1][2].size() will return 3 because there are three elements in that row (values 18, 19 and 20). If we need bounds-checking, then we could use matrix.at(1).at(2).size() instead.

Just as we can have triangular arrays, we can add and remove elements in any vector at any time:

matrix[1][2].push_back (100);

Now the row with 18, 19 and 20 will now contain 18, 19, 20 and 100.

That's pretty much all there is to multi-dimensional arrays. Everything you can do with a std::vector you can also do with a C-style dynamic array, of course, however the onus is entirely upon the programmer to manage the underlying resources. Vectors take care of all that for you and are no less efficient for it. Some programmers (particularly C programmers) feel C++ takes away too much control from the programmer, but then you have to question why they are using C++ in the first place. That's specifically what it was designed to do! C++ is every bit as efficient as C but is much easier to work with. If you have any doubts, then simply try it both ways and test the results; empirical evidence is undeniable evidence.

Before I go, there are a couple of points worth mentioning with regards the STL containers in general. Firstly, never derive any class directly from an STL container. The reason for this is simply that they were never intended to act as base classes and for that reason alone they do not have virtual destructors, which is an absolute requirement of any base class. Although the STL containers do form a hierarchical framework of sorts, that framework should not be considered a class hierarchy. The STL is actually made up of completely separate components such as adapters and algorithms, as well as the containers themselves, while iterators are used to "wire" these separate components together. While the STL does not strictly adhere to the object-oriented approach to programming, do keep in mind that C++ is nothing if not flexible and the STL is by far the most efficient generic library of its kind. But it is not a magic bullet to every problem. If you do need to derive from an STL container, the only way to do so is to define a class-wrapper with an embedded STL container, exposing as much or as little of the embedded container's interface as required. Your class-wrapper can then be given a virtual destructor and as many virtual methods as required and you can derive from that class as you see fit. The STL could have provided class-wrappers, of course, however class-wrappers cannot be generalised to suit every possible purpose, thus it is left to the programmer to create their own according to their specific needs.

Secondly, never put raw pointers in an STL container. When a container falls from scope it will destroy the pointers, but it will not release the resources they were pointing at. The reason for this simple: there is no way for the container to know who "owns" the resource being pointed at. If it were a shared resource and the container arbitrarily released it, all objects that shared that resource would be instantly invalidated. If you must place pointers in an STL container (which is actually the most common use for any container) then use a smart pointer rather than a raw pointer. If the container must "own" the resource, use a std::unique_ptr (the resource will be destroyed when the smart pointer falls from scope). If it is a shared resource, use std::shared_ptr instead (std::shared_ptr objects make use of resource counters to determine if the resource should be destroyed when the smart pointer falls from scope). There are other ways of dealing with raw pointers within STL containers, however you have to jump through a great many hoops while smart pointers take care of everything for you.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There are three ways. The first two are static declarations, the last is a dynamic declaration.

type name[dimension];

type name[] = { element_1, element_2, ..., element_n };

type* name = new type[dimension];

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A single-dimensional array is a one-dimensional array, or just simply an array. An array is a contiguous block of memory divided into one or more elements of the same data type. Since all elements are of the same type, they are also of the same size. This makes it trivial to calculate the position of each individual element given only the starting address of the array and a zero-based index of the element you are interested in (the size of each element is a given since the array's type determines the size of each element). In other words, arrays permit constant-time, random access to any value in the array.

Note that a multi-dimensional array is really no different to a one-dimensional array, it is simply an array where every element is itself an array (an array of arrays). The only real difference is the way in which the memory is laid out, which we'll discuss later.

Arrays can exist in memory or on disk. The only difference with a disk-based array is that instead of memory offsets you use file position offsets instead. Disk-based arrays are much slower to access, but are ideal when dealing with extremely large data sets that are too big to fit in memory all at once. For the remainder of this answer, we will only consider memory-based arrays.

Arrays can be static or dynamic. A static array is allocated at compile time on the stack whereas a dynamic array is allocated at run-time on the heap (or free store). Static arrays are ideally suited to relatively small arrays where the size of the array is known in advance. They are generally more efficient than dynamic arrays in terms of performance, however the stack is very limited and the size cannot be changed at run-time. For large arrays or where the number of elements cannot be determined at compile time or where the number of elements is variable, a dynamic array is the best choice.

A static array can be instantiated in C++ as follows:

std::array<int, 10> my_array {};

The std::array template is part of the C++ Standard Template Library (STL) and can be found in the <array> library header.

The above example declares a static array of type int with 10 elements. The {} is an empty initialiser list that ensures all the elements are initialised with the value zero. This is required because the int data type is a primitive data type and primitives have no constructors of their own.

You can also use the initialiser list to initialise arbitrary values:

std::array<int, 10> my_array {5,4,2,7,8,1,9};

The above intialises the first seven elements with the given values while the three remaining elements are initialised to zero.

To access an element in my_array we use pointer arithmetic. The name of the array is a reference to the start address of the array, so all we need is an offset from that address. The first element is at offset zero (because it resides at the address referenced by my_array), thus the fifth element is at offset 4. Therefore to assign a new value to the fifth element, we can use the following notation:

*(my_array + 4) = 42;

Although an int is sizeof(int) bytes, we don't need to calculate the number of bytes as part of the offset. The compiler already knows that my_array is a reference of type int and automatically works it out for us. In other words, it automatically jumps 4 integers into the array to arrive at the start address of the fifth integer. We then assign the value 42 to this dereferenced address.

Pointer arithmetic such as this is extremely efficient, however it's not particularly user-friendly. To make life easier for programmers, C++ permits the use of subscript notation:

my_array[4] = 42;

This is merely sugar-coating for the underlying pointer arithmetic, but is much easier to read and understand.

A common mistake when accessing elements is to forget that subscripts are zero-based:

my_array[10] = 42; // error!

Fortunately, the std::array knows its own size and can perform range-checks to ensure subscripts are within the accepted range. Has we used a C-style array, this type of error could be missed, resulting in a buffer overflow (writing beyond the array bounds) which inevitably leads to undefined behaviour.

You can, of course, use a C-style array (also known as a built-in array) if you wish, however do keep in mind that built-in arrays do not perform bounds checks. You declare a C-style array as follows:

int my_array[10] {};

Aside from lack of bounds checks, built-in arrays need to use pointer arithmetic to make use of STL algorithms. Therefore it's best to use the std::array for all fixed-length arrays where you need to sort elements and the like, and to use built-in arrays for trivial purposes such as unsorted data.

Dynamic arrays are also known as vectors in C++, and can be instantiated as follows:

std::vector<int> my_array {4,3,7,6};

The std::vector template can be found in the <vector> header. As you can see the only difference in the declaration is the use of the name vector instead of array. You use a vector in exactly the same way as an array, they both have the exactly same syntax throughout.

The only real difference between a vector and an array is that a vector can grow naturally simply by pushing new elements onto the back of the vector:

my_vector.push_back (42);

Behind the scenes, the vector reserves additional space to cater for new elements. When the reserve completely runs out, the vector automatically reallocates the array and reserves additional space, typically 1.6 times the occupied space. This is reasonably efficient, but if you know you're going to need a lot of additional space all at once, you can reserve as much or as little as you think you'll need:

my_vector.reserve (1000);

Once you've pushed all your elements onto the vector, you can eleminate any wasted space by shrinking:

my_vector.shrink_to_fit ();

To remove elements, you must remove them from the back of the vector:

my_vector.pop_back();

If you want to perform a lot of removals from the middle or beginning of a vector, then a vector is perhaps not the best choice of container as the used elements must remain in contiguous memory with no gaps. A std::deque is better suited to adding or removing from the beginning and end of the container while a std::list is better suited to adding or removing anywhere within the container.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

There is no insert in an array, you have to move the existing elements (memmove) to make room for the new element.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the syntax for one dimensional array in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is non linear array?

What is ARRAY, explain its typesARRAY: -It is the combination of same data type or if same data is needed more then none time then we use array.Types Normally there are two types of array.(1) One dimensional array(2) Two dimensional array(1) One dimensional array: -This array is in form a list (vertical\Horizontal) the data input output or process in one dimensional array with the help of loop.The syntax of one dimensional array will be as:Array name [strength]For exampleA [5];The structure of above syntax will be as:A (0)A (1)A (2)A (3)A (4)(2) Two dimensional array: -Two dimensional array is also called a table because it consist of rows and columns. The syntax of two dimensional arrays will be as:Data type array name [rows] [columns]For exampleInt [3] [2];The structure of above syntax will be as:A [0] [0] A [0] [1]A [1] [0] A [1] [1]A [2] [0] A [2] [1]The data input output are process in this array with the help of nested loop.


What does a 2 dimensional array do?

A two dimensional array is a one-dimensional array of one-dimensional arrays. That is, just as we can have an array of integers, we can also have an array of integer arrays. This idea can be extended such that we can have an array of two-dimensional arrays (a three-dimensional array), and so on. We typically use a two-dimensional array to represent a table of rows and columns, where each row is a one-dimensional array.


Explain the Different types of array?

one dementional array and two dementional array


How will you initialize a 2-d array?

All arrays are one-dimensional. A two-dimensional array is simply a one-dimensional array of one-dimensional arrays: int a[2][3]; This is an array of 2 elements where each element is itself an array of 3 integers. In other words it is an array of 6 integers. The two dimensions simply allow us to split the array into two sub-arrays of 3 elements each.


What are 3 different ways to implement an array of order 4x8?

An array of order 4x8 can either be implemented as a one-dimensional array of order 32 or as a one-dimensional array of order 4, where each element is a one-dimensional array of order 8. In either case, the 32 data elements are allocated contiguously and there is no difference in performance. A third way is to implement the one-dimensional array of order 4 as an array of pointers to separately allocated one-dimensional arrays of order 8. The order 4 array is contiguous as are the order 8 arrays, however they need not be contiguous with one another other. This is the least efficient implementation due to the additional level of indirection required to navigate the array.

Related questions

What is single dimensional array in c plus plus?

A one dimensional array is an array of objects that goes in one "direction". Any array with only one [] is a one dimensional array. For example: int numbers[6]; is a one dimensional array. int numbers[6][3]; is a two dimensional array.Graphical terms:One dimensional array[4]:14 - 75 - 8164 - 234Two dimensional array[2][3]:47 - 178108 - 8517 - 128It didn't come out quite how I wanted it...


What is non linear array?

What is ARRAY, explain its typesARRAY: -It is the combination of same data type or if same data is needed more then none time then we use array.Types Normally there are two types of array.(1) One dimensional array(2) Two dimensional array(1) One dimensional array: -This array is in form a list (vertical\Horizontal) the data input output or process in one dimensional array with the help of loop.The syntax of one dimensional array will be as:Array name [strength]For exampleA [5];The structure of above syntax will be as:A (0)A (1)A (2)A (3)A (4)(2) Two dimensional array: -Two dimensional array is also called a table because it consist of rows and columns. The syntax of two dimensional arrays will be as:Data type array name [rows] [columns]For exampleInt [3] [2];The structure of above syntax will be as:A [0] [0] A [0] [1]A [1] [0] A [1] [1]A [2] [0] A [2] [1]The data input output are process in this array with the help of nested loop.


How does two dimensional array differ from single dimensional array?

A one dimensional array is a scalar value repeated one or more times.A two dimensional array is an array of one dimensional arrays.A three dimensional array is an array of two dimensional arrays, and so forth.The one dimensional array is like a list of things, where the two dimensional array is like an array of things. (Think one row of a spreadsheet versus the whole spreadsheet.)[addendum]Every level of array depth is also a level of pointer depth. For example: A 3 dimensional int array is an int***. So a one dimensional int array is an int*, and a two dimensional int array is an int**. This is only important if you are doing pointer work, but it can become very important.


What does a 2 dimensional array do?

A two dimensional array is a one-dimensional array of one-dimensional arrays. That is, just as we can have an array of integers, we can also have an array of integer arrays. This idea can be extended such that we can have an array of two-dimensional arrays (a three-dimensional array), and so on. We typically use a two-dimensional array to represent a table of rows and columns, where each row is a one-dimensional array.


Explain the Different types of array?

one dementional array and two dementional array


What is a multi array?

A two-dimensional array is the simplest multi-dimensional array and is implemented as a one-dimensional array where every element is itself a one-dimensional array. We can imagine a two-dimensional array as being a table of rows and columns where every row is an array in its own right. A three-dimensional array is simply a one-dimensional array of two-dimensional arrays, which can be imagined as being an array of tables. Extending the concept, a four-dimensional array is a table of tables. Multi-dimensional arrays may be jagged. That is, a two-dimensional array may have rows of unequal length. Unlike regular arrays, jagged arrays cannot be allocated in contiguous memory. Instead, we use the outer array (the first dimension) to store pointers to the inner arrays. An array of strings (character arrays) is an example of a two-dimensional jagged array.


Give the syntax to declare two dimensional array using array of pointers?

It is not possible to declare a two-dimensional array using an array of pointers in any programming language, but many programming languages support declarations of N-dimensional arrays of pointers.The exact syntax varies with the programming language, and requires support for N-dimensional arrays and pointers. In C, the following declares an array of pointer variables, each implemented as pointer to the generic type "void":void* array_1D[10];The type of the expression array_1D is "void * const."The following example expands on the previous one by declaring a two-dimensional array of "void" pointers:void* array_2D[10][20];The type of the expression array_2D is "void ** const."The last example declares a 3-dimensional array of "void" pointers, which can be seen as a 2-dimensional array of arrays of pointers:void* array_3D[10][20][30];


How many types of arrays in c?

An array is simply a contiguous block of memory containing two or more elements. There are two types of array: a static array which is allocated on the stack at compile time; and a dynamic array which is allocated on the heap at runtime. Both can be one-dimensional or multi-dimensional. A one-dimensional array can be likened to a row (or column) of chessboard squares, with as many squares as required to store all the elements. A multi-dimensional array is any array with two or more dimensions. A two-dimensional array can be likened to the whole chessboard, where any square can be identified by its row and column index. However the dimensions needn't be equal. A two-dimensional array can also be imagined as a one-dimensional array where every element is simply another one-dimensional array. Three-dimensional arrays can be likened to a cube, or as a one-dimensional array of two-dimensional arrays. A four-dimensional array can be linked to a one-dimensional array of three-dimensional arrays, and so on. Although every one-dimensional array must be allocated in contiguous memory, multi-dimensional arrays can be dynamically allocated so that each dimension is itself a separately allocated one-dimensional array of pointers to the next dimension, making it possible to allocate extremely large arrays over a series of smaller allocations rather than as a single contiguous block.


What is one dimentional array?

A One dimensional array is one in which a set of values are present in it. Ex: int[] myArray = new int[4]; The above statement creates a one dimensional array that can hold 4 values.


How will you initialize a 2-d array?

All arrays are one-dimensional. A two-dimensional array is simply a one-dimensional array of one-dimensional arrays: int a[2][3]; This is an array of 2 elements where each element is itself an array of 3 integers. In other words it is an array of 6 integers. The two dimensions simply allow us to split the array into two sub-arrays of 3 elements each.


What are 3 different ways to implement an array of order 4x8?

An array of order 4x8 can either be implemented as a one-dimensional array of order 32 or as a one-dimensional array of order 4, where each element is a one-dimensional array of order 8. In either case, the 32 data elements are allocated contiguously and there is no difference in performance. A third way is to implement the one-dimensional array of order 4 as an array of pointers to separately allocated one-dimensional arrays of order 8. The order 4 array is contiguous as are the order 8 arrays, however they need not be contiguous with one another other. This is the least efficient implementation due to the additional level of indirection required to navigate the array.


Pointer to 3 dimensons array?

If the array is static you can simply point at the first element. For dynamic arrays you can allocate a contiguous block to a single pointer which can then be subdivided using a one-dimensional array of pointer to pointers, each of which points to a one-dimensional array of pointers, each of which points to a separate object within the array. For extremely large arrays, however, it is better to split the elements into separate one-dimensional arrays, by creating a one-dimensional array of pointer to pointers first, then allocating each of those pointers to a separate one-dimensional array of pointers, each of which points to a separate one-dimensional array of objects. Either way, you must destroy all the individual arrays in the reverse order of creation.