Lab 1: Review 1 Pointer
Lab 1: Review 1 Pointer
Lab 1: Review
1 Pointer
Complete the following functions using the Pointer technique:
1. Input a n-element integer array with int *a is the pointer point to the allocated dynamic memory:
• void inputArray(int* &a, int &n)
2. Remove allocated dynamic memory:
Page 1 / 4
Data Structures & Algorithms Knowledge Engineering Department
2 Recursion
Complete the following functions using the Recursion technique (you may declare some sub-functions):
1. Find the total value of all integers that less than or equal to n: S = 12 + 22 + ... + n2 .
• int sumOfSquares(int n)
2. Find the greatest common divisor of 2 integers a and b:
• int GCD(int a, int b)
• int Factorial(int n)
5. Count the digits of a given number:
• int countDigit(int a)
6. Find the nth Fibonacci number using by the following formula: F (n) = F (n − 1) + F (n − 2).
• int Fib(int n)
3 File Handling
3.1 Data Description
This lab’s data is the anonymized data of the result of the High Graduation Exam 2018 - 2019. The information is provided
in the file "data.txt", which has the content as follow:
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 equal to a 0.
• The scores in the fields: Natural Sciences (KHTN) and Social Sciences (KHXH) will be instructed in the next part.
Page 2 / 4
Data Structures & Algorithms Knowledge Engineering Department
3.2 Programming
Given the Examinee data structure definition:
// Examinee.h
struct Examinee
{
string id;
float math, literature, physic, chemistry, biology, history, geography, civic_education, natural_science,
social_science, foreign_language;
};
Page 3 / 4
Data Structures & Algorithms Knowledge Engineering Department
4 Linkedlist
Given the following Linkedlist definition:
1. Initialize a NODE from a given integer: 7. Remove all NODE from a given List:
• NODE* createNode(int data) • void removeAll(List* &L)
2. Initialize a List from a give NODE: 8. Print all elements of a given List:
• List* createList(NODE* p_node) • void printList(List* L)
3. Insert an integer to the head of a given List: 9. Count the number of elements List:
5 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, 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, return the • Dequeue an element out of a given queue, return
key’s value. the key’s value.
• Count the number of elements of a given stack. • Count the number of element of a given queue.
• Determine if a given stack is empty. • Determine if a given queue is empty.
Page 4 / 4