Escolar Documentos
Profissional Documentos
Cultura Documentos
CSPP50101-1
Due Monday Aug 19
1. Numerical representation/Bitwise operations
Please do the following without the aid of a computer:
(submit as p3_1.txt)
a. Write each
octal, and
i. 1
ii. 8
b. Using only one or more bitwise operations (^, ~, &, |, >>, <<),
write a valid C expression which has the effect of multiplying an
arbitrary integer n by 2.
c. What is the effect of the C expression ( n >> 2 ), where n is an
arbitrary integer?
d. What is the result of the following expressions for an arbitrary integer n?
i. n | 0
iii. n & (~n)
ii n | (~n)
iv. n ^ (~n)
places, where n is some integer 0 < |n| < m, where m is the size of
the array. If n is positive, the array is right shifted; if n is
negative, the array is left shifted. Periodic boundary conditions
are used. That is, values pushed off the beginning/end of the
array are wrapped around to the end/beginning. The interface is:
void ishift( int arr[], int size, int n ){ ... }
That is, arr is modified in place using pass-by-reference.
Do _not_ call printf from within the shift function. Include a
simple driver (ie main) that sets up some data using argc/argv,
calls shift, and then prints the resulting shifted array.
You'll have to separate the input data from the shift parameter.
To do this, enter input data first, then separate with / for clarity.
PROMPT>> ./shift 1 2 3 4 5 / -2
Some examples:
a = {1,2,3,4,5,6}; a before shifting
ishift(a,6,-1);
left-shift 1
a = {2,3,4,5,6,1} a after shifting
a = {1,2,3,4,5,6}; a before shifting
ishift(a,6,-2);
left-shift 2
a = {3,4,5,6,1,2} a after shifting
a = {1,2,3,4,5,6}; a before shifting
ishift(a,6,2);
right-shift 2
a = {5,6,1,2,3,4} a after shifting
7. In class we built a simple running_average program that begins with
an array of n floats and outputs an array of n - 1 floats, each of
which is the result of averaging adjacent elements in the original
array. Recall that one element is always lost because of "boundary
effects". Note that nothing is really special about the process of
averaging only two elements at a time; we can easily generalize
this to any other number of elements. For example, let's consider
what averaging in groups of three would look like;
{a[0], a[1], ..., a[N-1]}
<-- original data
{b[0], b[1], ..., b[N-3]} <-- output data
The N-2 output elements are computed as:
b[0] =
b[1] =
b[2] =
b[N-3]
Example:
% running_average 1 2 6 4 3
Enter the size of your averaging width: 3
3.0 4.0 4.33
8. A "heap" is defined as a one-dimensional array with the following property:
a[j/2] >= a[j]
a
(
(
(
heap,
7 > 4
4 > 2
3 > 2
test
&& 7
&& 4
&& 3
the
> 3
> 0
> 1
relations:
)
)
)
100
200
250
300
500
$1000
$750
$500
$275
$200
0
200
250
300
500