0% found this document useful (0 votes)
4 views

Array Based on Previous PF and Printing Array-1

Uploaded by

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

Array Based on Previous PF and Printing Array-1

Uploaded by

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

ARRAY BASED

ON PREVIOUS
PF AND
PRINTING
ARRAY
Mam Iram
TABLE OF CONTENT
 Array based on previous PF and
Printing array (loop, pointer addresses)
Time and space complexity
§ Passing array by reference
§ Object Array, pointer to (integer,
character and object) array
OOP concepts (static vs Dynamic Array)
in c++
ARRAYS IN C++: CONCEPTS AND PRACTICES
Arrays in C++ are fundamental data
structures used to store collections of
data elements of the same type. This
guide integrates key aspects of arrays
with problem-solving techniques, object-
oriented programming (OOP) principles,
and practical topics in C++.
1. TYPES OF ARRAYS
Static vs Dynamic Arrays
 Static Arrays:
 Size is fixed at compile time.
 Stored in the stack.
 Memory allocation occurs during program
compilation.
 Example:

int static Array[5] = {1, 2, 3, 4, 5};


 Advantages: Simplicity, faster access due to
stack memory.
 Limitations: Size cannot be changed during
runtime, leading to inefficient memory usage.
DYNAMIC ARRAYS:
 Size can be determined at runtime.
 Stored in the heap.
 Created using pointers and new keyword.

Example:
int* dynamic Array = new int[5];
for (int i = 0; i < 5; i++)
{
dynamic Array[i] = i + 1;
}
Advantages: Flexible size and efficient memory
usage.
Limitations: Must manage memory
explicitly using delete[].
OBJECT ARRAYS
Arrays can store objects of a class, enabling
encapsulation and abstraction.
Example:
class Student
{
public:
string name;
int age;
Student(string n, int a) : name(n), age(a) { }
};
Student students[3] = {Student("Alice", 20),
Student("Bob", 22), Student("Charlie", 21)};
POINTERS TO ARRAYS

Integer and Character Arrays:


Pointers can refer to arrays, allowing access
and manipulation.
Example:
int arr[5] = {10, 20, 30, 40, 50};
int* ptr = arr; // Pointer to the array
cout << "First element: " << *ptr << endl; //
Access first element
Character arrays are used for strings in C++.
char str[] = "Hello";
char* charPtr = str;
POINTER TO OBJECT ARRAYS:
Example:
Student* objArray = new Student[2]
{
Student("Dave", 23), Student("Eve", 24)
};
cout << objArray[0].name; // Accessing object through
pointer

delete[] objArray; // Deallocate memory


2. PASSING ARRAYS
Pass by Reference
Passing arrays by reference avoids copying the entire array, improving
efficiency:
void modifyArray(int &arr[5])
{
for (int i = 0; i < 5; i++)
{
arr[i] *= 2;
}
}
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
modifyArray(arr);
}
DYNAMIC ARRAYS BY REFERENCE
For dynamically allocated arrays, pass
pointers:
void modifyArray(int* arr, int size)
{
for (int i = 0; i < size; i++)
{
arr[i] *= 2;
}
}
3. PRINTING ARRAYS

Using Loops
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
Using Pointers
int arr[5] = {1, 2, 3, 4, 5};
int* ptr = arr;
for (int i = 0; i < 5; i++)
{
cout << *(ptr + i) << " ";
}
Printing Addresses
for (int i = 0; i < 5; i++)
{
cout << &arr[i] << " ";
}
4. TIME AND SPACE COMPLEXITY

Time Complexity
Access: O(1) (Direct access using an index).
Traversal: O(n)
Insertion/Deletion:
Static Arrays: O(n) (due to shifting elements).
Dynamic Arrays: O(n) (if resizing is required).
Space Complexity
Static Arrays: Requires pre-allocated memory (O(n)).
Dynamic Arrays: Includes the memory for elements
plus overhead for managing dynamic allocation.
Practical Example
#include <iostream>
using namespace std;
class Product
{ public:
string name;
double price;
Product(string n, double p) : name(n), price(p) {}
void display()
{
cout << "Product: " << name << ", Price: $" << price << endl;
}
};
int main()
{
Product* products = new Product[3]
{
Product("Laptop", 999.99), Product("Phone", 699.99), Product("Tablet",
499.99)
};
for (int i = 0; i < 3; i++)
{
products[i].display();
}
 This program demonstrates a dynamic array of
objects with encapsulated properties and
methods.
 By leveraging C++'s flexibility, you can use
static and dynamic arrays effectively, optimize
memory management, and apply OOP
principles for enhanced modularity and
abstraction.
Any Question
Thanks

You might also like