DATA STRUDCTURE 2D Array

Deepa

 

DATA STRUDCTURE

2D Array Overview

A 2D array is essentially an array of arrays, organized in a matrix format with rows and columns. This structure is commonly used to mimic the functionality of a relational database table, providing a convenient way to handle large amounts of data efficiently.

 

 Declaring a 2D Array

The syntax for declaring a 2D array is similar to that of a one-dimensional array, but with two dimensions specified:

 

int arr[max_rows][max_columns];

 

This creates a data structure where each element is accessible using two indices: one for the row and one for the column.

 

Accessing Data in a 2D Array

You can access any element in a 2D array by specifying its row and column indices:

 

int x = arr[i][j];

 

Here, `i` and `j` are the row and column indices, respectively.

 

Initializing a 2D Array

Unlike one-dimensional arrays, where you can omit the size, a 2D array requires at least the size of the second dimension to be specified during initialization:

 

int arr[2][2] = {0, 1, 2, 3};

 

Example of 2D Array Usage

C Example

 

#include <stdio.h>

void main() {

    int arr[3][3], i, j;

 

    // Input elements in the array

    for (i = 0; i < 3; i++) {

        for (j = 0; j < 3; j++) {

            printf("Enter a[%d][%d]: ", i, j);

            scanf("%d", &arr[i][j]);

        }

    }

 

    // Print elements of the array

    printf("\nPrinting the elements...\n");

    for (i = 0; i < 3; i++) {

        printf("\n");

        for (j = 0; j < 3; j++) {

            printf("%d\t", arr[i][j]);

        }

    }

}

 

Java Example

 

import java.util.Scanner;

 

public class TwoDArray {

    public static void main(String[] args) {

        int[][] arr = new int[3][3];

        Scanner sc = new Scanner(System.in);

 

        // Input elements in the array

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                System.out.print("Enter Element: ");

                arr[i][j] = sc.nextInt();

            }

        }

 

        // Print elements of the array

        System.out.println("Printing Elements...");

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                System.out.print(arr[i][j] + "\t");

            }

            System.out.println();

        }

    }

}

 

C Example

 

using System;

 

public class Program {

    public static void Main() {

        int[,] arr = new int[3, 3];

 

        // Input elements in the array

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                Console.WriteLine("Enter Element:");

                arr[i, j] = Convert.ToInt32(Console.ReadLine());

            }

        }

 

        // Print elements of the array

        Console.WriteLine("Printing Elements...");

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                Console.Write(arr[i, j] + " ");

            }

            Console.WriteLine();

        }

    }

}

 

Mapping 2D Array to 1D Array

In memory, a 2D array is stored as a 1D array. Two common techniques are used for mapping:

 

1. Row Major Order: The rows are stored contiguously in memory.

   Address calculation: `Address(a[i][j]) = B.A. + (i * n + j) * size`

   Example: Calculate the address of `a[15][68]` with `B.A. = 0`, `size = 4 bytes`, in a `10...30, 55...75` array:

     `Address(a[15][68]) = (5 * 14 + 13) * 4 = 332 bytes`

 

2. Column Major Order: The columns are stored contiguously in memory.

   Address calculation: `Address(a[i][j]) = ((j * m) + i) * Size + B.A.`

   Example: Calculate the address of `a[0][30]` with `B.A. = 1020`, `Size = 8 bytes`, in a `-5...+20][20...70` array:

     `Address(A[0][30]) = 2980 bytes`

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send