Sie sind auf Seite 1von 3

Homework 4

CSPP50101-1
due Monday Sept. 2
General instructions: anywhere I say "write a function that ...", this
implies that you should include a main which gets data from argv and
calls the function.
All file input should take place through stdin and redirection. If you wish
to support direct file reads in addition, that's good but not required.
1. Rewrite the following function using pointer arithmetic in place of
array subscripting, as we discussed at length in class. As a reminder,
p[i] = *(p+i) by definition. You will get no credit if the new
function (ie the one you write) does not give the same results as the
original function. It is up to you to test this. Name your new function
new_mystery.
void mystery( int a[], int n ){
int i, tmp1, tmp2;
tmp1 = a[n-1];
for ( i=0; i<n-1; i=i+2 ){
tmp2 = a[i];
a[i] = tmp1;
tmp1 = a[i+1];
a[i+1] = tmp2;
}
}
Explain in words what this function does.

2. Each of the following snippets contains a common


error related to C pointers. In each case, state
in words the error or general rule that is being
violated. For example, if you were given the snippet
int x = 2.7, an appropriate answer would be "illegal
assignment: a float may not be stored in a variable
of type int".
Note that some of these may compile and execute properly on a given
system. That does not mean they are correct. You may get lucky
from time to time, but it is crucial be able to distuinguish
between what is guaranteed to work and what may work in certain
fortuitous situations.
a) char *p;
p = "hello world";
p[0] = 'H';

e) int i,j;
int* iptr, jptr;
iptr = &i;
jptr = &j;

b) char *p;
*p = 3;

f) char *p;
strcpy(p, "hello world");

c) char p;
char *pptr = &p;
*(pptr+2) = 'c';

g) int *fptr;
fptr = 1;

d) char s[] = "Hello";


s = "goodbye";

h) char *f[100];
f[0] = getchar();

3. Write a function called fmax with the following interface


float* fmax( float arr[], int size )
fmax takes an array of floats and an integer (representing the size of
the array in the usual way), computes the maximum value of arr, and
returns its address. For example, if arr[5] is found to be the maximum
value of arr, then fmax returns &arr[5].
Write a simple main which sets up some data with argv and calls
fmax. Upon receiving the pointer result in main, use this value to
increment by 1 the maximum value of fmax.
4. Write a function which computes the mean and standard deviation of
a float array. use pointers to "return" the values.
void extrema(float arr[], int size, float* mean, float* stdev);
5. Write a function which uses malloc to initialize a pointer to some block
of integers of size n. Use the following interface:
int* arr1d(int size);
6. Redo (5) using pass by reference to "return" the initialized pointer. You'll
have
to decide on what interface this implies.
7. Write a program that reads in a text file to a statically allocated
array of pointers (hint: this does _not_ mean that the pointers
themselves _don't_ need to be allocated). That is, read the file
line by line into into:
char *lines[MAX_LINES];
Your program must contain a call to a function called read_file_by_line
which does the bulk of the work. YOu'll have to decide the interface
for this function.
have the program echo the input as a demonstration.
8. Write a program to sort an input file alphabetically. The interface must be:
PROMPT >> sort < filename > sorted_file
9. Write a function which does the same as (5), but for a 2d array. Careful, thi
s
is tricky. Try to make the array elements contiguous in memory.
? arr2d(int xsize, int ysize);

10. Compare the performance of a very large calculation based largely


on a statically allocated 1d array vs. the same for a dynamically
allocated 1d array. You'll need to use the timer library (I'll
show an example). You are free to invent the calculation, but make
sure it's time-consuming enough that the results are statistically
meaningful.
Do same for a 2-d array.