Quiz 1 Solution

advertisement
Quiz 1 Solution
1. Create an appropriate declaration for the following.
A. A constant called ‘x’ that will be set to 789.
Answer:
flash int x = 789;
-ORconst int x = 789;
-ORconst int flash x = 789;
Note: flash and const are considered the same type of non-volatile, non-changeable
memory. An integer (int) value is declared in this case because 789 is a 16 bit
number (+/-32767 or 0-65535) and is the next larger size variable type from a
character (char or unsigned char) which is only 8 bits (+/- 128 or 0-255).
B. A variable called ’fred’ that will hold numbers from 3 to 456.
Answer:
unsigned int fred;
-ORint fred;
Note: In this case the numbers are always positive so either int or unsigned int will
work, but the range of the number is such that it will not fit into a char or unsigned
char.
C. A variable called ‘sensor_out’ that will contain numbers from –10 to +45.
Answer:
signed char sensor_out;
Note: In this case the numbers will fit into a character (+/-127). ‘signed’ must be
specified because CVAVR converts ‘char’ variables to unsigned char automatically
if it is not marked as ‘signed’ and the “chars are unsigned” option is checked in the
project options.
D. A variable array that will have 10 elements each holding numbers from –23
to 345.
Answer:
int array[10];
Note: In this case the numbers are such that it will not fit into a char because they
can exceed 128 (8 bits).
E. A character string constant that will contain the string ‘Press here to end’.
Answer:
flash char press_string[] = “Press here to end”;
-ORconst char flash press_string[] = “Press here to end”;
-ORconst char press_string[] = “Press here to end”;
Note: flash and const are considered the same type of non-volatile memory. All
three of these declarations will result in the same generated code.
1
2. Evaluate the following.
(Section 1.6)
A. unsigned char t; t = 0x23 * 2; // t = ?
t = 0x46
B. unsigned int t; t = 0x78 / 34; // t = ?
t=3
// 120/34 = 3.53 integer math truncates the decimal.
C. unsigned char x; x = 678; // x = ?
x = 0xA6
// 0x2A6 -> only the lower 8 bits are kept.
D. char d; d = 456; // d = ?
d = -56
// 456 = > 0x1C8, but char is 8 bits so upper
//bit is truncated, resulting in the signed byte value, 0xC8.
E. enum {start = 11, off, on , gone}; // gone = ?
gone = 14
F. x = 0xc2; y = 0x2; z= x ^ y; // z = ?
z = 0xC0
G. e = 27; e += 3; // e = ?
e = 30
H. x = 12; y = x + 2; // y = ?
y = 14, x = 12
x=?
3. Evaluate as true or false as if used in a conditional statement.
For all problems: x = 0x45; y = 0xc6
A. (x == 0x45)
Answer:
TRUE
B. (x | y)
Answer:
TRUE (since 0x45 | 0xC6 = 0xC7, and 0xC7 is not zero)
C. (x > y)
Answer:
FALSE
D. (y – 0x06 == 0xc)
Answer:
FALSE (since 0xC6 – 0x06 = 0xC0, and that is not equal to 0x0C)
4. Evaluate the value of the variables after the fragment of code runs.
unsigned char loop_count;
unsigned int value = 0;
for (loop_count = 123; loop_count< 133;loop_count++)
value += 10;
//value = ??
Answer:
Value = 100
Note: The variable value gets incremented by 10, then the value of cntr is
incremented and tested. The for-loop runs 10 times.
5. Evaluate the value of the variables after the fragment of code runs.
unsigned char cntr = 10;
unsigned int value = 10;
do
{
} while
value++;
(cntr < 10);
// value = ??
Answer:
Value = 11
cntr = ??
cntr = 10
Note: The variable value gets incremented then the value of cntr is tested but not
modified.
6. Evaluate the value of the variables after the fragment of code runs.
unsigned char cntr = 10;
unsigned int value = 10;
while
{
(cntr < 10)
value++;
}
// value = ??
Answer:
Value = 10
cntr = ??
cntr = 10
Note: The variable cntr is tested (but not modified) and is found to be at least 10.
value does not get incremented because the while-loop is not entered.
7. Given: unsigned char num_array[] = {1,2,3,4,5,6,7,8,9,10,11,12};
// num_array[3] = ??
Answer:
num_array[3] = 4
// the first index is 0
8. Given: unsigned int *ptr_array;
unsigned int num_array[] = {10,20,30,40,50,60,70};
unsigned int x,y;
ptr_array = num_array;
x = *ptr_array++;
y = *ptr_array++;
// x= ??
y = ??
ptr_array = ??
Answer:
x = 10
y = 20
ptr_array = num_array + 4
for(x = 21; x!=0x80; x<<=1) // shift a one left until it is 27
twos[y++] = x;
// store value into array and increment index
Download