You are on page 1of 26

Assignment No.

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

1.4 Write a program to allocate memory using calloc( ) 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*)calloc(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]);
}
}

1.5 Write a program to allocate memory dynamically, print n number


of characters and then release the allocated memory using free( ).
#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]);
for(i=0;i<n;i++)
printf("%d\t",a[i]);
free(a);
}

1.6 Write a C program to search an element in an Array using dynamic


memory allocation.
#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,i+1,i);
break;
}
}
if(i==n)
printf("\nSearched item is not present is array\n9");
}
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 elements to search\n");
scanf("%d",&search);
L_search(a,n,search);

1.7 Write a C program to find the3rd maximum element in an array


using dynamic memory allocation
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void swap(int *p1,int *p2)
{ int t=*p1;
*p1=*p2;
*p2=t;
}
void third_largest(int *a,int n)
{ int i,j;
for(i=0;i<n;i++)
{ for(j=0;j<n-1-i;j++)
{ if(a[j]<a[j+1])
swap(&a[j],&a[j+1]); } }
printf("\n The third largest element is: %d\n",a[2]);
}
void main()
{ int *a,n,i;
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("\nthe elemtnts u inserted are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
third_largest(a,n);}

1.8 Write a C program to find the minimum element in an array using


dynamic memory allocation.
#include<stdlib.h>
#include<math.h>
void min(int *a,int n)
{ int i,temp,j;
for(i=0;i<n;i++)
{ for(j=0;j<n-1-i;j++)
{ if(a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp; }
}
}
printf("\nMinimum number of the array is:%d\n",a[0]);
}
void main()
{
int *a,n,i;
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("\nthe elemtnts u inserted are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
min(a,n);}

1.9 Write a C program to search an element in a 2D-Array using


dynamic memory allocation.
#include<stdio.h>
#include<stdlib.h>
#define row 2
#define col 2
void search(int **arr1,int temp)
{
int i,j,flag=0,sum=0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
sum++;
if(temp==arr1[i][j])
{
printf("Element at pos %d",sum);
flag=1; }
}
}
if(flag==0)
{
printf("not found");
}
}
int main()
{
int **arr1,i,j,key;
arr1=(int**)malloc(row*sizeof(int));
for(i=0;i<row;i++)
{
arr1[i]=(int*)malloc(col*sizeof(int));
}
printf("\nEnter four numbers:-\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&arr1[i][j]);
}
}
printf("Enter the element you want to search =");
int temp;
scanf("%d",&temp);
search(arr1,temp);
return 0;
}
1.10 Write a C program to find the maximum element in a 2D-array
using dynamic memory allocation.
#include<stdio.h>
#include<stdlib.h>
#define row 2
#define col 2
void highest(int **arr1)
{
int i,j,first;
first=arr1[0][0];
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(arr1[i][j]>first)
{
first=arr1[i][j]; }
}
}
printf("\nMaximum value is:-%d\n",first);
}
int main()
{
int **arr1,i,j;
arr1=(int**)malloc(row*sizeof(int));
for(i=0;i<row;i++)
{
arr1[i]=(int*)malloc(col*sizeof(int));
}
printf("\nEnter four numbers:-\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&arr1[i][j]);
}
highest(arr1);
return 0;
}

1.11 Write a C program to find the minimum element in a 2D-array


using dynamic memory allocation.
#include<stdio.h>
#include<stdlib.h>
#define row 2
#define col 2
void min(int **arr1)
{
int i,j,min;
min=arr1[0][0];
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(arr1[i][j]<min)
min=arr1[i][j];}}
printf("\nMaximum value is:-%d\n",min);
}
int main()
{
int **arr1,i,j;
arr1=(int**)malloc(row*sizeof(int));
for(i=0;i<row;i++)
{
arr1[i]=(int*)malloc(col*sizeof(int));
}
printf("\nEnter four numbers:-\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&arr1[i][j]);
}
min(arr1);
return 0;
}

1.12 Write a C program to merge two sorted dynamic array.


#include<stdio.h>
#include<stdlib.h>
void merge(int *a1,int *a2,int n1,int n2)
{
int *mrg,n,k=0,i=0,j=0;
n=n1+n2;
mrg=(int*)malloc(n*sizeof(int));
while(i<n1 && j<n2)
{
if(a1[i]<a2[j])
{
mrg[k++]=a1[i++];
}
else
{
mrg[k++]=a2[j++];
}
}
while(i<n1)
{
mrg[k++]=a1[i++];
}
while(j<n2)
{
mrg[k++]=a2[j++];
}
printf("\nAfter merging two arrays:- ");
for(i=0;i<n;i++)
printf("%d\t",mrg[i]);
}
void sort(int *a,int n)
{
int i,j,k,temp,min,count=1;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main()
{
int *a1,*a2,n1,n2,i=0;
printf("\nEnter the size of first array:- ");
scanf("%d",&n1);
a1=(int*)malloc(n1*sizeof(int));
printf("\nEnter %d elements:-\n",n1);
for(i=0;i<n1;i++)
scanf("%d",&a1[i]);
sort(a1,n1);
printf("\nEnter the size of second array:- ");
scanf("%d",&n2);
a2=(int*)malloc(n2*sizeof(int));
printf("\nEnter %d elements:-\n",n2);
for(i=0;i<n2;i++)
scanf("%d",&a2[i]);
sort(a2,n2);
merge(a1,a2,n1,n2);
return 0;
}
1.13 Write a C program to merge two unsorted dynamic array in sorted
order.
#include<stdio.h>
#include<stdlib.h>
void merge(int *a1,int *a2,int n1,int n2)
{
int *mrg,n,k=0,i=0,j=0;
n=n1+n2;
mrg=(int*)malloc(n*sizeof(int));
while(i<n1 && j<n2)
{
if(a1[i]<a2[j])
{
mrg[k++]=a1[i++];
}
else
{
mrg[k++]=a2[j++];
}
}
while(i<n1)
{
mrg[k++]=a1[i++];
}
while(j<n2)
{
mrg[k++]=a2[j++];
}
printf("\nAfter merging two arrays:- ");
for(i=0;i<n;i++)
{
printf("%d\t",mrg[i]);
}
}
void sort(int *a,int n)
{
int i,j,k,temp,min,count=1;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main()
{
int *a1,*a2,n1,n2,i=0;
printf("\nEnter the size of first array:- ");
scanf("%d",&n1);
a1=(int*)malloc(n1*sizeof(int));
printf("\nEnter %d elements:-\n",n1);
for(i=0;i<n1;i++)
scanf("%d",&a1[i]);
sort(a1,n1);
printf("\nEnter the size of second array:- ");
scanf("%d",&n2);
a2=(int*)malloc(n2*sizeof(int));
printf("\nEnter %d elements:-\n",n2);
for(i=0;i<n2;i++)
scanf("%d",&a2[i]);
sort(a2,n2);
merge(a1,a2,n1,n2);
return 0;
}

1.14 Write a C program to delete a range of data from a dynamic array.


#include<stdio.h>
#include<stdlib.h>
void delete1(int *arr,int n1,int pos1)
{
int i;
for(i=pos1;i<n1-1;i++)
{
arr[i]=arr[i+1];
}
for(i=0;i<n1-1;i++)
{
printf("%d\t",arr[i]);
}
}
void main()
{
int *a,i,n,pos,nw;
printf("enter the number of elements\n");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
printf("enter the %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the position to delete\n");
scanf("%d",&pos);
pos--;
delete1(a,n,pos);
}

1.16 Write a C program to program to find the3rd maximum element in


a 2D array using dynamic memory allocation.
#include<stdio.h>
#include<stdlib.h>
#define row 2
#define col 2
void third_highest(int **arr1)
{
int i,j,second,first,third;
second=arr1[0][0];
first=arr1[0][0];
third=arr1[0][0];
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(arr1[i][j]>first)
{
third=second;
second=first;
first=arr1[i][j];
}
else if(arr1[i][j]>second)
{
third=second;
second=arr1[i][j];
}
else if(arr1[i][j]>third)
{
third=arr1[i][j];
}
}
}
printf("\nThird highest value is:-%d\n",third);
}
int main()
{
int **arr1,i,j;
arr1=(int**)malloc(row*sizeof(int));
for(i=0;i<row;i++)
{
arr1[i]=(int*)malloc(col*sizeof(int));
}
printf("\nEnter four numbers:-\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&arr1[i][j]);
}
}
third_highest(arr1);
return 0;
}

1.17 Write a C program to store the ages in a 2D dynamic array.Every


row must have a specific range. At the time of taking input data will go
to a specific position of a row if it is blank otherwise display full
message.
#include<stdio.h>
#include<stdlib.h>
int row=0;
int col=0;
void range(int **arr1,int temp)
{
int i,j,first,min;
min=arr1[0][0];
first=arr1[0][0];
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(arr1[i][j]>first)
{
first=arr1[i][j];
}
if(arr1[i][j]<min)
{
min=arr1[i][j];
}
}
}
if(temp>=min && temp<=first)
{
printf("\Age is valid");
}
else{
printf("\Age is not valid");
}
}
int main()
{
int **arr1,i,j,key;
printf("\nEnter the num of rows and cols:-\n");
scanf("%d%d",&row,&col);
arr1=(int**)malloc(row*sizeof(int));
for(i=0;i<row;i++)
{
arr1[i]=(int*)malloc(col*sizeof(int));
}
for(i=0;i<row;i++)
{
printf("Enter the range for row %d ",i+1);
for(j=0;j<col;j++)
scanf("%d",&arr1[i][j]);
}
printf("Enter Age =");
int temp;
scanf("%d",&temp);
range(arr1,temp);
return 0;
}

1.18 Write a C program to store multiple name in a 2D dynamic array


and then count the length of the different name.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define row 2
#define col 2
void print(char **arr1)
{
int i,j,flag=0,sum;
char *temp;
temp=(char*)malloc(10*sizeof(char));
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
temp=arr1[i][j];
sum=strlen(temp);
printf("%s -> %d",arr1[i][j],sum);
}
}
if(flag==0)
printf("not found");
}
int main()
{
char **arr1,key;
int i,j;
arr1=(char**)malloc(row*sizeof(char*));
for(i=0;i<row;i++)
{
arr1[i]=(char*)malloc(col*sizeof(char));
}
printf("\nEnter four names:-\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%s",&arr1[i][j]);
}
print(arr1);
return 0;
}
1.19 Write a C program to store the week name in an array efficiently
and then print them.
#include<stdio.h>
#include<stdlib.h>
int main()
{
char **a;
int n,i;
printf("enter the size of the array\n");
scanf("%d",&n);
a=(char**)malloc(n*sizeof(char));
for(i=-0;i<n;i++)
{
*a=(char*)malloc(50*sizeof(char));
}
char
wk[7][11]={"Sunay","Monday","Tuesday","Wednesday","Thursday","Friday","Saturda
y"};
int count=7;
if(count>=n)
{
for(i=0;i<count;i++)
{
a[i]=&wk[i];
}
for(i=0;i<count;i++)
{
printf("%s",a[i]);
}
}
else
{
count=n;
for(i=0;i<count;i++)
{
a[i]=&wk[i];
}
for(i=0;i<count;i++)
printf("%s",a[i]);
}
}

1.20 Write a C program to declare n number of dynamic 1D array and


then combine them to generate a dynamic 2d array. Also display the
same.
#include<stdio.h>
#include<stdlib.h>
#define col 3
int main()
{
int **arr1,i,j,n;
printf("\nEnter the number of 1D array:-\n");
scanf("%d",&n);
arr1=(int**)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
arr1[i]=(int*)malloc(col*sizeof(int));
}
for(i=0;i<n;i++)
{
printf("\nEnter data for array no %d:-\n",i+1);
for(j=0;j<col;j++)
scanf("%d",&arr1[i][j]);
}
for(i=0;i<n;i++)
{
printf("Data in array %d ",i+1);
for(j=0;j<col;j++)
printf("%d ",arr1[i][j]);
printf("\n");}
}

You might also like