C Write a function that takes an array Arr of size N and ret

C++:
Write a function that takes an array Arr of size N and returns another array which, first disregards the numbers greater than a certain threshold (also a passed parameter), and then stores the remaining elements of the array Arr first in an increasing order and then in a decreasing order
Example: If Arr = {5,4,1,12,0,2} and threshold = 10, the returned array should be = {0,1,2,4,5,5,4,2,1,0}
In the main method, the user is asked to enter the size of array Arr, the value of the threshold, as well as all the elements of array Arr.
Notes and hints:
Write an iterative merge sort method to do the sorting part.
You may need to define additional functions.
The size of the returned array is dynamic (depending on the number of eliminated elements). Be careful how you return such an array.

Solution

//array format e program
#include <iostream>
#include <string>

void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */
int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];

/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}

/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;

// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}

int* filterFunction(int a[],int threshold,int size)
{
int oCou = 0;
for(int p=0;p<size;p++)
   {
   if(a[p]>threshold)
   continue;
   else
   oCou+=1;
   }
//std:: cout<<oCou<\"\ \";
  
int temp[oCou];
int count=0;
//remove elemets above threshold
for(int j=0;j<size;j++)              
   {
   if(a[j]>threshold)
   continue;
   else
   temp[count++]=a[j];
   }
  
/*       for(int i=0;i<count;i++)
   {
   std:: cout<<temp[i];
  
  
   }
*/  
   //sort array
   mergeSort(temp, 0, count - 1);
  
   //build new array with having both asceding and descending order elemetsnfrom sorted array
   bool flag=true;
   int tcount = 0;
   int finalArrCount = count*2;
   int finalarr[finalArrCount];
   for(int k=0;k<finalArrCount;k++)
   {
   if(tcount == count)
   flag = false;
   if(flag)
   {
   // std::cout<<temp[tcount++];
   finalarr[k]=temp[tcount];
   // std:: cout<<\"flag true:\"<<temp[tcount]<<\"count:\"<<tcount<<\"finalArrcount\"<<finalArrCount;
   tcount++;
         

   }
   else
   {
   tcount--;
   finalarr[k]=temp[tcount];
     
         
   }
  
   }
  
/*           for(int i=0;i<finalArrCount;i++)
   {
//   std:: cout<<finalarr[i]<<\"\ \";
  
  
   }

*/   int * tarr=finalarr;
   return tarr;
}


int main() {
int sizearray=0;
   int threshold=0;
   std::cout<<\"enter no of elements to be entered\";
   std::cin>>sizearray;
//   std::cin >> sizearray;
   int arr[sizearray];
   std::cout<<\"enter numbers\";
   for(int i=0;i<sizearray;i++)
   {
   std::cin>>arr[i];
  
   }
  
  
   std::cout<<\"enter threshold\";
   std::cin>>threshold;
   int tsize = 0;
for(int p=0;p<sizearray;p++)
   {
   if(arr[p]>threshold)
   continue;
   else
   tsize+=1;
   }
   int* p;
p=filterFunction(arr,threshold,sizearray);

   for(int i=0;i<tsize*2;i++)
   {
  
  
   std:: cout<<\"\ \"<<*(p+i); // print formatted array
   // std:: cout << \"i:\" << i << \"tsize: \"<<tsize;
   }
   return 0;
}

C++: Write a function that takes an array Arr of size N and returns another array which, first disregards the numbers greater than a certain threshold (also a p
C++: Write a function that takes an array Arr of size N and returns another array which, first disregards the numbers greater than a certain threshold (also a p
C++: Write a function that takes an array Arr of size N and returns another array which, first disregards the numbers greater than a certain threshold (also a p
C++: Write a function that takes an array Arr of size N and returns another array which, first disregards the numbers greater than a certain threshold (also a p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site