Notes on Arrays in C Programming

Arrays

  • Definition: A composite of related data items stored under the same name.
  • Purpose: To store data in a more organized fashion.

Data Structures

  • Allows programmers to efficiently group related items for better management.

Arrays in C

  • Characteristics:
    • Collection of data items of the same data type.
    • Similar to a list with a predefined number of slots.
    • Individual items are known as elements.
  • Declaring an Array:
    • Syntax: data_type array_name[size];
    • Example: int nums[10]; declares an integer array named nums with 10 elements.

Accessing Elements

  • Each element in an array is accessed using a subscript (or index).
  • The first index starts at 0, and goes up to N-1 (where N is the number of elements).
  • Syntax for access: array_name[index]

Memory Addresses of Elements

  • Elements of an array are stored in contiguous memory locations.
  • The address of the first element is the same as the address of the array itself.
  • Example of accessing each element's address:
    c int nums[10]; for(i = 0; i < 10; i++) { printf("Address of nums[%d]: %p\n", i, &nums[i]); }

Problem Solving: Average of Numbers

  • Scenario: Calculate the average of 100 integers.
  • Objectives:
    1. Obtain numbers from the user and store them in an array.
    2. Compute the average of these numbers.
  • Using loops to gather inputs and to compute sums.

Common Operations with Arrays

  • Iterating through array elements to perform tasks such as:
    • Input values
    • Calculate sums
    • Print values

Macros in C

  • Use macro definitions to reduce magic numbers and improve code maintainability.
  • Example: #define MAX 100 allows you to use MAX instead of 100 throughout the code.

Array Initialization

  • You can initialize an array upon declaration using an initializer list:
    c int nums[5] = {1, 2, 3, 4, 5};
  • Default values for uninitialized elements:
    • int = 0
    • double = 0.0
    • char = '\0' (null character)

Searching within an Array

  • Linear Search: A simple method of searching for a specific value.
    • Loop through the array and check if the current element matches the target value.

Circular Shift

  • Definition: Shifts elements in an array to the left by a specified number k, with wrapped-around behavior for the elements.

Variable-Length Arrays (VLAs)

  • Used when the size of the array is determined at runtime (requires dynamic memory handling).

2D Arrays

  • Two-dimensional arrays store data in a grid (rows and columns).
  • Declaration: data_type array_name[rows][cols];
  • Traversal requires nested loops (outer loop for rows and inner loop for columns).

Multi-Dimensional Arrays

  • Arrays can be extended beyond 2D, but are harder to visualize as dimensions increase.
  • Declaration: data_type array_name[depth][rows][cols];

Final Notes

  • Practice solving problems using arrays since foundational operations are reusable across different scenarios and programs.