Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Using C Programming Language) Write a function that will take an integer array

ID: 3809798 • Letter: #

Question

(Using C Programming Language) Write a function that will take an integer array and its size as inputs. This function shall separate the odd and even integers from the original array into two separate arrays, and return these arrays, along with their sizes, to main. The original array and two separate arrays shall be displayed in main. The function must use only pointers, no array indeces. Test this function with at least two different example arrays. Hint: use the keyword sizeof to determine the size of a given array, e.g. n = sizeof(array) / sizeof(int).

Explanation / Answer

Please find the program below.


#include <stdio.h>

   // values to be returned from the function.
struct values
   // EAR - even array
   // OAR - odd array.
   // j - corresponds to number of valid elements in EAR.
   // k - corresponds to number of valid elements in OAR.
     
   long int EAR[20], OAR[20];
   int j,k;
   };

   // seperateODDEVEN seperates the ODD and EVEN integers from the input array.
struct values seperateODDEVEN(long int ARR[], int size){
  
       // retValues will hold all the values to be returned.
struct values retValues;
retValues.j=0,retValues.k=0;
  
       /* Copy odd and even elements into their respective arrays */
       int i,n=size;
  
       for (i = 0; i < n; i++)
{
if (ARR[i] % 2 == 0)
{
retValues.EAR[retValues.j] = ARR[i];
retValues.j++;
}
else
{
retValues.OAR[retValues.k] = ARR[i];
retValues.k++;
}
}

       return retValues;
}   

   // main function
void main()
{
long int ARR[10], OAR[10], EAR[10];
int i, j = 0, k = 0, n;

printf("Enter the size of array AR ");
scanf("%d", &n);

printf("Enter the elements of the array ");
for (i = 0; i < n; i++)
{
scanf("%ld", &ARR[i]);
fflush(stdin);
}

       // call the function to seperate.
       struct values ot = seperateODDEVEN(ARR,n);

       printf(" The elements of OAR are ");

       for (i = 0; i < ot.k; i++)
       {
           printf("%ld ", ot.OAR[i]);
       }

       printf(" The elements of EAR are ");
       for (i = 0; i < ot.j; i++)
       {
           printf("%ld ", ot.EAR[i]);
       }
}

OUTPUT:
$ gcc seperate.c
$ ./a.out
Enter the size of array AR
5
Enter the elements of the array
1 2 3 4 5

The elements of OAR are
1
3
5

The elements of EAR are
2
4