Lecture 2 - Static Array PDF

Document Details

UserReplaceableOrphism376

Uploaded by UserReplaceableOrphism376

Zagazig University

Dr. Mahmoud Alnamoly

Tags

static arrays data structures c++ programming computer science

Summary

This lecture provides a comprehensive overview of static arrays in C++. It covers the fundamental concepts of static arrays including characteristics, different types (1D, 2D, multi-dimensional), and operations. Code snippets and examples, in C++, are included for practical application.

Full Transcript

02 Static Array Dr. Mahmoud Alnamoly Dr. Mahmoud Alnamoly Static Array ? An array is a basic data structure used to store a fixed-size collection of elements of the same type. These elements are arranged in contiguous memory locations, allowing each element to be indexed or accesse...

02 Static Array Dr. Mahmoud Alnamoly Dr. Mahmoud Alnamoly Static Array ? An array is a basic data structure used to store a fixed-size collection of elements of the same type. These elements are arranged in contiguous memory locations, allowing each element to be indexed or accessed directly using an integer index. Data structure arrays are essential because they form the basis for many other data Dr.structures, such as stacks, queues,Tree and array-based lists. Mahmoud Alnamoly Static Array Characteristics ✓ Fixed Size: Once an array is declared, its size cannot be changed during runtime. You need to know the number of elements you will store in the array beforehand. Memory is allocated on the stack. The allocation is done at compile time, and the size is fixed. ✓ Homogeneous Elements: All elements in an array must be of the same data type, such as all integers, all floats, or all characters. ✓ Indexed by Integers: Each element in an array is assigned a unique integer called an index, which identifies its position within the array. Indexing usually starts at 0. ✓ Efficient Access: Because of the way arrays are stored in memory (contiguously), accessing any element by its index is very efficient. This direct access using the index is often referred to as "random access." Dr. Mahmoud Alnamoly Different types of arrays One-Dimensional Arrays (1D Arrays) These are the simplest form of arrays, consisting of a single line of elements. Each element in a 1D array is accessed by a single index representing its position within the array. Dr. Mahmoud Alnamoly Different types of arrays Two-Dimensional Arrays (2D Arrays) A two-dimensional array in data structure, often thought of as a matrix, consists of rows and columns. It is used to represent data in a grid format and is accessed by two indices: one for the row and another for the column. Dr. Mahmoud Alnamoly Different types of arrays Multi-Dimensional Arrays These are the arrays with more than two dimensions. The multi-dimensional arrays in data structures can be used in scenarios requiring complex data representation. Dr. Mahmoud Alnamoly Array Declaration // Declaring 1D static array of integers with size 5 int numbers = { 1, 2, 3, 4, 5 }; // Declaring and initializing a 2D array int arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Dr. Mahmoud Alnamoly Different Array Operations Arrays support a variety of operations that allow for the manipulation and interaction with the data they hold: ✓ Access Elements in Array ✓ Insert Element in Array ✓ Delete an Element From Array ✓ Search for an Element in Array ✓ Update Value of Existing Element in Array ✓ Sort Array Elements Dr. Mahmoud Alnamoly 1. Access Elements in 1D Array #include using namespace std; int main() { // Declaring a static array of integers with size 5 int numbers = { 1, 2, 3, 4, 5 }; // Accessing elements of the array for (int i = 0; i < 5; i++) { cout

Use Quizgecko on...
Browser
Browser