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

In C++: A common operation that is performed on arrays, especially in audio and

ID: 3779926 • Letter: I

Question

In C++:

A common operation that is performed on arrays, especially in audio and image applications, is called decimation. Decimation is the under sampling of an array by a constant factor. As an example, consider the input array:

45, 12, 76, 35, 23, 19, 8, 100, -4, 5, 8, 11, 15, 22, 90

If we decimate this array by a factor of 2, we keep every other element. In this case, we would get:

12, 35, 19, 100, 5, 11, 22

If we decimate the array instead by a factor of 3, we keep every third element. In this case, we would get:

76, 19, -4, 11, 90

Write a function with the following prototype that performs decimation:

double* decimate_array(double* arr, int DecimationFactor, int arr_size, int& output_size);

Here, arr is an input array, DecimationFactor is the number of indices to skip (this would be 2 and 3 in the respective examples above), arr_size is the size of the input array, and output_size is the length of the output array (you need to fill in its value). Your function should dynamically allocate an output array that is large enough to hold the decimated array elements, which is then to be returned as a pointer.

You do not need to write a main function.

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;
double* decimate_array(double* arr, int DecimationFactor, int arr_size, int& output_size)
{
   double* output_arr=new double[1000];

   for(int i=DecimationFactor-1;i<arr_size;i=i+DecimationFactor)
   {
      
       output_arr[output_size++]=arr[i];
      
      
   }
   return output_arr;
}

int main(int argc, char const *argv[])
{
   double arr[15]={45, 12, 76, 35, 23, 19, 8, 100, -4, 5, 8, 11, 15, 22, 90};
   int DecimationFactor=3;
   int arr_size=15;
   int output_size=0;
   double* output_arr= decimate_array(arr,DecimationFactor,arr_size,output_size);

   for (int i = 0; i < output_size; ++i)
   {
      cout<<output_arr[i]<<" ";
   }


   return 0;
}

====================================================================

akshay@akshay-Inspiron-3537:~/Chegg$ g++ decimate.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
76 19 -4 11 90