Lecture Notes on Arrays

Jun 22, 2024

Lecture Notes on Arrays

Introduction to Variables

  • Fundamental data types: int, float, char, double, etc.
  • Example: int a = 5;
    • Declaration of a variable a of type int with value 5.
    • Storage in memory involves memory allocation by the compiler.

Memory Representation

  • Memory is a long tape of bytes (1 byte = 8 bits).
  • Storage of int a = 5:
    • Allocates 4 bytes (or 32 bits) in memory for an integer.
    • The number 5 is converted to binary and stored in these 4 bytes.
    • Example Addressing:
      • If starting byte address is 100, then the next bytes would be 101, 102, and 103.
      • Addressing commonly expressed in hexadecimal format (e.g., 0x64).

Need for Arrays

  • Single variables can only store one value at a time.
  • To store multiple values (e.g., roll numbers of 60 students), arrays are used.
  • Arrays allow storing multiple values under one variable name.

Declaration of Arrays

  • General Syntax:

    • data_type array_name[size];
    • Example: int a[60]; stores 60 integers.
  • Invalid Declarations:

    • int a[]; (Size must be specified).
    • int a[n]; where n is a variable (unless using macros).
  • Language-Specific Syntax:

    • Different languages have different array declaration syntaxes (C, Python, Pascal, etc.).

Types of Arrays

  • One-Dimensional (1D) Arrays
  • Two-Dimensional (2D) Arrays
  • Multi-Dimensional Arrays

Initializing Arrays

  • Arrays are a collection of elements of the same data type.

  • Valid Examples:

    • int a[5] = {6, 2, 4, 3, 0}; (integers)
    • char b[5] = {'a', 'b', 'c', 'd', 'e'}; (characters)
    • float c[5] = {1.1, 2.2, 3.3, 4.4, 5.5}; (floats)
  • Invalid Examples:

    • int d[5] = {1, 'a', 3, 'b', 5}; (mixed data types)

Memory Storage of Arrays

  • Elements stored in consecutive memory locations.
  • Index usually starts at 0.
  • Accessing Elements:
    • Using index (e.g., a[0] for the first element).
    • Formulate the address: base_address + (index * size_of(data_type))
    • Provides random access, allowing efficient retrieval.

Array Initialization Methods

  • Compile-Time Initialization:

    • Specify elements at the time of declaration.
    • Example: int arr[5] = {6, 2, 4, 3, 0};
  • Run-Time Initialization:

    • Elements specified while the program is running.
    • Use loops and standard functions (scanf in C).
    • Example code for run-time initialization:
      int arr[5];
      for(int i = 0; i < 5; i++) {
          scanf("%d", &arr[i]);
      }
      

Important Properties and Drawbacks of Arrays

  • Fixed size: Size cannot be changed at run-time.
  • Sequenced collection of same data type items.
  • Random access allows constant time retrieval (O(1)).
  • Drawbacks:
    • Cannot change size after declaration.
    • Memory wastage if declared size is larger than needed.
    • Inflexibility if more space is needed than declared.

Summary

  • 1D Arrays:
    • Declaration, initialization, and memory representation.
    • Discussed fixed size, continuous memory allocation, and random access.
  • Future Topics:
    • Operations on 1D arrays (e.g., insertion, traversal).
    • Introduction to 2D arrays and accessing their elements.