Write a function add echo that adds an echo to the samples array Your function s
ID: 3721975 • Letter: W
Question
Write a function add echo that adds an echo to the samples array Your function should take four parameters: the samples array the size of the samples array the sampling rate (as an int) and the delay d in seconds (as a double), in this order The function will not return any value. For each sample in the samples array, add the value from d seconds ago. Examples add echo ([5, 8, 3, 10, 6, 2],6,1,1) Sampling rate is 1 (meaning 1 sample per second) and the delay is 1 second. For the echo, we need to add the value from 1 second before which means the previous position. [5, 8, 3, 10, 6, 2] [5, 8+5 3+8, 10+3, 6+10, 2+6] [5, 13, 11, 13, 16, 8] add echo ([5, 8, 3, 10, 6, 2],6,1,2) Sampling rate is 1 (meaning 1 sample per second) and the delay is 2 seconds. For the echo, we need to add the value from the 2 seconds ago. Given the sampling rate of 1 sample per second, this means we need to add the value from two positions before ([5, 8, 3, 10, 6, 2] , 6, 1,2) [5, 8, 3+5, 10+8, 6+3, 2+10] [5, 8, 8, 18, 9, 12] add echo ([5, 8, 3, 10, 6, 2],6, 2, 2) Sampling rate is 2 (meaning 2 samples per second) and the delay is 2 seconds. For the echo, we need to add the value from the 2 seconds ago. Given the sampling rate of 2 samples per second, this means we need to add the value from four positions before ([5, 8, 3, 10, 6, 2] , 6, 2, 2) [5, 8, 3, 10, 6+5 2+8 [5, 8, 3, 10, 11, 10] add echo ([5, 8, 3, 10, 6, 2],6, 2,1.5) Sampling rate is 2 (meaning 2 samples per second) and the delay is 1.5 seconds. For the echo, we need to add the value from the 1.5 seconds ago. Given the sampling rate of 2 samples per second, this means we need to add the value from three positions before ([5, 8, 3, 10, 6, 2] , 6, 2, 1.5) [5, 8, 3, 10+5,6+8, 2+3] [5, 8, 3, 15, 14, 5] Note: the values of the highlighted samples will stay unchanged from their original valueExplanation / Answer
#include<iostream>
using namespace std;
void add_echo(int arr[], int n, int r, double d)
{
int arr1[100]; // Declared an array to store the new values of the previous array
if (n <= 1)
return;
for (int i=0; i<(double(r)*d); i++)
{
arr1[i] = arr[i];
}
for(int i=(r*d);i<n;i++)
{
arr1[i]=arr[i]+arr[i-int((double(r)*d))];
}
for (int i=0; i<n; i++)
cout << arr1[i] << " ";
}
// main method to check for one of the given test cases.
int main()
{
int arr[] = {5, 8, 3, 10, 6, 2};
int n = sizeof(arr)/sizeof(arr[0]);
int r=2;
double d=1.5d;
add_echo(arr, n,r,d);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.