Monash University > School of Computer Science and Software Engineering > CSE1303 > Part B > Tutorials > Tutorial B2
This tute covers material from lectures B04 to B06.
Attempt the questions marked (*) like this before the tutorial. If you have specific questions about unmarked questions, you can ask the tutor about them during the tutorial.
unsigned int mystery(const char *s)
{
const char *t;
for(t=s; *t != 0; t++)
{
/* spin wheels */
}
return t - s;
}
Rewrite the functions push and pop with the top of stack being indicated by a pointer instead. That is, the definition for the structure is now
struct StackRec
{
float entry[MAXSTACK];
float *topPtr;
};
typedef struct StackRec Stack;
Do not worry about testing for the stack being empty or full. You will need to use pointer arithmetic to adjust the top-of-stack pointer upon push and pop.
(Hint: Consider the following code:
unsigned long x; unsigned long *xptr; unsigned char c; unsigned char *cptr; x = 0x12345678; xptr = &x; /* get address of x */ cptr = (unsigned char *) xptr; /* pretend same address points to char instead */ c = *cptr;What value can you expect to see in c? How does this value differ on big-endian and little-endian computers?)
For reference, here are the sizes in bits of the components of the C floating-point types float, double and long double:
| format | sign | exponent | mantissa (including implicit bit) | excess |
|---|---|---|---|---|
| float | 1 | 8 | 24 | 127 |
| double | 1 | 11 | 53 | 1023 |
| long double | 1 | 15 | 64 | 16383 |
| A | B | S | C |
|---|---|---|---|
| 0 | 0 | ||
| 0 | 1 | ||
| 1 | 0 | ||
| 1 | 1 |
Comment on S and C from a logical-operation perspective (i.e., what logical functions can be used to implement them?).
(To retain the bit pattern in the float, you will need to convert it to an unsigned long using a similar trick to the one in the "Byte Order" section above. If you don't, C will round the float to the nearest integer, which is not what you want.)
Last modified 2002-07-03