Name: Anonymous 2012-03-14 10:51
Got this assignment, just wondering if my code is right!
The functions you are to implement are as follows:
double average(double f[], int size)
Compute and return the average of the values in the array.
double deviation(double f[], int size)
Compute and return the standard deviation of the sample using the values in the array.
void hiPass(double signal[], int size)
A simple hiPass filter removes the DC from a signal. The DC is just the average value of the signal so hiPass just subtracts the average from the elements in signal[]. Remember that arrays are effectively passed by reference.
void reverse(char data[], int size)
Rearrange the values in an array so that the first element contains the original value of the last element, the second element contains the original value of the second last element, and so on, until the last element contains the original value of the first element. Remember that arrays are effectively passed by reference.
and the code i wrote is...
/** average ***********************************************************
*
* @params - f[] -- the given array
* - size -- size of the array
*
* @pre - numbers -- any real number
* - size -- any integer greater than 0
*
* @returns - the average of n numbers in the array
*
* **************************************************************************/
double average(double f[], int size) {
double sum = 0.;
for (int i = 0; i < size; i++)
sum += f[i];
return sum/size;
}
/** deviation *************************************************************
*
* @params - f[] -- the given array
* - size -- the size of the array
*
* @pre - numbers -- any real number
* - size -- any integer greater than 0
*
* @return - the deviation of n numbers in the array
*
* ************************************************************************/
double deviation(double f[], int size) {
double avg = average (f,size);
double sum=0;
for (int i=0; i<size; i++)
sum+=(f[i]-avg)*(f[i]-avg);
sum/=size;
return sqrt(sum);
}
/** hiPass *************************************************************
*
* @params - signal[] -- the given array
* - size -- the size of the array
*
* @pre - numbers -- any real number
* - size -- any integer greater than 0
*
* @return - the deviation of each number in the array
*
* ************************************************************************/
void hiPass(double signal[], int size){
double avg = average(signal,size);
for (int i = 0; i < size; i++)
signal[i]-= avg;
}
/** reverse *************************************************************
*
* @params - data[] -- the given array
* - size -- any integer
*
* @pre - numbers -- any real number
* - size -- any integer greater than 0
*
* @return - the reverse of the chose numbers in the array
*
* ************************************************************************/
void reverse(char data[], int size){
char temp;
for (int i = 0; i < (size/2); i++){
temp = data[i];
data[i] = data[size-1-i];
data[size-1-i] = temp;
}
}
The functions you are to implement are as follows:
double average(double f[], int size)
Compute and return the average of the values in the array.
double deviation(double f[], int size)
Compute and return the standard deviation of the sample using the values in the array.
void hiPass(double signal[], int size)
A simple hiPass filter removes the DC from a signal. The DC is just the average value of the signal so hiPass just subtracts the average from the elements in signal[]. Remember that arrays are effectively passed by reference.
void reverse(char data[], int size)
Rearrange the values in an array so that the first element contains the original value of the last element, the second element contains the original value of the second last element, and so on, until the last element contains the original value of the first element. Remember that arrays are effectively passed by reference.
and the code i wrote is...
/** average ***********************************************************
*
* @params - f[] -- the given array
* - size -- size of the array
*
* @pre - numbers -- any real number
* - size -- any integer greater than 0
*
* @returns - the average of n numbers in the array
*
* **************************************************************************/
double average(double f[], int size) {
double sum = 0.;
for (int i = 0; i < size; i++)
sum += f[i];
return sum/size;
}
/** deviation *************************************************************
*
* @params - f[] -- the given array
* - size -- the size of the array
*
* @pre - numbers -- any real number
* - size -- any integer greater than 0
*
* @return - the deviation of n numbers in the array
*
* ************************************************************************/
double deviation(double f[], int size) {
double avg = average (f,size);
double sum=0;
for (int i=0; i<size; i++)
sum+=(f[i]-avg)*(f[i]-avg);
sum/=size;
return sqrt(sum);
}
/** hiPass *************************************************************
*
* @params - signal[] -- the given array
* - size -- the size of the array
*
* @pre - numbers -- any real number
* - size -- any integer greater than 0
*
* @return - the deviation of each number in the array
*
* ************************************************************************/
void hiPass(double signal[], int size){
double avg = average(signal,size);
for (int i = 0; i < size; i++)
signal[i]-= avg;
}
/** reverse *************************************************************
*
* @params - data[] -- the given array
* - size -- any integer
*
* @pre - numbers -- any real number
* - size -- any integer greater than 0
*
* @return - the reverse of the chose numbers in the array
*
* ************************************************************************/
void reverse(char data[], int size){
char temp;
for (int i = 0; i < (size/2); i++){
temp = data[i];
data[i] = data[size-1-i];
data[size-1-i] = temp;
}
}