Tải bản đầy đủ (.pdf) (6 trang)

Lab 9 pointers

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 (248.71 KB, 6 trang )

Course: Programming Fundamentals (C language)
LAB 9 – Pointers
Main content:
1.
2.
3.
4.

Get used to pointer declarations
Get used to using pointers in C programs
Complete more complex C programs
Check coding styles

Practice:
1. Get used to pointer declarations
1.1. What resulting values do the variables a1 and a2 have after the execution of the following
code?

void main() {
int a1 = 5, a2 = 8;
int* p1 = &a1;
int* p2 = &a2;
int** p3 = &p2;
int** p4 = &p1;
*p4 = p2;
*p1 += 2;
p2 = &a1;
(**p3)--;
**p4 -= 3;
}


1.2. What are the values of a and b variables at the end of the execution of the following
program?

void main() {
int a = 10;
1


float b = 5.5;
int* p1 = &a;
float* p2 = &b;
void* p3 = p1;
(*(int*)p3)++;
p3 = p2;
*(float*)p3 += 3;
p1 = p3;
*(float*)p1 -= 2;
(*p2)++;
}
1.3. Write a C program with the following descriptions. What is the value of each variable at the
end of the execution of the program?
1.3.1. Define an integer variable named “a”. Its initialized value is 10.
1.3.2. Define a double variable named “b”. Its initialized value is 2.5.
1.3.3. Define a pointer, named “p1”, which can point to an integer memory location. Its
initialized value is the address of “a”.
1.3.4. Define a pointer, named “p2”, which can point to an integer memory location. Its
initialized value is NULL.
1.3.5. Define a pointer, named “p3”, which can point to a double memory location. Its
initialized value is the address of “b”.
1.3.6. Define a pointer, named “p4”, which can point to a float memory location. Its

initialized value is NULL.
1.3.7. Pointer “p2” points to an integer memory location in the Heap memory.
1.3.8. Pointer “p4” points to the same location to which “p3” points.
1.3.9. Increase 4 for the value in the integer memory location to which “p1” points.
1.3.10. Increase 5 for the value in the float memory location to which “p4” points.
1.3.11. Decrease 2 for the value in the double memory location to which “p3” points.
1.3.12. Set 8 to the value in the integer memory location to which “p2” points.
1.3.13. Pointer “p1” points to the same location to which “p2” points.
1.3.14. Pointer “p3” points to the same location to which “p1” points.
1.3.15. Increase 10 for the value in the memory location to which “p3” points.
1.3.16. Pointer “p2” points to the memory location named “b”.
2


1.3.17. Decrease 1 for the value in the memory location to which “p2” points.
1.3.18. Pointer “p3” points to the memory location of 4 floating-point numbers in the Heap
memory initialized with zeros.
1.3.19. Set 12 to the value of the third number in the memory location to which “p3”
points.
1.3.20. Pointer “p4” points to the same memory location to which “p3” points.
1.3.21. Increase 5 for the value in the memory location to which “p4” points.
1.3.22. Pointer “p3” points to the memory location named “b”.
1.3.23. Decrease 1 for the value in the memory location to which “p3” points.
1.3.24. Pointer “p2” points to the same location to which “p1” points.
1.3.25. Increase 1 for the value in the memory location to which “p2” points.
1.4. Given a program as follows. What is printed on screen?

2. Get used to using pointers in C programs
2.1. Write a C program using functions to process the purchased products of each customer as
follows:

Customer Products bought
Brown

milk, beer, peanut

White

bread, butter, milk, sugar

David

milk, peanut, egg

William

bread, butter, coconut

Henry

beer, coconut, bean, peanut, sugar

Tony

milk, peanut, bread, butter

Peter

bread, butter, peanut

3



Find and print the users who bought the products above average.
After a week, a new list of the purchased products of each customer is updated as follows:
Customer Products bought
Peter

beer, peanut, water

Mary

milk, peanut, bread, butter, sugar, noodle

David

beer, coconut

Again, find and print the updated users who bought the products above average.
2.2. Given a graph representing friend connections among several Facebook users, write a C
program using dynamic arrays and functions to find and print the users who have the
highest number of direct friends and who have the lowest number of direct friends. It is
supposed that Peter is a new Facebook user who is a direct friend of John and Mary. Now
update the information and print the new information about the users who have the
highest number of direct friends and who have the lowest number of direct friends.
A friend graph is given as follows:

4


A new friend graph after adding Peter is as follows:


3. Complete more complex C programs
3.1. Write a C program using functions to compute the addition and subtraction of two matrixes
which are randomly generated with random integer numbers in range [-100, 100]. The size
of each matrix is given by the user.
3.2. Given a collection of sentences about sport news, write a C program using functions to
return all the sentences relevant to the query input by the user. The relevance between a
query and a sentence is based on the highest number of the words they have in common.
For example, given a collection as follows:
S1 = “A football game took place in Manchester yesterday.”
S2 = “Ronaldo got a hat trick in the football game again for Real Madrid.”
S3 = “This was the second time Manchester was full of happy people.”
A query is given as: “football in Manchester”
The relevant sentences returned are: “A football game took place in Manchester yesterday.”
3.3. It is supposed that each arithmetic expression on integer numbers is represented in the
form of binary tree as described in the example as follows:

5


Expression: (1+2)*3
Binary tree:

Write a C program using functions to define appropriate structures for this expression tree
kind and then allow users to input an expression in the format of a string, parse this
expression into an expression tree, and finally determine a value of the given expression by
traversing the tree in left-right-node order.
4. Check coding styles
For each C program that you have written, check coding styles with the following points:
- How have you used blank lines?

- How have you used tabs for alignment?
- How have you used comments in your source code files?
- Above all, how about your naming conventions? What have you named in your source code
files?

6



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×