answersLogoWhite

0


Best Answer

#include <iostream>

#include <iomanip>

using std::cout;

using std::endl;

using std::setw;

int main()

{

static const char *Rush[3][2] = {

{ "Geddy Lee", "Vocals, bass and keyboards" },

{ "Alex Lifeson", "Lead/rhythm guitar" },

{ "Neil Peart", "Drums & percussion" }};

cout << "Personnel:" << endl << endl;

for (int nPerson=0; nPerson<3; ++nPerson)

cout << setw(16) << Rush[nPerson][0] << " : " << Rush[nPerson][1] << endl;

cout << endl;

return( 0 );

}

User Avatar

Wiki User

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

Wiki User

11y ago

A simple formula to declare arrays in C++ is as:

1. For 1D or one dimensional array:

data_type name_of_array[size_of_array] ;

For example, if I want to declare a 1D array named marks, of integer type with a size 10, I will write

int marks [10];

2. For 2D or two dimensional arrays:

data_type name_of_array[number_of_rows][number_of_columns] ;

For example, to declare any floating point, 2D array, with 3 rows and 5 columns, I will write

float sample [3][5];

The above methods are fine for static arrays, where the number of elements is known at compile time, but not dynamic arrays, which must be determined at runtime. The following will allocate memory to a one dimensional array of 10 integer elements

int elems = 10; // variable -- can be changed at runtime by whatever means.

int * p = ( int * ) calloc( elems, sizeof( int ));

// use p as you would a static array.

p[1] = 100;

printf( "%d\n", p[1] ); // prints the second element.

// release the allocated memory.

free( p );

2 dimensional dynamic arrays require the use of pointer-to-pointer types. Basically, you create a pointer-to-pointer variable which points to a one-dimensional array of pointers. Each of the pointers in the array points to a one-dimensional array of the actual type. All pointers must be the same type as the type in the array.

// equivalent of a [10][20] array of type float:

int d1 = 10, d2 = 20; // variables -- can be changed at runtime.

float ** pp = ( float * ) calloc( d1, sizeof( float ));

// now instantiate the second dimension:

for( int i = 0; i < d1; ++i )

pp[i] = ( float * ) calloc( d2, sizeof( float ));

// use pp as you would a static array.

pp[1][2] = 199.99;

printf( "%.2f", pp[1][2] );

// Remember to release the 2nd dimension before deleting the main pointer.

for( int i = 0; i < d1; ++i )

free( pp[i] );

free( pp );

Larger multi-dimensional dynamic arrays are created by adding more levels of indirection to the main array pointer. So a 3D dynamic array uses a pointer-to-pointer-to-pointer variable to point to a one-dimensional array of pointer-to-pointer, and each of those pointer-to-pointer elements represents a two dimensional array as shown above. The only thing to remember is that you must release each one-dimensional array in the reverse order it was allocated. So if you have a 3 dimensional array, release the 3rd dimension before releasing the 2nd dimension, then release the main pointer (the 1st dimension).

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include<iostream>

int main()

{

int a[5];

int b[3]={1,2,3};

int c[]={1,2,3,4,5};

int* d = new int[5];

delete[]d;

}

In the above example, a, b and c are static arrays while d is a dynamic array. a is uninitialised, thus you must initialise each element before using it. b and c are initialised with the given values. Although c has no explicit dimension, 5 elements are specified thus it has an implicit dimension of 5. If you initialise fewer elements than specified by the dimension, the remaining elements are assigned 0. If you initialise more elements than are defined, a compiler errror occurs.

Note that d must be released when no longer required. Note the use of the subscript operator [] to ensure all elements in the array are deleted. If you omit this operator, only the first element is deleted.

In C++, an STL vector can be used instead of a dynamic array. Vectors are objects which makes it much easier to pass arrays by reference into functions, without the need to also pass the size of the array (the size is a private member of the vector object).

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

Like so:

int x[2][3] = {{00, 01, 02}, {10, 11, 12}};

The above declares a two-dimensional array with 2 rows and 3 columns. Note the whole initialiser is enclosed in braces while each row is also enclosed in braces. This is because each row is a one-dimensional row with 3 elements, thus each must be initialised separately.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

the process is same as that for C. char array [10][20];

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you declare a two dimensional array with initialiser in C programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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];


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.


Is two dimensional array is multi dimensional array in c language?

Yes because multi means more than one in programming, just like it does in English. No isn't that a surprise. Have you read any good programming text books lately.


Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


Columns and rows in c programming?

Do you perhaps mean -- a two-dimensional array? A two dimensional array is nothing more than a one-dimensional array where every element is a one-dimensional array. int matrix[4][5]; C is a row-major language thus the first dimension refers to the number of rows. Here we have declared an array of 4 rows, where each row is an array of 5 elements of type int.


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 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.


Explain the Different types of array?

one dementional array and two dementional array


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 a table array?

A two-dimensional array.


How do you declare an int array?

we define the array isArray array[]={1,2,3,4,5} this is the integer ArrayArray array[]={"apple","banana","carrot","mango"} this is the String Array


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.