Arrays

1. Introduction to Arrays

Definition:
An array is a collection of elements, all of the same type, stored in contiguous memory locations. It provides a way to store multiple items of the same data type in a single variable, allowing for effic…


This content originally appeared on DEV Community and was authored by Jayaprasanna Roddam

1. Introduction to Arrays

Definition:

An array is a collection of elements, all of the same type, stored in contiguous memory locations. It provides a way to store multiple items of the same data type in a single variable, allowing for efficient indexing and access.

Characteristics of Arrays:

  • Fixed Size: Once an array is created, its size cannot be changed (static arrays).
  • Homogeneous Elements: All elements in an array are of the same type.
  • Random Access: Elements can be accessed directly using their index.
  • Memory Layout: Arrays are stored in contiguous memory, allowing efficient access.

2. Accessing Array Elements

Syntax:

In most programming languages, array elements are accessed using an index that starts from 0. For example, in Go:

arr := []int{10, 20, 30, 40, 50}
fmt.Println(arr[0]) // Output: 10

Example Problem:

Given an array of integers, return the first and last elements.

func firstAndLast(arr []int) (int, int) {
    return arr[0], arr[len(arr)-1]
}

// Example usage
arr := []int{1, 2, 3, 4, 5}
first, last := firstAndLast(arr)
// Output: first = 1, last = 5

3. Insertion and Deletion in Arrays

Insertion:

  • At the End: Inserting an element at the end of the array is straightforward (if there's space).
  • At a Specific Index: Requires shifting elements to make space.

Deletion:

  • From the End: Simply remove the last element.
  • From a Specific Index: Requires shifting elements to fill the gap.

Example Problem:

Implement a function to insert an element at a specific index in an array.

func insertAtIndex(arr []int, index int, value int) []int {
    arr = append(arr[:index], append([]int{value}, arr[index:]...)...)
    return arr
}

// Example usage
arr := []int{1, 2, 3, 4, 5}
arr = insertAtIndex(arr, 2, 99)
// Output: arr = [1, 2, 99, 3, 4, 5]

4. Traversal of Arrays

Definition:

Traversal is the process of accessing each element of an array exactly once for processing. It is commonly done using loops.

Example Problem:

Write a function to print all elements of an array.

func printArray(arr []int) {
    for _, value := range arr {
        fmt.Println(value)
    }
}

// Example usage
arr := []int{1, 2, 3, 4, 5}
printArray(arr)
// Output: 1 2 3 4 5

5. Static vs. Dynamic Arrays

Static Arrays:

  • Size is fixed at compile time.
  • Memory is allocated on the stack.
  • Example: int arr[5]; in C/C++.

Dynamic Arrays:

  • Size can be determined at runtime.
  • Memory is allocated on the heap.
  • In languages like Go, dynamic arrays are implemented using slices.

Example Problem:

Compare the usage of static and dynamic arrays.

// Static array (size fixed at compile time)
var staticArr [5]int

// Dynamic array (size determined at runtime)
dynamicArr := make([]int, 0)
dynamicArr = append(dynamicArr, 1, 2, 3)


This content originally appeared on DEV Community and was authored by Jayaprasanna Roddam


Print Share Comment Cite Upload Translate Updates
APA

Jayaprasanna Roddam | Sciencx (2024-10-09T14:22:00+00:00) Arrays. Retrieved from https://www.scien.cx/2024/10/09/arrays-3/

MLA
" » Arrays." Jayaprasanna Roddam | Sciencx - Wednesday October 9, 2024, https://www.scien.cx/2024/10/09/arrays-3/
HARVARD
Jayaprasanna Roddam | Sciencx Wednesday October 9, 2024 » Arrays., viewed ,<https://www.scien.cx/2024/10/09/arrays-3/>
VANCOUVER
Jayaprasanna Roddam | Sciencx - » Arrays. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/09/arrays-3/
CHICAGO
" » Arrays." Jayaprasanna Roddam | Sciencx - Accessed . https://www.scien.cx/2024/10/09/arrays-3/
IEEE
" » Arrays." Jayaprasanna Roddam | Sciencx [Online]. Available: https://www.scien.cx/2024/10/09/arrays-3/. [Accessed: ]
rf:citation
» Arrays | Jayaprasanna Roddam | Sciencx | https://www.scien.cx/2024/10/09/arrays-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.