Computer Science, asked by chandankumar2216, 10 months ago

Define an array.Explain one dimensional and two dimensional array.

Answers

Answered by ashnishinnu
2

Answer:

Explanation:

An array is a collection of variables that are of similar data types and are referred by a common name. A specific element in an array is accessed by a particular index of that array. Arrays in Java work differently as compared to C++. The main topic of our discussion is the difference between One-dimension and Two-Dimension array. A one-dimensional array is a list of variables with the same datatype, whereas the two-Dimensional array is ‘array of arrays’ having similar data types. C++ do not have bound checking on arrays whereas, Java have strict bound checking on arrays.

Definition of One-Dimensional Array (1-D array)

One-Dimensional or Single-Dimensional array is considered as the” list of variables of similar data types”, and each variable can be distinctly accessed by specifying its index in square brackets preceded by the name of that array.

1d arrayIn C++, the declaration of an array variable with size is enough to allocate space for them in memory. In Java, this is achieved in two steps. First, you must declare a variable of the desired type. Second, you must allocate the memory to hold the array using ‘new’ and assign it to declared array variable. Hence, arrays are dynamically allocated in Java.

Let’s discuss in C++ Context

   //declaration in C++

   type variable_name[ size ];

Here type declares the data type of array variable, and size defines the number of element that array will hold.

For example, if we want to declare an array which will contain the balance of each month of the year.

   //example

   int month_balance[12];

Month _balance is the array variable which will hold the 12 integers, which will represent the balance of each month. Now, if we want to access the balance of month ‘April’ we simply had to mention the variable name followed by square bracket containing the index value for the month of April  i.e. ‘month_balance[3]’. But as ‘April’ is the fourth month of the year but we had mentioned ‘[3]’ because all arrays have 0 as the index of their first element.

In Java, this can be done as

   //declaration in Java

   type variable_name [];

   variable_name = new type[size];

Here, initially we had declared an array variable with its type and then we had allocated memory to it using ‘new’ and the assign ‘new’ to the declared array variable. Let’s take above example if we want to declare an array which will contain the balance in each month of the year.

   //example

   int month_balance[];

   month_balance= new int[12];

Here, ‘new’ allocates memory to array variable “month_balance”, so now, mont_balance will now hold the memory for 12 integer values.

Arrays can be initialized when they are declared. An array initializer is the list of comma-separated values surrounded by curly braces.

//example

   int month_balance={ 100, 500, 200, 750, 850,  250, 630, 248, 790, 360, 450,180 };

Definition of Two-Dimensional Array (2-D array)

Both C++ and Java support multidimensional array. One of the simplest forms of a multidimensional array is a, two-dimensional array or 2-D array. A two-Dimensional array can be considered as ‘array of arrays’  or ‘array of one-dimensional arrays’. To declare the two-dimensional array variable, we have to specify the array name followed by two square brackets where the second index is the second set of square brackets.

A two-dimensional array is stored in the form of the row-column matrix, where the first index indicate the row and second index indicates the column. The second or the rightmost index of an array changes very fastly as compared to first or left-most index while accessing the elements of an array.

2d arrayIn C++, the two-dimensional array is declared as;

   //declaration in C++

   type variable_name[size1][size2];

For example, we want to store the balance of every 30 days in each month of the year, in a 2-D array.

   //example

   int month_balance[12][30];

In Java, the two-dimensional array is obtained by

   //declaration in Java

   type variable_name= new int[size1][size2];

   //example

   int month_balance= new int [12][30];

As we cannot pass the entire array as a parameter to a function, a pointer to the first element of the array is passed. An argument receiving the two-dimensional array must define it’s the right-most dimension. The rightmost dimension is required because the compiler needs it, to confirm the length of each row if it wants to index the array correctly. If the right-most index is not mentioned, the compiler cannot determine where the next row starts.

 

When the memory is dynamically being allocated to the two-dimensional array in Java, the left-most index is specified, and the remaining dimensions can be allocated separately i.e. all rows of the array may not be of the same size.

Answered by nitinrajverma4
0

Answer:

An array is a collection of variables that are of similar data types and are referred by a common name. A specific element in an array is accessed by a particular index of that array. Arrays in Java work differently as compared to C++. The main topic of our discussion is the difference between One-dimension and Two-Dimension array. A one-dimensional array is a list of variables with the same datatype, whereas the two-Dimensional array is ‘array of arrays’ having similar data types. C++ do not have bound checking on arrays whereas, Java have strict bound checking on arrays.

Explanation:

Similar questions