*C++ Write a function normalize to normalize a sound. Its parameters should be t
ID: 3723092 • Letter: #
Question
*C++
Write a function normalize to normalize a sound. Its parameters should be the same as those for reverse() . Normalizing the sound samples array is a two step process. Find the maximum (highest) value in the samples array. Multiply all values in the samples array by 36773 and divide by the maximum value.
Examples
[0, 1, 2, 3] --> [0, 1*36773/3, 2*36773/3, 3*36773/3]
[3, 2, 1, 0] --> [3*36773/3, 2*36773/3, 1*36773/3, 0]
[5, 8, 10] --> [5*36773/10, 8*36773/10, 10*36773/10]
This is the reverse function:
void reverse(int* samples, int size)
{
int i=0, j=size-1;
while(i<=j)
{ int temp = samples[i];
samples[i] = samples[j];
samples[j] = temp; i++;
j--;
}
}
Explanation / Answer
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
using namespace std;
int max(int* samples,int size){
int m=samples[0];
int i;
for( i=1;i<size;i++){
if(m<samples[i]){
m=samples[i];
}
}
return m;
}
void normalize(int* samples,int size){
int i=0;
int m = max(samples,size);
for(i=0;i<size;i++){
samples[i] = (samples[i]*36773)/m;
cout<<samples[i]<<" ";
}
cout<<endl;
}
int main()
{
int a[]={0,1,2,3};
int size=sizeof(a)/sizeof(a[0]);
normalize(a,size);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.