Page 269 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 269
246 Chapter 8 Programming in C and C++
Problems
Problems in this chapter and many in later chapters are C and C+ + language
programming problems. We recommend the following guidelines for problems answered
in C: In main() or "self-initializing procedures" each statement must be limited to C
operators and statements described in this chapter, should include all initialization
operations, and should have comments as noted at the end of §8.4, and subroutines
should follow the style for C procedures recommended in §8.5. Unless otherwise noted,
you should write programs with the greatest static efficiency.
1. Write a shortest C procedure void main() that will find x and y if ax + by = c
and dx + ey = f. Assume that a, b, c, d, e, f are global integers that somehow
are initialized with the correct parameters, and answers, x and y, are stored in local
variables in main () . (You might verify your program with a source-level debugger.)
2. Write a shortest C procedure void main ( ) that sorts five numbers in global integer
vector a[ 5 ] using an algorithm that executes four passes, where each pass compares
each a[i], i running from 0 to 3. During each pass, a[i] is compared to each
a [ j ], j running from i+1 to 4, putting the smaller in a (i ] and larger in a [ j ].
3. Write a C procedure void main() to generate the first five Fibonacci numbers F(i),
(F(0) = F(l) = 1 and for i>l, F(i) = F(i-l) + F(i-2) ) in global integers
aO, al, a2, a3, a4 so that ai is F(i). Compute F(2), F(3), and F(4).
4. A two-dimensional array can be simulated using one-dimensional vectors. Write a
shortest C procedure void main() to multiply two 3x 3 integer matrices, A and B,
putting the result in C, all stored as one-dimensional vectors in row major order. Show
the storage declarations/directives of the matrices, so that A and B are initialized as
1 2 3 10 13 16
A = 4 5 6 B= 11 14 17
7 8 9 1 2 1 5 1 8
5. A long can be simulated using one-dimensional char vectors. Suppose A is a
zero-origin 5-by-7 array of 32-bit numbers, each number stored in consecutive bytes
most significant byte first, and the matrix stored in row major order, in a 140-byte char
vector. Write a C procedure int get (char *a, unsigned char i, unsigned char
j r char *v), where a is the storage array, i and j are row and column, and v is the
vector result. If 0 & i < 5 and 0 a? j < 7, this procedure puts the ith row, jth
column 32-bit value into locations v, v+1, v+2, andv+3, most significant byte
first, and returns 1; otherwise it returns a 0 and does not write into v.
6. A struct can be simulated using one-dimensional char vectors. The
struct{long vl; unsigned int v2:4, v3:8, v4:2, v5:1}; has, tightly packed, a
32-bit element vl, a 4-bit element v2, an 8-bit element v3, a 2-bit element v4, a 1-
bit element v5, and an unused bit to fill out a 16-bit unsigned int. Write shortest C
procedures void getVl(char *s, char *v), void getV2( char *s, char *v),