Книга: Advanced PIC Microcontroller Projects in C
3.5 Exercises
3.5 Exercises
1. Write a C program to set bits 0 and 7 of PORTC to logic 1.
2. Write a C program to count down continuously and send the count to PORTB.
3. Write a C program to multiply each element of a ten element array by 2.
4. Write a C program to add two matrices P and Q. Assume that the dimension of each matrix is 3?3 and store the result in another matrix called W.
5. Repeat Exercise 4 but this time multiply matrices P and Q and store the product in matrix R.
6. What do the terms variable and constant mean?
7. What does program repetition mean? Describe the operation of while, do-while, and for loops in C.
8. What is an array? Write example statements to define the following arrays:
a) An array of ten integers
b) An array of thirty floats
c) A two-dimensional array having six rows and ten columns
9. Trace the operation of the following loops. What will be the value of variable z at the end of each loop?
a)
unsigned char j = 0, z = 0;
while (j < 10) {
z++;
j++;
}
b)
unsigned char z = 10;
for (j = 0; j < 10; j++) z--;
10. Given the following variable definitions, list the outcome of the following conditional tests in terms of “true” or “false”:
unsigned int a = 10, b = 2;
if (a > 10)
if (b >= 2)
if(a == 10)
if (a > 0)
11. Write a program to calculate whether a number is odd or even.
12. Determine the value of the following bitwise operations using AND, OR, and EXOR operations:
Operand 1: 00010001
Operand 2: 11110001
13. How many times does each of the following loops iterate, and what is the final value of the variable j in each case?
a) for(j = 0; j < 5; j++)
b) for(j = 1; j < 10; j++)
c) for(j = 0; j <= 10; j++)
d) for(j = 0; j <= 10; j += 2)
e) for(j = 10; j > 0; j -= 2)
14. Write a program to calculate the sum of all positive integer numbers from 1 to 100.
15. Write a program to evaluate factorial n, where 0! and 1! evaluate to 1 and n! = n ? (n – 1)!
16. Write a program to calculate the average value of the numbers stored in an array. Assume that the array is called M and has twenty elements.
17. Modify the program in Exercise 16 to find the smallest and largest values of the array. Store the smallest value in a variable called Sml and the largest value in a variable called Lrg.
18. Derive equivalent if-else statements for the following tests:
a) (a > b) ? 0 : 1
b) (x < y) ? (a > b) : (c > d)
19. Given that f1 and f2 are both floating point variables, explain why the following test expression controlling the while loop may not be safe:
do {
...............
...............
} while(f1 != f2);
Why would the problem not occur if both f1 and f2 were integers? How would you correct this while loop?
20. What can you say about the following while loop?
k = 0;
Total = 0;
while (k < 10) {
Sum++;
Total += Sum;
}
21. What can you say about the following for loop?
Cnt = 0;
for (;;) {
Cnt++;
}
- 1.25 Exercises
- 2.3 Exercises
- 4.5 Exercises
- 5.5 Exercises
- CHAPTER 4 Functions and Libraries in mikroC
- CHAPTER 5 PIC18 Development Tools
- 1.6 Converting Binary Numbers into Decimal
- 1.7 Converting Decimal Numbers into Binary
- 1.9 Converting Hexadecimal Numbers into Binary
- 1.10 Converting Hexadecimal Numbers into Decimal
- 1.11 Converting Decimal Numbers into Hexadecimal
- 1.12 Converting Octal Numbers into Decimal