Question

In: Computer Science

C Programming build a function that is outside of main domain but can be called Build...

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.

Solutions

Expert Solution

#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.


Related Solutions

Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
C++ Programming-Need getGrades function (I keep getting errors). Write a function called getGrades that does the...
C++ Programming-Need getGrades function (I keep getting errors). Write a function called getGrades that does the following: Take an array of integers and an integer representing the size of the array as parameters. Prompt the user to enter up to 20 grades. Store the grades in the array. Return the number of grades entered. Write another function called calcStats that does the following: Take an array of integers and an integer representing the size of the array as normal parameters....
C# Programming create a Hash Function
C# Programming create a Hash Function
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts...
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts any base(startBase) to another (endBase) by first converting the start base to decimal then to the end base. Do not use library. (bases 1-36 only)
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program accepts inputs from the user from the console. The user can provide the following two types of input: add num1 num2 mult num1 num2 Here, num1 and num2 are two integer numbers. E.g., the user may input add 1 2. The threads receive the command along with the number, and performs the appropriate arithmetic operation and returns the results to main program. The main...
in phyton programming: with numpy Create a function called biochild.  The function has as parameters...
in phyton programming: with numpy Create a function called biochild.  The function has as parameters the number m and the lists ??????h?? and ??????h??.  The lists ??????h?? and ??????h?? contain 0’s and 1’s.  For example: ??????h?? = [1,0,0,1,0,1] and ??????h?? = [1,1,1,0,0,1]  Both lists have the same length ?.  The 0's and 1's represent bits of information (remember that a bit is 0 or 1).  The function has to generate a new list (child)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT