Write a program to find sum of first and last element of an array demonstrating use of new and delete operator.
Sep 2, 2019Source code:
//program to find the sum of first and last element of //an array, using new and delete operators #include <iostream> using namespace std; int main(){ float *aptr, sum;// pointer to allocate memory for an array int n; // reading size of an array //read array size cout<<"Enter the size of an array:"; cin>>n; aptr = new float[n]; //allocate memory cout<<"Enter array elements:"; for(int i = 0; i < n; i++){ cin>>*(aptr + i); //reading array elements } sum = *(aptr) + *(aptr + n -1); cout<<"Sum of first and last emem="<<sum<<endl; return 0; }
Sample run:
Enter the size of an array:5 Enter array elements:1.1 2.2 3.3 4.4 5.5 Sum of first and last emem=6.6