Professional Documents
Culture Documents
1:-
1.1 Write a program to display n number of elements.Memory should be
allocated dynamically using malloc( )
#include<stdio.h>
#include<stdlib.h>
void L_search(int *a,int n,int search)
{
int i;
for(i=0;i<n;i++)
{ if(a[i]==search)
{ printf("\n%d is found\n",search);
break;
}
}
if(i==n)
printf("\nSearched item is not present is array\n9");
}
void main()
{
int *a,n,i,search;
printf("\nenter the number of elements\n");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
printf("\nenter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nenter the element to search\n");
scanf("%d",&search);
L_search(a,n,search);
}
1.2 Write a program to display n number of elements. Memory
should be allocated dynamically using calloc( ).
#include<stdio.h>
#include<stdlib.h>
void L_search(int *a,int n,int search)
{
int i;
for(i=0;i<n;i++)
{ if(a[i]==search)
{ printf("\n%d is found\n",search);
break;
}
}
if(i==n)
printf("\nSearched item is not present is array\n9");
}
int main()
{
int *a,n,i,search;
printf("\nenter the number of elements\n");
scanf("%d",&n);
a=(int*)calloc(n,sizeof(int));
printf("\nenter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nenter the element to search\n");
scanf("%d",&search);
Linear_search(a,n,search);
}
1.3 Write a program to allocate memory using malloc( ) and then
reallocate the previously allocated memory using realloc( ). Display the
elements which have been taken after reallocation.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a,n,i,search;
printf("\nenter the number of elements\n");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
printf("\nenter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int *n1;
n1=(int*)realloc(a,n*sizeof(int));
for(i=0;i<n;i++)
{
printf("%d",n1[i]);
}
}