0
1.1kviews
Comment on dynamic memory allocation. Write a program to read and store N integers in an array, where value of N is defined by user. Find minimum and maximum numbers from the array.
1 Answer
1
29views

Dynamic Memory Allocation

  • Dynamic memory allocation manages system memory at runtime.
  • Dynamic memory allocation in c language enables the C programmer to allocate memory at runtime.
  • Dynamic memory allocation uses heap space of the system memory.
  • Dynamic memory allocation in C language is performed by using 4 functions that come under the standard C library header file stdlib.h
    • malloc() function - It allocates single block of requested memory.
    • calloc() function - It allocates multiple block of requested memory.
    • realloc() function - It reallocates the memory occupied by malloc() or calloc() functions.
    • free() function - It frees the dynamically allocated memory.

C Program

#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 200
void main()
{
int array[MAX_SIZE];
int a, max, min, n;
clrscr();
printf("Enter the total number of elements an Array contains:");
scanf("%d", &n);
printf("Enter elements of an Array one by one:");
for(a=0; a<n; a++)
{
scanf("%d", &array[a]);
}
max = array[0];
min = array[0];
for(a=1; a<n; a++)
{
if(array[a] > max)
{
max = array[a];
}
if(array[a] < min)
{
min = array[a];
}
}
printf("Maximum Element in the Array = %d\n", max);
printf("Minimum Element in the Array = %d\n", min);
getch();
}

OUTPUT:

Enter the total number of elements an Array contains:10

Enter elements of an Array one by one:34

67

34

12

267

3

56

23

45

22

Maximum Element in the Array = 267

Minimum Element in the Array = 3

Please log in to add an answer.