Arrays

EGN 3211 - Engineering Analysis and Computation (Fall 2024)Instructor: Dr. Suboh A. SubohDepartment: Electrical and Computer EngineeringUniversity: Central Florida

Arrays Overview

  • Definitions and Importance: Arrays are fundamental data structures used to store multiple values of the same type in a single variable, allowing efficient data management and access.

  • Types of Arrays: There are several types of arrays, including one-dimensional, two-dimensional, and variable-length arrays, each serving different purposes and use cases in engineering computation.

Declarations of Arrays

  • One-Dimensional Arrays

    • Syntax: type name[expr];

    • Components:

      • type: Specifies the data type (e.g., int, float) of array elements.

      • name: The unique identifier for the array.

      • expr: An integral expression to define the number of elements.

    • Example:

      int a[6]; // Declares an array named 'a' with 6 integer elements.  

Memory Storage of Arrays

  • Efficiency: Arrays occupy consecutive memory locations, which enhances access speed due to locality of reference. All elements share the same name (identifier) and data type.

  • Addressing: Given name[position_number], each element can be accessed directly.

  • Example:

    int a[6]; // Occupies 24 bytes in memory (on a typical system)  
    • Memory Layout Example:

      • a[0]: 0x2000

      • a[1]: 0x2004

      • a[5]: 0x2014

Two-Dimensional Arrays

  • Declaration

    • Syntax: type name[expr1][expr2];

    • Components:

      • expr1: Number of rows

      • expr2: Number of columns

    • Example:

      int a[2][3]; // Array 'a' has 2 rows and 3 columns (6 elements total).  
  • Memory Representation:

    • Elements in a 2D array are accessed using arrayname[row][column].

    • Example:

      int b[2][3]; // Represents a block of memory where each row is contiguous.  

Definitions Related to Arrays

  • Rank: Indicates the number of dimensions of the array. For instance, int a[3][4] has a rank of 2.

  • Extent: Refers to the number of elements in each dimension, e.g., [3, 4] in the previous example.

  • Shape: A vector that illustrates extents of each dimension.

  • Size: The total bytes used to store all elements, calculated as total_elements * size_of_element_type. For int a[3][4], Size is 48 bytes (4 bytes per int * 3 rows * 4 columns).

Size of Arrays

  • Determining Size using sizeof Operator:

    • Examples:

      int a[6]; // sizeof(a) = 24  
      double d[3][4]; // sizeof(d) = 96  

Array Initialization

  • One-Dimensional:

    • Example:

      int a[5] = {1, 2, 3, 4, 5}; // Initializes array 'a' with specified values  
      • Leftover elements are initialized to zero if initializers are insufficient.

  • Two-Dimensional Arrays:

    • Example:

      int a[2][2] = {{1, 2}, {3, 4}}; // Must use braces to group initializers by row.  

Processing Data in Array

  • Generating Linearly Spaced Data:

    • Code Snippet:

      double x[N];  
      for (i = 0; i < N; i++) {  
          x[i] = x0 + i * (xf - x0) / (N - 1);  
      }  
  • Average (Mean) Calculation:

    • Formula: Sum of elements / Number of elements.

    • Example Code:

      double sum = 0;  
      for (i = 0; i < N; i++) {  
          sum += a[i];  
      }  
      meanval = sum / N;  

Variable Length Arrays (VLA)

  • Dynamic Size Capability: Arrays can be created with sizes determined at runtime. They must maintain the same rank and extents as the actual argument passed to functions.

  • Example:

    void func(int m, int n, int a[m][n]); // Function prototype illustrating a VLA.  

Vector Operations

  • Scalar Product:

    • Definition: The scalar product of two vectors a and b is calculated as:s = a . b = a1*b1 + a2*b2 + ... + an*bn

    • Example Code:

      double s = 0;  
      for (i = 0; i < N; i++) {  
          s += a[i] * b[i];  
      }  

Matrix Operations

  • Addition/Subtraction of Matrices: Requires matrices to have the same dimensions.

  • Example for Matrix Addition:

    for (i = 0; i < M; i++) {  
        for (j = 0; j < N; j++) {  
            c[i][j] = a[i][j] + b[i][j];  
        }  
    }  
  • Matrix by Vector Multiplication: The number of columns in the matrix must equal the number of elements in the vector.

  • Example Code for Multiplication:

    for (i = 0; i < M; i++) {  
        b[i] = 0;  
        for (j = 0; j < N; j++) {  
            b[i] += a[i][j] * x[j];  
        }  
    }  

Statistical Libraries in C

  • Functions Overview: These libraries provide key statistical functions that include:

    • Mean

    • Minimum

    • Maximum

    • Variance

    • Standard Deviation

    • Sorting

    • Median

  • Example Usage: Utilizing these functions allows engineers to perform statistical analysis efficiently on datasets, which is vital for data interpretation and decision-making in engineering.

Sample Application

  • GPA Calculation: This section may illustrate practical applications such as computing various statistics from GPAs stored in an array.

  • Example Code for Statistics Calculation: Demonstrates how to compute mean, minimum, maximum values within an array representation of GPAs.

  • Output Demonstration: Various statistics can be displayed using printf statements to provide insights into the GPA data set.

Final Remarks

  • Understanding Structure and Indexing: Clear understanding of array structure and proper indexing is critical for avoiding errors in data access and manipulation.

  • Function Implementation Practice: Students must practice implementing functions that accept array arguments to solidify their understanding of how data can be processed efficiently.

  • Familiarization with Vector and Matrix Operations: A strong grasp of vector and matrix operations is essential for solving complex engineering problems and simulations.