1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

Supplementary tasks pointers

7 0 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Pointers
Trường học University of Example
Chuyên ngành Computer Science
Thể loại Bài tập
Năm xuất bản 2023
Thành phố Example City
Định dạng
Số trang 7
Dung lượng 348,85 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

1 Chapter 8 Pointers A Review A 1 What is a pointer? Give at least three examples of pointers of different data types A 2 Differentiate the following expressions when variable is a pointer and when no[.]

Trang 1

Chapter 8: Pointers

A Review

A.1 What is a pointer? Give at least three examples of pointers of different data types

A.2 Differentiate the following expressions when variable is a pointer and when not by determining which expressions are valid and which not, which expressions return a pointer and which not: &variable,

*variable, **variable, &*variable, *&variable

A.3 What are differences between a fixed-size array of integer numbers and an array of integer numbers controlled via a pointer?

int a[5] = {1, 2, 3, 4, 5};

int* aPtr = (int *)malloc(5*sizeof(int));

int i;

for (i=0; i<5; i++) aPtr[i] = a[i];

A.4 What are advantages and disadvantages of using an array located in heap memory as compared to the one in stack memory? Give examples for illustration

A.5 How can we refer to each member of a structure via a pointer which points to that structure? Give

an example for illustration

A.6 Discuss address passing to pointers which are formal parameters of a function Give an example for illustration of each way of address passing

A.7 What are dangling references? How to handle those dangling references? Give an example for

illustration

A.8 What is a function pointer? Describe its use context via an example

B Practice

B.1 Re-define the arrays in the previous chapter, chapter 7, so that we have their equivalent arrays located in heap memory

B.2 Define pointers appropriate for the following descriptions

B.2.1 A pointer that points to a character memory location

B.2.2 A pointer that points to a floating-point memory location

B.2.3 A pointer that points to a memory location of ten integer numbers

Trang 2

B.2.4 A pointer to a string that is located in the heap memory

B.2.5 A pointer to an array of strings that are located in the heap memory

B.2.6 A pointer to a matrix of floating-point numbers that are located in the heap memory

B.2.7 A pointer to an array of elements each of which is a structure of suppliers with two members: id and value in the heap memory

B.2.8 A pointer to a list of functions to calculate a sum of the N first natural numbers and a factorial of a natural number N

B.3 Find and fix the following program for no errors and memory leaks What will be displayed on screen?

Trang 3

B.4 Describe the value of each variable after each statement in the following program

Trang 4

B.5 Given an array of n integer numbers with n input from a user, write a function to generate the elements of this array randomly After that, write a function to change the value of each element in the array to a sum of its current value and the new value of the previous element and then write another function to restore the array to its original version Write a program to illustrate the sum-based encoding of an array and its corresponding de-coding

Input: n = 7

Output:

A random array is: 1, 2, 3, 4, 5, 6, 7

Sum-based encoding of this array: 1, 3, 6, 10, 15, 21, 28

Its corresponding de-coding: 1, 2, 3, 4, 5, 6, 7

B.6 Write a program to check if a matrix is a diagonal matrix, a lower triangular matrix, or an upper triangular matrix If not, change it to a corresponding diagonal, lower triangular, or upper triangular

Trang 5

upper triangular matrix is a square matrix with all elements below the main diagonal equal to zero It is noted that the size of a matrix is not given in advance

B.7 Given the information of each food item as displayed in the following table:

(Source: A multiple regression project, Roger Johnson.)

Define a structure to represent each food item Among these food items, which one has the smallest standard deviation: calories, fat, protein, or carbohydrates?

B.8 Given an array of n integer numbers and another integer number num, complete the following function to return all the addresses of the elements in the array that are equal to the given number num After that, write a program to illustrate a function call with the resulting function and then change all the found elements to be MAX_INT which is 2,147,483,647

Input: int a[n] = {1, 2, 3, 4, 1, 2, 4, 6, 8, 9, 1};

num = 1

Output: int** numAddr = {a, a+4, a+10}

Trang 6

B.9 Given two strings, s1 and s2, write a function to return all the starting positions where s2 appears in s1 After that, write a program to illustrate a function call with the resulting function It is noted that the starting position of s1 is counted from 0

Input:

s1 = “Today is Monday, the first day.”

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 17 19 20 21 22 23 24 25 26 27 28 29 30

s2 = “day”

Output: 2, 12, 27

B.10 In chapter 4, you have prepared a menu for the program of your assignment in B.6 The details are below

In your assignment, you might have a menu listing several options to have access to your program The following are some options:

- Populate data

- Query data

- Make simple queries

- Make advanced queries

- Update data

Trang 7

- Delete an existing item

- Simulate a control flow

- Exit

Write a program to generate a menu with the aforementioned options in a similar format shown above Ask a user about what he/she would like to proceed Display what he/she has chosen from a corresponding function After that, say “Thank you!” before asking he/she to press Enter for closing the program It is supposed that a user can key what he/she can Therefore, make sure that you check his/her input carefully and appropriately Function pointers are suggested to be used in this program B.11 Check all the programs you have written for the arrays If they are not dynamically allocated in the heap memory, change your programs so that they are dynamically allocated in the heap memory Please remember to free the memory locations once done

B.12 Check your coding style with the programs you have written Should anything be changed with

your programs so that they can be not only executable but also readable? Rewrite your programs if yes

Ngày đăng: 11/04/2023, 18:47