0% found this document useful (0 votes)
10 views

Lab 1

DSA

Uploaded by

Kidynein
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab 1

DSA

Uploaded by

Kidynein
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structures & Algorithms Knowledge Engineering Department

Lab 1: Review

1 Pointer
Complete the following functions
1. Swap 2 given integers.
void swap(int* a, int* b)

2. Calculate the total value of 2 integers.


int* sum(int* a, int* b)

3. Input an array with an unknown size.


void inputArray(int* &a, int &n)

4. Print a given array.


void printArray(int* a, int n)

5. Find the largest value from a given array.


int* findMax(int* arr, int n)

6. Find the longest ascending subarray from a given array.


int* findLongestAscendingSubarray(int* a, int n, int &length)

7. Swap 2 given arrays.


void swapArrays(int* &a, int* &b, int &na, int &nb)

8. Concatenate 2 given arrays.


int* concatenate2Arrays(int* a, int* b, int na, int nb, int &nc)

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)

This is the intellectual property of the Knowledge Engineering Department. Page 1 / 6


Data Structures & Algorithms Knowledge Engineering Department

10. Generate a random matrix with keyboard input size.


void generateMatrix1(int** &a, int &length, int &width)

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)

12. Swap 2 columns/rows of a given matrix.


void swapRows(int** a, int length, int width, int ir1, int ir2)
void swapColumns(int** a, int length, int width, int ic1, int ic2)

13. Generate the transpose matrix of a given matrix.


int** transposeMatrix(int** a, int length, int width)

14. Concatenate 2 given size-equal matrices, horizontally / vertically.


int** concatenate2MatricesH(int** a, int** b,
int length, int width, int &lres, int &wres)
int** concatenate2MatricesV(int** a, int** b,
int length, int width, int &lres, int &wres)
15. Multiple 2 given matrices.
bool multiple2Matrices(int**& res, int** a, int **b, int& lengthr, int& widthr, int lengtha,
int widtha, int lengthb, int widthb)

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)

This is the intellectual property of the Knowledge Engineering Department. Page 2 / 6


Data Structures & Algorithms Knowledge Engineering Department

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.

This is the intellectual property of the Knowledge Engineering Department. Page 3 / 6


Data Structures & Algorithms Knowledge Engineering Department

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;
};

Fulfill the following requirements:


1. Read the information of one examinee:
• Examinee readExaminee(string line_info);
• Input: line_info - a line from "data.txt" which provides the information of 1 examinee.
• Output: Return Examinee variable, which stores the info of the given examinee.
• Note: The scores of Natural Sciences and Social Sciences column in data.txt is not available by
default. Calculate the scores for each combination and store them into struct Examinee.
– The score of Natural Sciences combination = physics + chemistry + biology.
– The score of Social Sciences combination = history + geography + civic education.
2. Read the information of a list of examinees:
• vector<Examinee> readExamineeList(string file_name);
• Input: file_name - path to input file "data.txt".
• Output: vector<Examinee> variable, which stores the info of all examinees from the file.
3. Write scores of examinees to file:
• void writeScores(vector<Examinee> examinee_list, string out_file_name);
• Input: examinee_list - List of examinees.
out_file_name - name of file to write.
• Output: Write to output file Compulsory Scores (BB), Natural Sciences Scores (KHTN), and
Social Sciences (KHXH) of each examinee using the following format:
– The first line contains header information: "SBD BB KHTN KHXH".
– Each next line contains info of only one examinee: ID, Compulsory Scores, Natural Sciences
Scores, and Social Sciences Scores separated by a single space.
– The Compulsory Scores = maths + literature + foreign language.
• Example:
SDB BB KHTN KHXH
XX001 28.0 29.25 0.0
...
XX999 20.0 0.0 28.75

This is the intellectual property of the Knowledge Engineering Department. Page 4 / 6


Data Structures & Algorithms Knowledge Engineering Department

4 Linkedlist
Given the following Linkedlist definition:

struct NODE { struct List {


int key; NODE* pHead;
NODE* pNext; NODE* pTail;
}; };

Complete the following functions to fulfill the given requirements:

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)

This is the intellectual property of the Knowledge Engineering Department. Page 5 / 6


Data Structures & Algorithms Knowledge Engineering Department

5 Doubly Linkedlist
Following is the representation of a doubly linked list:

struct DNODE { struct DList {


int key; DNODE* pHead;
DNODE* pNext; DNODE* pTail;
DNODE* pPrev;
}; };

Implement functions to execute the operations from singly linkedlist section 4.

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.

This is the intellectual property of the Knowledge Engineering Department. Page 6 / 6

You might also like