Você está na página 1de 5

Homework 3

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

of the following decimal integers in their binary,


hexadecimal representations.
iii. 16
iv. 1000

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)

2. Write an application that returns the maximum of an integer array


input with argc/argv. The application must include a function with
the following prototype:
int imax(int arr[], int size);
3. Write an application that uppercases a string inpupt with
argc/argv. The application must include a function that uses
pass-by-reference to "return" the string. Use the following
prototype for the function:
void upper(char input[]);
4. Write a function called extrema which takes an integer array as input and
"returns" both its maximum and minimum value. It is up to you to
decide how to return this information, keeping in mind that a
function can only put a single variable in the return statement. Again,
package this as an application that gets input from argc/argv.
5. Write a program that takes a positive integer as input and prints
its bit pattern as output.
(you can skip any leading 0 bits)
ex.
>> write_bits 100
1 1 0 0 1 0 0
(program ends)
>> write_bits 10
1 0 1 0
(program ends)
6. Write a function which shifts the elements of an integer array 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]

(a[0] + a[1] + a[2])/3


(a[1] + a[2] + a[3])/3
(a[2] + a[3] + a[4])/3
= (a[N-3] + a[N-2] + a[N-1])/3

Starting with our running_average program from class, modify the


program to average in groups of m rather than groups of two, where
m is an arbitary integer 1 < m < N. m should be read from stdin
using scanf, while the numbers to be averaged should be read from
argv.

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]

for 1 <= j/2 < j <= N

where N/2 means "integer divide" -- that is, the result is


truncated to the nearest integer. So for example:
a[1] >= a[2]; a[1] >= a[3];
a[2] >= a[4]; a[2] >= a[5];
.
.
This makes more sense if we think of it as a tree with a[1] at the root:
a[1]
/
\
a[2] a[3]
/
\
a[4]
a[5]
Then each "parent" must be greater than or equal to its two
"children". In any case, this turns out to be a very convenient
arrangement for performing further work very efficiently, such as
sorting the array.
Write a main program that arranges an arbitrary list of integers
into a heap. You should take the integers as input from argv and
copy them to a separate array after converting with atoi(). This
copy is wasteful but is easy to avoid if we use more realistic i/o
(such as a binary file). However, that is not the purpose of this
exercise.
example:
% heapify 3 1 2 7 4 0 2
7 4 3 2 0 2 1
To verify that the output is
a[1] >= a[2] && a[1] >= a[3]
a[2] >= a[4] && a[2] >= a[5]
a[3] >= a[5] && a[3] >= a[7]

a
(
(
(

heap,
7 > 4
4 > 2
3 > 2

test
&& 7
&& 4
&& 3

the
> 3
> 0
> 1

relations:
)
)
)

9. Following our model from class, implement a simple event-loop program


with a scripting interface that accepts the following protocol:
- list
- purchase <item> <quantity>
- exit
General Description: The program maintains a catalog of items for sale. A
user may request a listing of the items for sale. A user may also purchase

a speficied quantity of a given item.


Specifics: The program maintains a list of at least five items for
sale. How these lists are stored in the program is your
choice. However, they need not be read from a file -- they may be
hardwired in the program. Each item has an associated name, price, and
quantity (number available). Each student can make up their own items
(since there is a list command, the user can find the items avaible
and attempt to purchase them), and can initialize them however he/she
wishes. If the user types the command "list", the program lists all of
the information on the items for sale (item names, price, and
quantity). If user types the "purchase <itemname> <quantity>" command,
the program deducts the appropriate number of items from its list and
reports some confirmation statement to the user. If the user types
"exit", the program terminates. If the user types anything else, the
program reports some simple error to the user.
Additional Specifications:
These should be obvious, but just in case ...
- A user may not select to purchase an item which doesn't exist
- A user may not purchase a greater number of items for a particular
quantity than what is avaible.
Example session:
STORE > list
Items for sale:
Pinto
$1000
Gremlin $750
ElTorino $500
Pacer
$275
K-Car
$200

100
200
250
300
500

STORE > purchase Pinto 100


Purchase confirmed of 100 Pintos.
STORE > list
Pinto
Gremlin
ElTorino
Pacer
K-Car

$1000
$750
$500
$275
$200

0
200
250
300
500

STORE > hello


Error, must type one of the following:
list
purchase item quantity
exit
STORE > purchase mercedes 1
Sorry, mercedes is not an item sold in this store.
STORE > purchase Pinto 1
Sorry, Pintos are out of stock. Purchase denied.
STORE > exit
goodbye ...

10. Make the program above persistent across invocations by somehow


saving the main data structure to disc. By "persistent", I mean that
the program, when restarted, should begin where it left off, and not
re-initialize the data.

Você também pode gostar