Assignment 2: Dynamic memory allocation in C.
- Dynamic Memory Allocation is manual allocation and freeing of memory according to your programming needs.
- Dynamic memory is managed and served with pointers that point to the newly allocated memory space in an area which we call the heap.
- Now you can create and destroy an array of elements dynamically at runtime without any problems. To sum up, the automatic memory management uses the stack, and the C Dynamic Memory Allocation uses the heap.
- The <stdlib.h> library has functions responsible for Dynamic Memory Management.
- malloc() :- Allocates the memory of requested size and returns the pointer to the first byte of allocated space.
Set A . Write C programs for the following problems.
1. Write a program to allocate memory dynamically for n integers such that the memory is initialized to 0. Accept the data from the user and find the range of the data elements.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, max;
int *ptr;
// Input maximum elements of array
printf("Enter total number of elements: ");
scanf("%d", &max);
// Allocate memory for 'max' integer elements and initialized to 0
ptr = (int *) calloc(max, sizeof(int));
// If memory not allocated
if(ptr == NULL)
{
printf("Memory is not created!!!");
exit(0); // Exit from the program
}
// Input elements from user
printf("Enter %d elements: \n", max);
for (i = 0; i < max; i++)
scanf("%d", (ptr + i));
// Print all elements
printf("\nArray elements are:\n");
for (i = 0; i < max; i++)
printf("%d ", *(ptr + i));
// Release allocated memory
free(ptr);
return 0;
}
2. Accept n integers in an array. Copy only the non-zero elements to another array (allocated using dynamic memory allocation). Calculate the sum and average of non-zero elements.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int count;
int *arr ,*arr1;
int sum = 0;
float avg=0.0;
printf("Enter the total number of elements you want to enter : ");
scanf("%d", &count);
arr = (int *)malloc(count * sizeof(int));
for (i = 0; i < count; i++)
{
printf("Enter element %d : ", (i + 1));
scanf("%d", arr + i);
}
for (i = 0; i < count; i++)
{
if(*(arr+i)!=0)
*(arr1+i)=*(arr+i);
}
for (i = 0; i < count; i++)
{
sum += *(arr1 + i);
}
printf("sum is %d \n", sum);
avg=sum/count;
printf("Avg is %d \n", avg);
free(arr);
return 0;
}
Set B . Write C programs for the following problems.
1. Accept the number of rows (m) and columns (n) for a matrix and dynamically allocate memory for the matrix. Accept and display the matrix using pointers. (Hint: Use an array of pointers.)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int **a, row,col,i,j,t=0;
printf("Enter Limit for Rows : ");
scanf("%d",&row);
printf("\nEnter Limit for Columns : ");
scanf("%d",&col);
a=(int **)malloc(row*sizeof(int*));
for(i=0;i<row;i++)
{
a[i]=(int *)malloc(col*sizeof(int));
}
printf("\nEnter Elements for Matrix of Size %d*%d:\n\n",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n%d*%d Matrix : \n\n",row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%3d ",a[i][j]);
}
printf("\n");
}
return 0;
}
No comments:
Post a Comment