0% found this document useful (0 votes)
8 views3 pages

1D Arrays

Uploaded by

i232092
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

1D Arrays

Uploaded by

i232092
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1D Arrays

#include <iostream>
using namespace std;

------------------------------> Arrays <------------------------------

-> What is Array?


-> Declaring and Initializing.
-> Accessing Array.

----------------------------------------------------------------------

int main() {

Array is collection of similar data elements grouped under one Name.

---------------------------------------------------------------------------
----------------> Array Declaration and Initialization <-------------------
---------------------------------------------------------------------------

int A[5]; // Declare Array of 5 size ---> Static Array ---> Garbage Values

A[2] = 5; // Array Acessing ---> using subscript [] operator

int arr[5] = { 2,4,6,8,10 }; // Array initializing

int arr1[5] = { 2,4 }; // Remaining values are initialized with 0

int arr2[5] = { 1,2,3,4,5,6 }; // Error -> Out of Range

int arr2[] = { 1,2,3,4,5,6 }; // Size is determined automatically

cout << &arr[0] << endl; // Address of an Array

---------------------------------------------------------------------------
----------------------> Finding Array Length <----------------------------
---------------------------------------------------------------------------

int length_of_array = sizeof(arr) / sizeof(arr[0]); // Finding length


cout << length_of_array << endl; // 20/4 = 5

---------------------------------------------------------------------------
-----------------------> Array Traversing <--------------------------------
---------------------------------------------------------------------------

Traversing of Array ---> For Loop


Traversing means -> |"Visiting all elements once"|

for (int i = 0; i < 5; i++) {


cout << arr[i] << endl;
or
cout << i[arr] << endl;
or
cout << *(arr + i) << endl;
}
---------------------------------------------------------------------------
-------------------> Ways to Access Array Elements <----------------------
---------------------------------------------------------------------------

Ways of Accessing particular element in an Array

1. A[2]
2. 2[A]
3. *(A+2)

---------------------------------------------------------------------------
--------------------> Static VS Dynamic Array <----------------------------
---------------------------------------------------------------------------

--------------> For Static Array <--------------------

// size Decided at Compile time


--> Once its created, its size can't be modified.

int arr4[5];

Q: How to increase the size of Array? (Answer after few Lines)

1. Size of Array inside Stack (Static Array) can't be resized

--> because the memory for the array should be contiguous.


--> because we don't know the next memory block is free or not.

2. Size of Array inside Heap (Dynamic Array) can be changed.

--------------> For Dynamic Array <--------------------

1. For Accessing anything from Heap, we must need a pointer.


2. You must deallocate the memory if the memory isn't required otherwise
memory leakage.

int* ptr = new int[5]; // Dynamic Array of size 5 ----> created in Heap
delete[]ptr; // Deallocation of memory ---> returning to heap.

----> Pointer acts as a name of array.

---------------------------------------------------------------------------
----------------> Increasing Size of an Array <----------------------
---------------------------------------------------------------------------

Steps:

1. Create two pointers e.g *p and *q.


2. The second pointer q should be of required Size.
3. Copy all the elements from p to q. (using for loop)
4. Deallocate the p memory.
5. Point p to q. (p and q are pointing to same array).
6. Remove q. i-e make it NULL.
int* p = new int[5];
int* q = new int[10];

for (int i = 0; i < 5; i++) {


q[i] = p[i];
}

delete[]p;
p = q;
q = NULL;

return 0;
}

You might also like