Tải bản đầy đủ (.doc) (3 trang)

Bài tập Kỹ thuật vi xử lý - DTVT - BKDN (bài10)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (54.37 KB, 3 trang )

Homework #10
Structures (Ch. 17), Input & Output (Ch. 18), Recursion (Ch. 19), Debugging (Ch. 20)

In order to automate the grading process, please follow the exact instructions for each
questions and don’t use extra or unnecessary words.
1. (3 points) Consider the code segment below. If the variable c’s value is 0x053E, what are the
values of variables i, f, and d?
union
{
char c;
int i;
float f;
double d;
} x;
char* c = &x.c;
int* i = &x.i;
float* f = &x.f;
double* d = &x.d;

Answer: i :

f:

d:

2. (2 points) Explain the bug in the program below.
struct node
{
int count;
struct node *next;
};


int main()
{
int data = 0;
struct node *getdata;
getdata->count = data + 1;
printf("%d", getdata->count);
}

Answer:
3. (2 points) Given the C program on the right, what is the missing MSP430 assembly
instruction generated by the compiler for the line:
struct node
{

getdata.next = &getdata;

Hint: Think about activation records and where each
variable is stored in memory.
Main :
0x8020:
8221
0x8020:
4381 0000
0x8024:
0x8028:
5221
0x802A:
4130

SUB.W

CLR.W
missing
ADD.W
RET

#4,SP
0x0000(SP)
instruction
#4,SP

Answer:
BYU, ECEn/CS 124

Homework #10

int count;
struct node *next;

};

int main(void)
{
struct node getdata;
getdata.count = 0;
getdata.next = &getdata;
return 0;
}


4. (2 points) The C program below is compiled on a machine in which char’s are 1 byte, int’s

and pointers are 2 bytes, and float‘s are 4 bytes.

a. What is the size (in bytes) of the
activation record for the function
is_it_noble (include the return address)?
Answer:

b. Assuming that periodic_table, x,
and y are the only local variables, what is the
size (in bytes) of the activation record for the
function main?
Answer:

struct element
{
char name[10];
int atomic_number;
float atomic_mass;
};
int is_it_noble(struct element t[], int i)
{
if ((t[i].atomic_number==2) ||
(t[i].atomic_number==10) ||
(t[i].atomic_number==18) ||
(t[i].atomic_number==36) ||
(t[i].atomic_number==54) ||
(t[i].atomic_number==86)) return 1;
return 0;
}
int main(void)

{
int x, y = 4;
struct element periodic_table[10];
x = is_it_noble(periodic_table, y);
return x;
}

5. (3 points) The following lines of C code appear in a program. What will be the output of
each printf statement?
#define LETTER '1'
#define ZERO 0
#define NUMBER 123
printf("%c", 'a');
printf("0x%x", 12288);
printf("$%d.%c%d\n", NUMBER, LETTER, ZERO);

Answer:
BYU, ECEn/CS 124

Homework #10


6. (3 points) What is the correct statement that uses one scanf function to read two integers
into the two variables temp1 and temp2?
int temp1;
int temp2[1];

Answer:
7. (12 points) What do the following print statements print?
print statement

1
2
3
4
5
6
7
8
9
10
11
12

output

printf("%c %c \n", 'a', 65);
Printf("%d %ld\n", 2010, 650000L);
Printf("%10d\n", 2010);
Printf("%010d\n", 2010);
Printf("%o %x\n", 100, 100);
printf("%#o %#x\n", 100, 100);
printf("%4.2f\n", 3.1416);
printf("%+.0e\n", 3.1416);
printf("%E\n", 3.1416);
printf("%*d\n", 5, 10);
printf("%s\n", "A string");
printf("%.*s\n", 5, "A string");

8. (3 points) Consider the C program below. State the complete output if the input is:
1. 4 9

2. 27 5
3. -1 3
Answer 1:
Answer 2:
Answer 3:

#include <stdio.h>
int Power(int a, int b);
int main(void)
{
int x, y, z;
printf("\nInput two numbers:");
scanf("%d %d", &x, &y);
if (x > 0 && y > 0) z = Power(x, y);
else z = 0;
printf ("\nResult = %d.", z);
}
int Power(int a, int b)
{
if(a < b)
return 0;
else
return (1 + Power(a / b, b));
}

BYU, ECEn/CS 124

Homework #10




×