Quick actions

cmd+k|ctrl+k

Navigation

Languages

increase_size

Snippet info

Language

Cpp

Visibility

public

Author

umashankar

Created

2021-04-23T05:50:26.710479Z

Updated

2021-04-23T06:29:43.106292Z

// Increasing the array size 

#include <iostream>
using namespace std;

int main() {
    cout << "Enter the array size";
    int n;
    cin >> n;
    int *arr1 = new int[n]
    for(int i=0; i<n; ++i){
        cout << "Enter element";
        cin >> *(arr1+i);
    }
    
    cout << "Do you wanna increase the size of the array: (true/false)";
    // for bool in c use <stdbool.h> c++ has bool datatype independently    
    bool val;
    cin >> val;
    cout << "Enter the new size of the array: ";
    int n1;
    cin >> n1;
    int *arr2 = new int[n1];
    memcpy(arr2,arr1,n);
    // delete operator deallocates a block of memory allocated in heap
    delete []arr1;
    arr1 = arr2; // now arr1 also pointing to same array in heap;
    // removing arr2 pointing to array
    arr2 = nullptr;
    
    return 0;
}
INFO