Code:
#include <stdio.h>
#include <stdlib.h>
int array_input ( double user[] );
void array_sort ( double user[], int limit );
int array_combined ( double A[], double B[], double C[], int a_loop, int b_loop );
int main()
{
int limit = 1000; // limit of array size
double A[limit], B[limit], C[ 2 * limit]; // 1,2, and 3 arrays
int junk = 0, loop, a_loop, b_loop, c_loop; // loop and array variables
double *puser, *puser_B, *puser_C; // pointer declaration
char yes; // user controlled loop
// user controlled loop
while (junk == 0 )
{
printf("Enter the numbers in the first array.\n");
fflush(stdin);
// function call for user input of first array
puser = &A;
a_loop = array_input( puser );
printf("Enter the numbers in the second array.\n");
fflush(stdin);
// function call for user input of second array
puser = &B;
b_loop = array_input( puser );
// function call to sort first array
printf("\nThe first set of numbers sorted in descending order are:\n");
puser = &A;
array_sort( puser, a_loop );
// function call to sort second array
printf("\nThe second set of numbers sorted in descending order are:\n");
puser = &B;
array_sort( puser, b_loop );
// function call to combine arrays
puser = &A;
puser_B = &B;
puser_C = &C;
c_loop = array_combined ( puser, puser_B, puser_C, a_loop, b_loop );
// function call to sort combined array
array_sort( puser_C, c_loop );
printf("\n");
printf("\nThe combined loop is:\n");
for ( loop=0; loop < c_loop; loop++)
printf("%.1f\n",C[loop]);
printf("Would you like to run the program again? y for yes\n");
fflush(stdin);
scanf("%c", &yes);
if (yes != 'y')
exit(0);
}
return 0;
}
// input array
int array_input ( double user[] )
{
int loop = 0, x, finished; // looping variable
char yes; // user controlled variable
while ( finished == 0) {
while (scanf("%lf",&user[loop]))
{
loop++;
}
printf("Do you want to stop entering numbers? y for yes.\n");
fflush(stdin);
scanf("%c", &yes);
if ( yes == 'y' ) {
finished = 1;
}
if ( yes != 'y')
printf("Enter the next number >");
}
printf("The numbers you entered were:\n");
for ( x=0; x < loop; x++)
printf("%.1f\n",user[x]);
return (loop);
}
void array_sort ( double user[], int limit )
{
int loop,x; // loop variables
double temp; // temp variable
// set limit of array size
loop = limit;
// shorten array after largest moved to end
for ( x = 1 ; x < loop ; x++ )
{
// loop to move highest number to end of array
for (limit = 0; limit < loop - x ; limit++)
{
if ( user[limit] < user[limit+1] )
{
temp = user[limit+1];
user[limit+1] = user[limit];
user[limit] = temp;
}
}
}
// print sorted array
for ( x=0; x < loop; x++)
printf("%.1f\n",user[x]);
}
// function to combine arrays
int array_combined ( double A[], double B[], double C[], int a_loop, int b_loop )
{
int x=0, y=0, z=0,w; // loop variables
// combine arrays to point where size of arrays are equal
while(x < a_loop && y < b_loop )
{
if(A[x] <= B[y])
{
C[z]=A[x];
x++;
z++;
}
else
{
C[z]=B[y];
z++;
y++;
}
}
// if A is larger, add in remaining elements of A
while( x < a_loop )
{
C[z]=A[x];
x++;
z++;
}
// if B is larger, add in remaining elements of B
while( y < b_loop )
{
C[z]=B[y];
z++;
y++;
}
// eliminate duplicates
for( x = 0; x < z; x++ )
{
for( y = x + 1; y < z; )
{
if( C[y] == C[x])
{
for( w = y; w < z ; w++ )
C[w] = C[w+1];
z--;
}
else
y++;
}
}
return(z);
}