Friday, March 4, 2011

User-Defined Functions

Develop a top-down modular program that will perform the following tasks:
(a) Read two integer arrays with unsorted elements.
(b) Sort them in ascending order
(c) Merge the sorted arrays
(d) Print the sorted list
Use functions for carrying out each of the above tasks. The main function should have only function calls.

#include <stdio.h>
Sort(int Arr[20],int Arr1[20]);
void Merge(int Arr[10],int Arr1[10]);
Read(int a[20],int b[20]);
int n,Arr[20],Arr1[20];
void main()
{
int i,a[20],b[20];
clrscr();
Read(a,b);
Sort(a,b);
Merge(a,b);
getch();
}
Read(int a[20],int b[20])
{
int i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements of first Unsorted Array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("Enter the elements of second Unsorted Array\n");
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
return(a[20],b[20]);
}
Sort(int Arr[20],int Arr1[20])
{
int i,j,t;
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(Arr[i]>Arr[j])
{
t=Arr[i];
Arr[i]=Arr[j];
Arr[j]=t;
}
if(Arr1[i]>Arr1[j])
{
t=Arr1[i];
Arr1[i]=Arr1[j];
Arr1[j]=t;
}
}
}
printf("First Sorted Array!\n");
for(i=1;i<=n;i++)
printf("%d ",Arr[i]);
printf("\nSecond Sorted Array!\n");
for(i=1;i<=n;i++)
printf("%d ",Arr1[i]);
return(Arr[20],Arr1[20]);
}
void Merge(int Arr[20],int Arr1[20])
{
int i,j,m,t,Arr2[20];
m=n+n;
for(i=1;i<=n;i++)
{
Arr2[i]=Arr[i];
Arr2[n+i]=Arr1[i];
}
for(i=1;i<m;i++)
for(j=i+1;j<=m;j++)
if(Arr2[i]>Arr2[j])
{
t=Arr2[i];
Arr2[i]=Arr2[j];
Arr2[j]=t;
}
printf("\nMerged Sorted Array!\n");
for(i=1;i<=m;i++)
printf("%d ",Arr2[i]);
}

No comments:

Post a Comment