In: Computer Science
C Programming
build a function that is outside of main domain but can be called
Build a function that will :
- Take 3 arrays and their lengths as input: Array 1 for even
numbers, Array 2 for odd numbers. Both 1 and 2 have the same
size
- put all elements in array 3
elements from array 1 should be placed in the even indexes and
elements of array 2 should be placed in the odd indexes, in
increasing order of even and odd.
array 3 should have an even and odd sequence in increasing order
but not their combined sequence.
Example : INPUT: array 1=[2,4,6] , array2=[1,3,5], array 3=[]
OUTPUT: array 3=[2,1,4,3,6,5]
Build another function to:
- sort the elements in array 3 from lower to higher
- find the median
- delete the median value if it is included in the array and
repeat.
If the median does not exist in the array (ie a float) the program
should execute.
#include<stdio.h>
int arr3[100],even[100],odd[100];
int array(int even[],int odd[],int arr3[],int size)
{
int j=0,i,count;
for(i=0;i<size;i++)
{
if(j%2==0)
arr3[j]=even[i];
j++;
if(j%2==1)
i--;
}
j=1;
for(i=0;i<size;i++)
{
if(j%2==1)
arr3[j]=odd[i];
j++;
if(j%2==0)
i--;
}
for(count=0;count<(j-1);count++)
{
printf("\n%d\t",arr3[count]);
}
j--;
return j;
}
void sort(int arr3[],int j)
{
int i,k,temp,count;
for(i=0;i<j-1;i++)
{
for(k=0;k<j-i-1;k++)
{
if (arr3[k]>arr3[k+1])
{
temp=arr3[k];
arr3[k]=arr3[k+1];
arr3[k+1]=temp;
}
}
}
printf("\nSorted array\n");
for(i=0;i<j;i++)
{
printf("%d\t",arr3[i]);
}
int mindex=j/2;
int mindex2=mindex+1;
int median=(mindex+mindex2)/2;
for(i=0;i<j;i++)
{
if(arr3[i]==median)
{
count=i;
break;
}
}
for(i=count;i<j;i++)
{
arr3[i]=arr3[i+1];
}
printf("\n");
for(i=0;i<j-1;i++)
{
printf("%d\t",arr3[i]);
}
}
int main()
{
int size,i;
printf("Enter size of array ");
scanf("%d",&size);
printf("\nEnter even array ");
for(i=0;i<size;i++)
{
scanf("%d",&even[i]);
}
printf("\nEnter odd array ");
for(i=0;i<size;i++)
{
scanf("%d",&odd[i]);
}
int j=array(even,odd,arr3,size);
sort(arr3,j);
}
/*OUTPUT:
Enter size of array 3
Enter even array 2
4
6
Enter odd array 1
3
5
2
1
4
3
6
5
Sorted array
1 2 3 4 5 6
1 2 4 5 6
--------------------------------
Process exited after 4.852 seconds with return value 0
Press any key to continue . . .
*/
EXPLANATION:
We take input in main then we call the function to join the arrays.
Firstly, we take j=0. In loop 1 we check if j is even as j%2=0. If the condition is true then we copy the element of even array to arr3 else if j%2==1, then we decrease value of I by 1. We do so because when j%2==1 then also value of I is increased and in every loop when the value is to be copied, the program will skip one value and copy another. Hence, we decrease value of I by 1. Same logic goes for loop 2, here, when j%2==1, which is if j is odd then the odd array value gets copied otherwise value of I is decreased. We get our final array.
To sort the array we have used bubble sorting technique.
To find the median:
We know that size of even array and odd array is always same. So when we will combine the two arrays, the resultant array will always have an even size. So, we will always get two mid values. We store first value in mindex and second in mindex2. We find the average of two value. Then we compare, we keep on copying the value to another array unless the value is found. Once the value is found, we break the flow ad move out of the loop and store that index. In second loop we copy the elements 1 position ahead of the actual position.