Advertisement Remove all ads

Write a Program to Create Two Integer of Size 8 and 7. Initialize the Arrays with Random Values. Sort the Arrays in Ascending Order with the Help of User Defined Function Namely “Sort Array”. - Structured Programming Approach

Answer in Brief

Write a program to create two integer of size 8 and 7. Initialize the arrays with random values. Sort the arrays in ascending order with the help of user defined function namely “sort array”. Merge these arrays with the help of another user defined function named “merge arrays” which returns a new array. Program should display the arrays before and after sorting, also the merged arrays.

Advertisement Remove all ads

Solution

#include<conio.h>
#include<stdio.h>
void main()
{
int a[25],b[25],sum[50],i,j,k=1,n,m,s,temp;
clrscr();
printf("Enter the number of element in first array :");
scanf("%d",&n);
printf("\nEnter the element of array :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\nEnter the number of element in second array :");
scanf("%d",&m);
printf("\nEnter the element of array :\n");
for(i=1;i<=m;i++)
scanf("%d",&b[i]);
s=m+n;
for(i=1;i<=s;i++)
{
if(i<=n)
{
sum[i]=a[i];
}
else
{
sum[i]=b[k];
k=k+1;
}
}
printf("\n Array before sorting is\n");
for(i=1;i<=s;i++)
printf("%d\t",sum[i]);
for(i=1;i<=s;i++)
{
for(j=1;j<=s;j++)
{
if(sum[i]<=sum[j])
{
temp=sum[i];
sum[i]=sum[j];
sum[j]=temp;
}
}
}
printf("\nElement of array after sorting is :\n");
for(i=1;i<=s;i++)
printf("%d\t",sum[i]);
getch();
}

Output:
Enter the number of elements in first array: 8
Enter the element of array:
1 4 7 8 9 22 66 11
Enter the number of elements in second array: 7
33 55 88 14 16 18 79
Array before sorting is
1 4 7 8 9 22 66 11 33 55 88 14 16 18 79
Array after sorting is
1 4 7 8 9 11 14 16 18 22 33 55 66 79 88

Concept: Array
  Is there an error in this question or solution?
Advertisement Remove all ads

APPEARS IN

Advertisement Remove all ads
Advertisement Remove all ads
Share
Notifications

View all notifications


      Forgot password?
View in app×