Dynamic Memory Allocation Assignment
Dynamic Memory Allocation Assignment
510519056
1. Write a program in C to dynamically allocate space in memory for n
number
of integers where n is the input at runtime. Then find the maximum and
minimum of these numbers.
Program:
#include<stdio.h>
#include<conio.h>
#include"alloc.h"
void main()
int n,max,min,*p,i;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Enter a number");
scanf("%d",&p[i]);
max=p[0];
min=p[0];
for(i=1;i<n;i++)
{
if(p[i]>max)
max=p[i];
if(p[i]<min)
min=p[i];
getch();
Screenshot 1
Screenshot 2
Screenshot 3
2. Write a program in C to dynamically allocate space for an m × n
matrix where
the value of the number of rows m and number of columns n will be
taken as
input. Store the base addresses of each of the rows in another array
of pointers
where the size of the array is exactly m. Use another pointer to keep
track of
the base address of this array of pointers.
Program:
#include<stdio.h>
#include<conio.h>
#include"alloc.h"
void main()
int i,j,r,c,**a;
clrscr();
scanf("%d",&r);
scanf("%d",&c);
for(i=0;i<r;i++)
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("\n");
getch();
Screenshot 1
Screenshot 2
Screenshot 3