Pages

Thursday, June 27, 2013

C++ CODE FOR DYNAMIC MEMORY ALLOCATION (DMA)

QUESTION:
Write a program to allocate memory locations for an array of ‘int’ type using ‘new‘ operator.

The program should input values for the length of array during program execution. Enter values

into array and find maximum values of an array.

Note: You have to release the memory using delete operator before the end of program.
 
 


CODE:

#include <iostream>
using namespace std;
int main(){
int size;
cout<<"Enter the size of the array\n";
cin>>size;
int *ptr = new int[size];
cout<<"\nEnter the values in the array\n\n";

for(int i=0;i<size;i++){
cin>>ptr[i];
}
int max = ptr[0];
for(i=0;i<size;i++){
 if(ptr[i] > max){
    max = ptr[i];
 }

}
cout<<"Maximum value of array is : "<<max<<endl<<endl;
delete [] ptr;
ptr = NULL;

 return 0;
}

 

No comments:

Post a Comment