Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

One Dimensional Dynamic Arrays

Firstly, allocating, we make a pointer of our array at the stack that containes the address of the first element of our array.

Secondly,we make a array at heap by the new keyword.

Thirdly,perform your tasks.

At last, dellocating, delete the array values at the heap by delete keyword and make the pointer of our array nullptr so its points to nothing.

#include <iostream>

using namespace std;

int main()
{
    int size = 5;
    int sum=0;

    //Allocating the memory
    int *arr = new int[size]{1,2,3,4,5};

    //sum of array
    for(int i=0;i<size;i++){
        sum+=arr[i];
    }
    cout<<"The sum of all the elements in the array is = "<<sum<<endl;

    //Delloacting the memory
    delete[] arr;
    arr = nullptr;

    return 0;
}

How to change the size of our array at run-time or at execution time? How to increase or decrease the size of our array dynamically or at execution time?

Firstly, we create our array, the size we want to change dynamically.

Secondly, we create a temporary(temp) dyanmic 1D-Array, the size of which is that we want after change meaning that size+increaseNum or size+decreaseNum .

Thirdly, copy the array to tempArray.

At Last, delete the array values from heap and mutate the array-pointer to the tempArray-pointer. No,w you can mutate the size as size+=num (for-increaing) or size-=num(for-decreaing)

Both our array and tempArray pointers in the stack point to the same changed tempArray values in the heap.

Now,You can perform tasks to the changed sized array. After performing methods, deallocate the aray and program happily finishes😊.(Problem-Solved!Hurray🥳)

#include <iostream>
using namespace std;
int main()
{

    int size = 5;
    int *arr = new int[size]{1, 2, 3, 4, 5};

    int num;
    cout << "Enter the number how much size of array you wnat to change :" << endl;
    cin >> num;

    int *temp = new int[size + num];
    for (int i = 0; i < size; i++)
    {
        temp[i] = arr[i];
    }

    //deleting the previous array
    delete[] arr;
    arr=temp;

    //giving values to increased indexes
    for(int i=size;i<size+num;i++){
        cin>>arr[i];
    }
    size+=num;

    delete[] arr;
    arr=nullptr;

    return 0;
}