In: Computer Science
Create a program in C that performs the following tasks:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 10
void mergeArrays( int *var1, int *var2)
{
int n1= 2*ARRAY_SIZE; //To double the size of new array
int arrnew[n1];
int x1 = ARRAY_SIZE; //To process 2nd array from newarray's index n, bcz there are already n elements.
int index = 0;
for(int i = ARRAY_SIZE-1 ; i>=0 ;--i){
arrnew[index++] = var2[i];
}
for(int i = ARRAY_SIZE-1 ; i>=0 ;--i){
arrnew[index++] = var1[i];
}
//To sort an array
for (int i = 0; i < n1; ++i)
{
for (int j = i + 1; j < n1; ++j)
{
if (arrnew[i] > arrnew[j])
{
int tempval = arrnew[i];
arrnew[i] = arrnew[j];
arrnew[j] = tempval;
}
}
}
//To print new sorted array
printf("\nValues in new sorted array are:");
for(int x=0; x<n1; x++)
{
printf("%d ", arrnew[x]);
}
printf("\n");
printf("\n");
}
void fillArray(int arr[]){
for (int i=0;i<ARRAY_SIZE;i++){
//To generate random number within 100
int randno =rand() % 100 + 1; //Includes 1 and 100
arr[i]=randno;
}
}
int main()
{
//Variable declarations
int arr1[ARRAY_SIZE]; //array1
int arr2[ARRAY_SIZE]; //array2
int min,max,randno; //To generate random number
//The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand()
srand (time(NULL) );
fillArray(arr1);
//Fill array with random number
printf("Values in array1 are:");
for(int x=0; x<ARRAY_SIZE; x++)
{
printf("%d ",arr1[x]) ;
}
printf("\n");
fillArray(arr2);
//Fill array with random number
printf("Values in array2 are:");
for(int x=0; x<ARRAY_SIZE; x++)
{
printf("%d ",arr2[x]) ;
}
printf("\n");
//Call merge array fuction to merge and sort array
mergeArrays(arr1, arr2);
return 0;
}
======================================
SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern. Please UPVOTE