Moving Average 4 a Write a function of the form void movingA

Moving Average

4. (a) Write a function of the form:

void movingAverage(int n, float x[], float y[], int m)

which takes in an n-dimensional array x and computes a moving average of the m previous terms in x; that is

1

yi = --- xi + xi – 1 + + xi m + 1m

You can set the first m – 1 terms of y to 0.

(b) Write a function

void weightedMovingAverage(int n, float x[], float y[], int m, float w[])

that includes a set of weights for each term:

1

yi = --- w0xi + w1xi – 1 +   + wm – 1xi m + 1

m

Note that this computation is the basis of digital filtering.

7. Write the following functions involving padding.

(a) void padBinary(int N, int binArray[], int numBits), which given a binary array (inBin[] of dimension N ) and total number of desired bits numBits, returns the zero-padded array. For example, if N = 5, binArray = [1 0 1 1 0], and numBits = 8, then binArray = [0 0 0 1 0 1 1 0].

(b) int unpadBinary(int N, int binArray[]), which given a binary array of dimension N, removes any leading 0’s. The number of digits that were removed is returned.

Write the function int bin2dec(int N, int binArray[]), which given a binary array, returns the equivalent decimal value.

(a) Write a program (you can put everything in main) which given a decimal number, creates the table which shows the steps for how it is converted to decimal.

(b) Turn your program into a function called int dec2bin(int dec, int binArray[]) which given a decimal number, sets up the corresponding binary array. Also, it returns (as an int) the number of bits contained in binArray[].

10. Write the functions:

void bin2hex(int N, int binArray[], char hexArray[])

void hex2bin(int N, int binArray[], char hexArray[])

Hint: In bin2hex, first pad with zeros so that the number of bits is a multiple of 4.

Permutation

5. (a) Write a function of the form:

int isPresentInArray(int n, int x[], int value)

which given an array x[] of length n, checks to see if value is present as one of the elements. If value is present return 1; otherwise, return 0.

(b) Write a function of the form void randperm(int n, int array[]), which computes a random permutation of the integers 0 ... n - 1 and stores the results in array[].

3

Number Systems

6. Write the following functions involving a single hex digit.

(a) char dec2hexNibble(n), which given an integer n (where 0 n 15 ), returns the corresponding hex character (single digit). Note: getHex(7) returns the character \'7\', not the number 7!

int hex2decNibble(char c), which given a hex character (0 - F), returns the corresponding decimal value

void hex2binNibble(char c, int binArray[]), which given a hex character, returns the corresponding 4-bit binary number (in binArray).

char bin2hexNibble(int binArray[]), which given a 4-bit binary number, returns the corresponding hex digit.

7. Write the following functions involving padding.

(a) void padBinary(int N, int binArray[], int numBits), which given a binary array (inBin[] of dimension N ) and total number of desired bits numBits, returns the zero-padded array. For example, if N = 5, binArray = [1 0 1 1 0], and numBits = 8, then binArray = [0 0 0 1 0 1 1 0].

(b) int unpadBinary(int N, int binArray[]), which given a binary array of dimension N, removes any leading 0’s. The number of digits that were removed is returned.

Write the function int bin2dec(int N, int binArray[]), which given a binary array, returns the equivalent decimal value.

(a) Write a program (you can put everything in main) which given a decimal number, creates the table which shows the steps for how it is converted to decimal.

(b) Turn your program into a function called int dec2bin(int dec, int binArray[]) which given a decimal number, sets up the corresponding binary array. Also, it returns (as an int) the number of bits contained in binArray[].

10. Write the functions:

void bin2hex(int N, int binArray[], char hexArray[])

void hex2bin(int N, int binArray[], char hexArray[])

Hint: In bin2hex, first pad with zeros so that the number of bits is a multiple of 4.

Solution

Here is the solution for the first 2 problems:

void movingAverage(int n, float x[], float y[], int m)
{
for(int i = 0; i < m-1; i++)   //Initializes the first m elements to 0.
y[i] = 0;
for(int i = m-1; i < n; i++)   //From mth element to nth(last) element.
{
y[i] = x[i];       //Copy mth element to array y.
for(int j = i; j >= i-m; j--) //For elements from m to j to i-m.
y[i] += x[j];               //Add the m elements in x to y.
}
}
void weightedMovingAverage(int n, float x[], float y[], int m, float w[])
{
for(int i = 0; i < m-1; i++)   //Initializes the first m elements to 0.
y[i] = 0;
for(int i = m-1; i < n; i++)   //From mth element to nth(last) element.
{
y[i] = w[i] * x[i];       //Copy mth element multiplied to its weight to array y.
for(int j = i; j >= i-m; j--) //For elements from m to j to i-m.
y[i] += x[j] * w[j];               //Add the m weighted elements in x to y.
}
}

Moving Average 4. (a) Write a function of the form: void movingAverage(int n, float x[], float y[], int m) which takes in an n-dimensional array x and computes
Moving Average 4. (a) Write a function of the form: void movingAverage(int n, float x[], float y[], int m) which takes in an n-dimensional array x and computes
Moving Average 4. (a) Write a function of the form: void movingAverage(int n, float x[], float y[], int m) which takes in an n-dimensional array x and computes

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site