Lab 1
Lab 1
Lab 1: Review
1 Pointer
Complete the following functions
1. Swap 2 given integers.
void swap(int* a, int* b)
9. Given 2 ascending arrays with distinguishing elements. Generate a new ascending array with all
elements from the given arrays.
int* merge2Arrays(int* a, int* b, int na, int nb, int &nc)
11. Given two 1D arrays a and b. Generate the matrix c that c[i][j] = a[i] * b[j].
int** generateMatrix2(int* a, int* b, int na, int nb, int &crow, &ccol)
16. Given a matrix a. Find the submatrix of a which satisfies keyboard input size and has the largest
total value of its elements.
int** findSubmatrix(int** a, int length, int width, int &lres, int &wres)
From No. 17. to No. 20. are Searching Algorithms. Return the first position found, else, return −1.
17. Sequential Search.
• int LinearSearch(int* a, int n, int key)
18. Sequential Search uses a flag.
• int SentinelLinearSearch(int* a, int n, int key)
19. Binary Search in sorted array a.
• int BinarySearch(int* a, int n, int key)
20. Binary Search in sorted array a uses recursion.
• int RecursiveBinarySearch(int* a, int left, int right, int key)
2 Recursion
Complete the following functions using the Recursion technique (you may declare some sub-functions):
1. Calculate the sum of S = 1 + 2 + 3 + ... + n.
2. Calculate the factorial n! = 1 ∗ 2 ∗ 3 ∗ ... ∗ n.
3. Calculate xn .
4. Count the number of digits of a given integer.
5. Verify if every digit of a given integer is even.
6. Count the number of common divisors of 2 given integers.
7. Calculate the Greatest common divisor and Least common multiple of 2 given integers.
8. Calculate the reverse value of a given integer.
9. Calculate the ith Fibonacci number.
• F0 = 0, F1 = 1
• Fn = Fn−1 + Fn−2 , (n ≥ 2)
10. * Given 4 single distinguish characters. Print out all possible permutations.
• Example: ABCD, ABDC, ACBD, ...
3 File Handling
3.1 Data Description
This given data is the anonymized data of the results of the National High School Graduation Exam
2018 - 2019. The information is provided in the file "data.txt", which has the first few lines as follows:
in which:
• The first line provides the included information fields.
• For the next lines, each one is the information of 1 candidate, separated by a comma ",".
• The empty fields mean there is no information. If the empty field is a subject, that is equal to a 0.
• The scores in the fields of Natural Sciences (KHTN) and Social Sciences (KHXH) will be instructed
in the next part.
3.2 Programming
Given the Examinee data structure definition:
// Examinee.h
struct Examinee
{
string id;
float maths, literature, physics, chemistry, biology, history, geography, civic_education, natural_science,
social_science, foreign_language;
};
4 Linkedlist
Given the following Linkedlist definition:
1. Initialize a NODE from a given integer: 10. Insert an integer at a position of a given List:
• NODE* createNode(int data) • void addPos(List &L, int data, int pos)
2. Initialize a List from a give NODE: 11. Remove an integer at a position of a given
• List createList(NODE* pNode) List:
3. Insert an integer to the head of a given List: • void removePos(List &L, int pos)
• void addHead(List &L, int data) 12. Insert an integer before a value of a given List:
4. Insert an integer to the tail of a given List: • void addBefore(List L, int data, int val)
• void addTail(List &L, int data) 13. Insert an integer after a value of a given List:
5. Remove the first NODE of a given List: • void addAfter(List L, int data, int val)
• void removeHead(List &L) 14. Print all elements of a given List:
6. Remove the last NODE of a given List: • void printList(List L)
• void removeTail(List &L) 15. Count the number of elements List:
7. Remove all NODE from a given List: • int countElements(List L)
• void removeAll(List &L) 16. Create a new List by reverse a given List:
8. Remove node before the node has val value in • List reverseList(List L)
a given List:
17. Remove all duplicates from a given List:
• void removeBefore(List L, int val)
• void removeDuplicate(List &L)
9. Remove node after the node has val value in
a given List: 18. Remove all key value from a given List:
• void removeAfter(List L, int val) • bool removeElement(List &L, int key)
5 Doubly Linkedlist
Following is the representation of a doubly linked list:
6 Stack - Queue
Following is the representation of a Singly linked list node:
struct NODE {
int key;
NODE* pNext;
};
Utilize the Linked list above, define the data structure of Stack and Queue, and then implement
functions to execute the following operations:
1. Stack 2. Queue
• Initialize a stack from a given key. • Initialize a queue from a given key.
• Push a key into a given stack. • Enqueue a key into a given queue.
• Pop an element out of a given stack, the • Dequeue an element out of a given
key’s value will be returned. queue, the key’s value will be returned.
• Count the number of elements of a given • Count the number of elements of a given
stack. queue.
• Determine if a given stack is empty. • Determine if a given queue is empty.