Array and two-dimensional array, Data Structure & Algorithms

Assignment Help:

Q. Describe the term array.  How do we represent two-dimensional arrays in memory?  Explain how we calculate the address of an element in a two dimensional array.                                                

Ans:

An array is a systematic arrangement of objects generally in rows and columns

Each memory location is defined by an array element. An array element is similar to one variable except it is identified by an index value as a substitute of a name. An index value is a number used to recognize an array element.

Declaration of a two dimensional array- A two dimensional array is declared similarly as we declare a one-dimensional array except that we specify the number of elements in both dimensions. Such as,

int grades[3][4];

The first bracket ([3]) tells the compiler that we are declaring 3 number of pointers, each of them pointing to an array. Here we are not talking about a pointer variable or pointer array. Instead of that, we are saying that each element of the first dimension of a two dimensional array reference a corresponding second dimension. In the above example, all the arrays pointed to by the first index are of the same size. The second index may be of variable size. For example, the earlier statement declares a two- dimensional  array  where  there  are  3  elements  in  the  first  dimension  and  4 elements in the second dimension array.

Two-dimensional array can be represented in memory by following two ways:

1.  Row major representation: To attain b this linear representation, the first row of the array is stored in the first memory locations reserved for the array, then the second row and this process continues.

2.  Column major representation: In this elements of the column are stored next to one another.

In row major representation, the address is always calculated in a two dimensional array as per the formula written below. The address of a[i][j]=base(a)+(i*m+ j)*size in which base(a) is the address of a[0][0], m is second dimension of array a and size represent size of the data type.


Related Discussions:- Array and two-dimensional array

Define graph, A graph is a mathematical structure giving of a set of vertex...

A graph is a mathematical structure giving of a set of vertexes (v1, v2, v3) and a group of edges (e1, e2, e3). An edge is a set of vertexes. The two vertexes are named the edge en

Several operations on a aa-tree, The following are several operations on a ...

The following are several operations on a AA-tree: 1. Searching: Searching is done using an algorithm which is similar to the search algorithm of a binary search tree. 2. Ins

Algorithm to insert a node in between any two nodes, Q. Write down an algor...

Q. Write down an algorithm to insert a node in between any two nodes in a linked list         Ans. Insertion of a the node after the given element of the listis as follows

Dynamic programming., Count Scorecards(30 points) In a tournament, N playe...

Count Scorecards(30 points) In a tournament, N players play against each other exactly once. Each game results in either of the player winning. There are no ties. You have given a

Example of binary search, Let us assume a file of 5 records that means n = ...

Let us assume a file of 5 records that means n = 5 And k is a sorted array of keys of those 5 records. Let key = 55, low = 0, high = 4 Iteration 1: mid = (0+4)/2 = 2

What is gouraud shading, Gouraud Shading The faceted appearance of a La...

Gouraud Shading The faceted appearance of a Lambert shaded model is due to each polygon having only a single colour. To avoid this effect, it is necessary to vary the colour ac

C++ function, Write c++ function to traverse the threaded binary tree in in...

Write c++ function to traverse the threaded binary tree in inorder traversal

Define tree ?, A tree is a non-empty set one component of which is designat...

A tree is a non-empty set one component of which is designated the root of the tree while the remaining components are partitioned into non-empty groups each of which is a subtree

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd