Dynamic Memory Allocation, Array

Task 1: Using the new operator, allocate an integer array of user-specified size (the user gives the size of the array as input). Assign values to the array elements by taking user inputs and then print the values. Finally de-allocate the array using the delete operator.


#include <iostream>
using namespace std;

int main()
{
    int i,n;
    int *p;
    cout << "Give an integer size array: ";
    cin >> i;
    cout << endl;

    p = new int[i];

    for(n=0; n<i; n++)
    {
        cout << "Array[" << n+1 << "] number: ";
        cin >> p[n];
    }

    cout << "\nYour Given Number: ";
    for(n=0; n<i; n++)
    {
        cout << p[n] << ", ";
    }

    delete[] p;
    return 0;
}



Task 2: Using the new operator, allocate a two-dimensional character array. Again the number of rows and columns are going to be provided by the user as input. All of the rows are the same size. Take character strings as input from the user and then print the strings. Finally de-allocate the array using the delete operator.


#include <iostream> using namespace std; int main() { int row,column; cout<<"Enter the size of row: "; cin>>row; cout<<endl<<"Enter the size of column: "; cin>>column; int **Array=new int*[row]; for(int i=0; i<row; i++) { Array[i]=new int[column]; } cout<<endl<<"Enter the elements of the array: "<<endl<<endl; for(int i=0; i<row; i++) { for(int j=0; j<column; j++) { cout<<"Array["<<i<<"]"<<"["<<j<<"]: "; cin>>Array[i][j]; } } cout<<endl<<endl; cout<<".............................................The Matrix................................................"<<endl<<endl; for(int i=0; i<row; i++) { for(int j=0; j<column; j++) { cout<<"\t\t"; cout<<Array[i][j]<<" "; } cout<<endl<<endl; } for(int i=0; i<row; i++) { delete []Array[i]; } delete []Array; return 0;
}


Task 3: Using the new operator, allocate a two-dimensional integer array. The number of rows and columns are going to be provided by the user as input. However, in this task, all of the rows are not the same size (the array is uneven). The user will specify how many elements the individual rows will have. Assign values to the array elements by taking user inputs and then print the values. Finally de-allocate the array using the delete operator.


#include<iostream> using namespace std; int main(){ int row, column; int *array = new int[500]; int **matrix; cout << "How many rows you want: "; cin >> row; cout << endl; matrix = new int*[row]; for (int i = 0; i < row; i++) { // user specify the elements in the individual rows int a; cout << "\nHow many elements want to store in Row[" << i+1 << "]: "; cin >> a; matrix[i] = new int[a]; array[i] = a; cout << "Please, enter the " << array[i] << " elements of Row[" << i+1 << "] =>" << endl; for (int j = 0; j < array[i]; j++) { // assigning the values to the Array cout << "Array [" << i+1 << "][" << j+1 << "]: "; cin >> matrix[i][j]; } } // printing the array cout<<endl<<endl; cout<<"----------> The Matrix <----------" << endl << endl; for (int i = 0; i < row; i++) { for (int j = 0; j < array[i]; j++) { cout << "\t" << matrix[i][j] << " "; } cout << endl; } // de-allocation for (int i = 0; i < row; i++) { delete[] matrix[i]; } delete[] array; delete[] matrix; return 0; }



Useful Links: