0% found this document useful (0 votes)
104 views6 pages

Dynamic Memory Allocation Assignment

The document describes two C programs that use dynamic memory allocation. The first program allocates space for n integers input by the user, stores the integers, and finds the maximum and minimum values. The second program allocates space for an m x n matrix where m and n are input, stores values in the matrix, and prints the output. Both programs take input, allocate memory dynamically, store values, perform calculations, and output results.

Uploaded by

Sourin Satpathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views6 pages

Dynamic Memory Allocation Assignment

The document describes two C programs that use dynamic memory allocation. The first program allocates space for n integers input by the user, stores the integers, and finds the maximum and minimum values. The second program allocates space for an m x n matrix where m and n are input, stores values in the matrix, and prints the output. Both programs take input, allocate memory dynamically, store values, perform calculations, and output results.

Uploaded by

Sourin Satpathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SANDIP SATPATHI

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();

printf("Enter No. of digits:");

scanf("%d",&n);

p=(int*)calloc(n,2);//dynamic memory allocation

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];

printf("Maximum=%d Minimum=%d\n",max,min);//printing output

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();

printf("Enter the number of rows:");

scanf("%d",&r);

printf("Enter the number of columns:");

scanf("%d",&c);

*a=(int**)malloc(r*sizeof(int*));//dynamic memory allocation

for(i=0;i<r;i++)

a[i]=(int*)malloc(c*2);//dynamic memory allocation

for(i=0;i<r;i++)

{
for(j=0;j<c;j++)

printf("Enter the number at(%d,%d):",i+1,j+1);

scanf("%d",&a[i][j]);

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf("%4d ",a[i][j]);//printing output

printf("\n");

getch();

Screenshot 1
Screenshot 2

Screenshot 3

You might also like