Commit fa9dae94 authored by Yipeng Hu's avatar Yipeng Hu

Merge branch 'patch-1' into 'master'

Patch 1

See merge request !2
parents afa7cb1b 6a84f9df
AACGCGTGGCGCCGTCCTTCAAGGAAAGCGGCTGAGTATGCGGTGGTTCAGGTGACGTATCGTGGGCCCCGCTTCTCCAATTCAGATTATTACGATTGTGCGTGAGACAGACGTGACCCGGTTTACCCGTGCGGACGTCATTTAACTGGTTCTTTTTACTGCTGGTCCCTTTCGCGAGACGACACGTGCTGTAGCAGCGGTGAGATATCCCTACAGTTTGGGCCGGAAAACCGGGCCCTAGATCGATTCGGGTGACGCGCTGTGTCTGCTGTTCACCGCTAGAATTTTGATATCAGCCGAGCGGACAAGCCGGGGGTTTGCATATTGCGTCCCACAGCCTATCCACGAGCCACCTTATATAAAGAGGATGTACTCGACGCTGAAGGTACCCTAATTCACAGCATTCGCGAAACAGAAGAGGTACGCCAGTGCCATAAGTACATAGTATCAATTACAGCTAGAGAGGTACATGTTACAACTCATACACCTACACATTCGAATGAGGAAATTATCAGTGATTTAATGGTCTTATGTCTCGTGGTCGGGCGAGGCCATAAAAACCCCATTTATTCAATGCAACTGTCGAATCTCAGGAATATTTTTCACTCTGACGGCTGAAAACCAATGCCCATCGGTGCGTACACCCCGGCTAGCACCTCGGAATTCAAGGGATTGCGGCGTGGAGGGCGCGCAAGTGGCATAGGATGCCCCGACGCGCCCTCGCGTCGCTACCGATGGTGAGCGCGAAGGTCCTAATTAAACTGACATAGATAGCGAAGTGTGTGCAACAATCGAGGGCGACGATTAGGATCAAATTTTAGCTACAGTACAAATACTTAGACTGTATCGAAGGATCCGGACACCGTCTTCTTCATAAGTTGTGCGATTGGCTCAAAGCCTCATCGCCTTCTGAGACTCGCTTTAAGCATAAAACAGAGTCGTACTTTTGTTAAAGGGAATCTTGAGACATGGCGTGACCGTACGATGAGATTTGCGAGAA
\ No newline at end of file
GTTCGCGTGGATTTATATGTAGAGCGGGCTTGTGGCTCTCCAGAGGCCATGGCCGGACAACGCCAGCACGGAAGCGGCTTGTTGCCGCAAGGTTCGAGGGCCCGGAATATTAGTCGACAAGCTACGCATAAAATAGAGGCAAGCGGACGAAGGACTTCCCTCGCTACGGCCGTCTCTAGGCCCTCCAAGCGAGTGGTCTGCCCTCCTCTGTGATCTGCTGGCGCTTACGTTGGCGCGCAAATTGAAGACCAGCCGGCTAACTATAATTAACCTTTTGCAAATCGTAATTAGATACACTTGCCCCTATCCTCGCACATAAAAGTAAAGCTCCAGACTGAGCTTACGTTCCAAGCCACTATCGCGGGTAGCTAGTTGTCTTGCGTAATATTAATATCGGCGACTATAAAGTGCGTATGCCATGTGTTTGACTCAACTGATACCCTTCCAGCATGTACCTTACATGCCGGCATTGTAGCAGACGCTTAGATTTTACTTTAGAGTACTGGCCCGCCCGTTGGAGGCTACTGCATACGCCCCCTATCTCCAGACGCTGGTTTTTTTACATAATACTAGATGGTCATAAATGAGCAAAGCGAGTTAAGCCTACCCCCACGCCTCGTGATTGCAACGGTTATTGCGCGTATTACTACTGGGTTGAAAACTATGTCTTAGCTTTTACGTTACCATGATACTAGCAGTACCACGTGCGTTATTTAGGCTATCGGGCAGCACACCTTCATCTTCGCGCCGTCGCCGTCGCTGAATAAGCTACTCGAACCCGGTCGTTGTAATTGAGATTTTGGAAGTTAAAATCAGCATGCCCAAACGCCTGTACGGAGTGAAGTACCTTCTCTCGGTCTAGAAATACTAGATATAGTCTTTCACGTTCGATGACGGTTAGCCAGTTCGAAATAAGTGTGGACGGAACTGGCCTCCATGTAAGTATCGCTTCTTTCCACTCAAGGACAGGCGGTCCAGCCTGATGTCTTCCTGTATCGTC
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
//declare a pointer to a (double) variable
double* val = nullptr; // declare a NULL pointer
//dynamically allocate a double and loading the address in val (initialise memory)
val = new double;
*val = 12.26; //store value at the allocated address
// print out the stored memory address
cout << "The stored memory address is : " << val << endl;
//print out the value
cout << "Value is : " << *val << endl;
//output
//The stored memory address is : 0133F724 (memory on heap is allocated randomly, therefore, the result is different when you run this code again!)
//Value is : 12.26
delete val; // release the memory pointed by val
// print out the stored memory address after deletion
cout << "After deletion, the stored memory address is : " << val << endl;
//print out the value after deletion
cout << "After deletion, Value is : " << *val << endl;
//output
//After deletion, the stored memory address is : 00008123
//
return 0;
}
#include <iostream>
using namespace std;
int main() {
//normal variable declaration
double normal_val = NULL;
normal_val = 38.26;
cout << "Value is : " << normal_val;
return 0;
}
//output
//Value is : 38.26
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
//normal array declaration
double normal_arr[10];
for (int i = 0; i < 10; i++)
normal_arr[i] = i + 1.1; // store values for the entire array
cout << "Value is: ";
for (int i = 0; i < 10; i++)
cout << normal_arr[i] << " ";
cout << endl;
return 0;
}
//output
//Value is : 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
//declare a pointer to a (double) variable
double* val = nullptr; // declare a NULL pointer
//dynamically allocate a double and loading the address in val (initialise memory)
val = new double;
*val = 12.26; //(3) store value at the allocated address
// print out the memory address
cout << "The memory address of stored value is : " << val << endl;
cout << "The memory address of the pointer val is : " << &val << endl;
//print out the value
cout << "Value is : " << *val << endl;
delete val; // release the memory pointed by val
return 0;
}
//output
//The memory address of stored value is : 0133F724
//The memory address of the pointer val is : 010FFDE0
//Value is : 12.26
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
//declare a pointer to an (double) array
double* arr = nullptr; // declare a NULL pointer
// dynamically allocate memory for 10 double variables continuously, pointer to the first element of the sequence, i.e. arr -> arr[0]
arr = new double[10];
for (int i = 0; i < 10; i++)
arr[i] = i + 1.1; // store value at the allocated address for the entire array
cout << "The memory address of pointer to an array (stored address) is: " << arr << endl;
cout << "The memory address of arr[0] is: " << &arr[0] << endl;
cout << "The memory address of arr[1] is: " << &arr[1] << endl;
cout << "The memory address of arr[2] is: " << &arr[2] << endl;
cout << "The memory address of arr[3] is: " << &arr[3] << endl;
cout << "The memory address of arr[4] is: " << &arr[4] << endl;
cout << "The value is : ";
for (int i = 0; i < 10; i++)
cout << arr[i] << " ";
cout << endl;
//output
//The memory address of pointer of array (stored address) is: 00862178
//The memory address of arr[0] is: 00862178
//The memory address of arr[1] is: 00862180
//The memory address of arr[2] is: 00862188
//The memory address of arr[3] is: 00862190
//The memory address of arr[4] is: 00862198
//The value is : 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1
//Memory on the heap is allocated randomly
delete[] arr; // release the memory for the entire array
arr = nullptr; // set the pointer to NULL pointer
return 0;
}
#include <iostream>
using namespace std;
void CreatePointer()
{
double * ptr = new double(1.1);
}
int main() {
//call CreatePointer() function
CreatePointer();
for (int i =0; i<10000000000000000000; i++)
CreatePointer();
return 0;
}
#include <iostream>
using namespace std;
void swap(int *x, int *y)
{
int t = *x;
*x = *y;
*y = t;
cout << "In the swap(), the value of x is: " << *x << endl;
cout << "In the swap(), the stored memory address of x is: " << x << endl;
cout << "In the swap(), the value of y is: " << *y << endl;
cout << "In the swap(), the stored memory address of y is: " << y << endl;
}
int main()
{
int a = 1;
int b = 2;
cout << "Before calling swap(), the value of a is: " << a << endl;
cout << "Before calling swap(), the memory address of a is: " << &a << endl;
cout << "Before calling swap(), the value of b is: " << b << endl;
cout << "Before calling swap(), the memory address of b is: " << &b << endl;
swap(&a, &b);
cout << "After calling swap(), the value of a is: " << a << endl;
cout << "After calling swap(), the memory address of a is: " << &a << endl;
cout << "After calling swap(), the value of b is: " << b << endl;
cout << "After calling swap(), the memory address of b is: " << &b << endl;
return 0;
}
//output
/*
Before calling swap(), the value of a is: 1
Before calling swap(), the memory address of a is: 010FF82C
Before calling swap(), the value of b is: 2
Before calling swap(), the memory address of b is: 010FF820
In the swap(), the value of x is: 2
In the swap(), the stored memory address of x is: 010FF82C
In the swap(), the value of y is: 1
In the swap(), the stored memory address of y is: 010FF820
After calling swap(), the value of a is: 2
After calling swap(), the memory address of a is: 010FF82C
After calling swap(), the value of b is: 1
After calling swap(), the memory address of b is: 010FF820
*/
#include <iostream>
using namespace std;
void swap(int **x, int **y)
{
int *t = *x;
*x = *y;
*y = t;
cout << "In the swap(), the value of x is: " << **x << endl;
cout << "In the swap(), the stored memory address of x is: " << x << endl;
cout << "In the swap(), the value of y is: " << **y << endl;
cout << "In the swap(), the stored memory address of y is: " << y << endl;
}
int main()
{
int *a = new int(1);
int *b = new int(2);
cout << "Before calling swap(), the value of a is: " << *a << endl;
cout << "Before calling swap(), the stored memory address of a is: " << a << endl;
cout << "Before calling swap(), the value of b is: " << *b << endl;
cout << "Before calling swap(), the stored memory address of b is: " << b << endl;
swap(&a, &b);
cout << "After calling swap(), the value of a is: " << *a << endl;
cout << "After calling swap(), the stored memory address of a is: " << a << endl;
cout << "After calling swap(), the value of b is: " << *b << endl;
cout << "After calling swap(), the stored memory address of b is: " << b << endl;
return 0;
}
// output
/*
Before calling swap(), the value of a is : 1
Before calling swap(), the stored memory address of a is : 014723F8
Before calling swap(), the value of b is : 2
Before calling swap(), the stored memory address of b is : 01472098
In the swap(), the value of x is : 2
In the swap(), the stored memory address of x is : 0133FE08
In the swap(), the value of y is : 1
In the swap(), the stored memory address of y is : 0133FDFC
After calling swap(), the value of a is : 2
After calling swap(), the stored memory address of a is : 01472098
After calling swap(), the value of b is : 1
After calling swap(), the stored memory address of b is : 014723F8
*/
\ No newline at end of file
#include <iostream>
using namespace std;
void swap(int &x, int &y)
{
int t = x;
x = y;
y = t;
cout << "In the swap(), the value of x is: " << x << endl;
cout << "In the swap(), the value of y is: " << y << endl;
}
int main()
{
int a = 1;
int b = 2;
cout << "Before calling swap(), the value of a is: " << a << endl;
cout << "Before calling swap(), the value of b is: " << b << endl;
swap(a, b);
cout << "After calling swap(), the value of a is: " << a << endl;
cout << "After calling swap(), the value of b is: " << b << endl;
return 0;
}
#include <iostream>
using namespace std;
void swap(int x, int y)
{
int t = x;
x = y;
y = t;
cout << "In the swap(), the value of x is: " << x << endl;
cout << "In the swap(), the value of y is: " << y << endl;
}
int main()
{
int a = 1;
int b = 2;
cout << "Before calling swap(), the value of a is: " << a << endl;
cout << "Before calling swap(), the value of b is: " << b << endl;
swap(a, b);
cout << "After calling swap(), the value of a is: " << a << endl;
cout << "After calling swap(), the value of b is: " << b << endl;
return 0;
}
\ No newline at end of file
#include <iostream>
#include <time.h>
using namespace std;
class Sequence
{
private:
char* ary = nullptr;
public:
Sequence(int length);
~Sequence();
void InitialiseSequence(int length);
void PrintSequence(int length);
};
Sequence::Sequence(int length)
{
ary = new char[length];
for (int i = 0; i < length; i++)
ary[i] = NULL;
}
Sequence::~Sequence()
{
delete[] ary;
ary = nullptr;
}
void Sequence::InitialiseSequence(int length)
{
/* initialize random seed: */
srand(time(NULL));
int randnumber = 0;
for (int i = 0; i < length; i++)
{
randnumber = rand() % 4 + 1;
if (randnumber == 1)
ary[i] = 'A';
else if (randnumber == 2)
ary[i] = 'T';
else if (randnumber == 3)
ary[i] = 'C';
else
ary[i] = 'G';
}
}
void Sequence::PrintSequence(int length)
{
cout << "The Sequence is :" << endl;
for (int i = 0; i < length; i++)
cout << ary[i];
cout << endl;
}
int main()
{
// creating an object of Sequence
int l = 10;
Sequence s(l); // parameterized constrcutor
/* creating a pointer of Sequence type &
assigning address of s to this pointer */
Sequence* ptr = &s;
/* Initialsie sequence by using pointer
ptr to call the method InitialiseSequence()
*/
ptr->InitialiseSequence(l);
/* Display sequence by using pointer
ptr to call the method PrintSequence()
*/
ptr->PrintSequence(l);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int NumberOfRow = 5;
int NumberOfCol = 4;
int** arr = new int*[NumberOfRow];
for (int i = 0; i < NumberOfRow; i++)
arr[i] = new int[NumberOfCol];
arr[0][0] = 1;
arr[4][3] = 2;
cout << "The value of arr[0][0] is: " << arr[0][0] << endl;
cout << "The value of arr[4][3] is: " << arr[4][3] << endl;
for (int i = (NumberOfRow - 1); i >= 0; i--)
delete[] arr[i];
delete[] arr;
arr = nullptr;
return 0;
}
//output
//The value of arr[0][0] is : 1
//The value of arr[4][3] is : 2
#include <iostream>
using namespace std;
class SmartPtr
{
int *ptr; // Actual pointer
public:
// Constructor: Refer https://www.geeksforgeeks.org/g-fact-93/
// for use of explicit keyword
explicit SmartPtr(int *p = NULL) { ptr = p; }
// Destructor
~SmartPtr() { delete(ptr); }
// Overloading dereferencing operator
int &operator *() { return *ptr; }
};
int main()
{
SmartPtr ptr(new int());
*ptr = 20;
cout << *ptr;
// We don't need to call delete ptr: when the object
// ptr goes out of scope, destructor for it is automatically
// called and destructor does delete ptr.
return 0;
}
// output
// 20
**In-class Examples**
The C++ codes of In-class examples are available [here](In-class Examples).
<br>
**Exercise 1**
*Create a segmented image*
(1) Load a pair of lung CT image and airway mask using OpenCV library
(2) The program reads two arguments from command prompt
i.e. Exercise1.exe source278.png mask278.png
(3) Store two loaded images into dynamically allocated 2D arrays for manipulation
(4) Compute the airway segment from dynamically allocated arrays
(5) Save the result to a PNG file using OpenCV library
i.e. segmentedAirway.png
(6) Delete dynamically allocated arrays
<br>
**Exercise 2**
*Analyze DNA sequences*
(1) Load two DNA sequences from two files dynamically
(2) Count the total number of A,T,C,G in each sequence
(3) Compare the similarity between these two sequences
The A/T/C/G is at the same position on both sequences
Sequence 1 : CATCGACTCC
Sequence 2 : ACTCGGCTTC
Similarity = 6/10 = 60%
(4) Delete the dynamically allocated sequences
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment