December, 2018

The last editing time: Sat 22:33 Dec.15 2018

  • The debugging of the program in [Mooc] What day is the day? reserved unsolved

An Introduction to the Employed Highlight Mark

TYPE INTRODUCTION
test.cpp filename left will be marked with highlighted background
terminal keywords will be marked as bold

Ubuntu Terminal Statement

STATEMENT INTRODUCTION
Ctrl + Alt + T Turn on the terminal
sudo rm -rf test.cpp delete the file test.cpp
mv test.cpp another.cpp rename the file test.cpp with another name another.cpp
g++ test.cpp -o test process the source code test.cpp and output as the file test
gedit test.cpp open the file test.cpp with Text Editor(if the file test.cpp not existing, then create it)
touch test.cpp create the new file test.cpp
su active administrator mode with code for unix
sudo password reset a password for unix
F11 To set the present window full-screened

MacOS X Terminal Statement

[Dec.26]

STATEMENT INTRODUCTION
command+H To hide the present window
command+W To close the present window
open -e test.cpp To open the file test.cpp with text editor
open -t test.cpp To open the file test.cpp with default editor
mkdir Repository To create a new folder (direction) ~/Repository
command+space Spotlight search
control+command+F To set the present window full-screened
command+option+esc To open the application control window
command+shift+5 To grasp a screenshot

Git Terminal Statement

[Dec.26] For more details, check the blog 手把手教你在Github上建立自己的個人博客網站@Marcus灬Li

STATEMENT INTRODUCTION
git init To initialize the present direction a git repository
git add . To upload the files in the repository locally
git config –global user.name "Username" To set the present repository a username
git config –global user.email “Email Address” To set the present repository an email address
git remote add origin Address of Your Remote Repository To set an address of your remote repository for the present repository
git push -u origin master To submit (or push) the files in the present repository to master branch of your remote reposiotory

C++ on Learning…

[function] isdigit()

The function isdigit() accept a single char value and return a bool value as judgement on whether the value accepted is a digit. If yes, the bool value true return while false returned vice.

#include <cstdio>
using namespace std;

int main()
{
	char n;
	...
	if (isdigit(n))
	...

[function] setw()

[Dec.12] The function setw() is employed to set the ouput content a fixed width to get orderly outlook. It supposed to be inserted to the output stream as the following format.

#include <iostream>
#include <iomanip>		// If the function setw() called, the heading file iomanip shoulda been included.
using namespace std;

int main()
{
	int width;
	... you shoulda called 
	cout << setw(width) << "This is just for test.\n"
	...

[library] cmath

The library camth contains a series of functions dealing with mathematical operations.

FUNCTIONS INTRODUCTION
int abs(int x) x\|x\|
double fabs(double x) x\|x\|
double cos(double x) cosx\cos x
double sin(double x) sinx\sin x
int ceil(double x) [x]+1[x]+1
double sqrt x\sqrt x

[library] ctype

The library contains a series of functions dealing with character operations.
CAUTION: char-type and int-type variables are stored the same format, you are able to employ either int-type and char-type for the following functions.

FUNCTIONS INTRODUCTION
int isdigit(int c) To judge if c is a digit
int isalpha(int c) To judge if c is an alpha
int isalnum To judge if c is an alpha or digit
int lower(int c) To judge if c is a lower alpha
int upper(int c) To judge if c is a upper alpha
int toupper(int c) Return the upper format when receiving a lower alpha
int tolower(int c) Return the lower format when receiving an upper alpha

Bitwise Operation and Logic Gate

[Dec.21 ~ Dec.

What is Bitwise Operation?

Bitwise Operation is somehow logic operation of binary digit or binary-based digit (like hexadecimal digit). Factually it displays a logic gate operation logically instead of physically. There is indeed similarity and relation between bitwise operation and logic gate, for much both alike each other.

Bitwise Operation

There are in total six (or more) bitwise operations as the following table.

TYPE CODE(C++)
AND a & b
OR a | b
XOR a ^ b
NOT ~a
shift left(unsigned) a << b
shift right(signed) a >> b
(This is only for Java)shift right(unsigned) a >>> b

Just consider Bitwise Operation a combination of

Exercises & Practice

[C++ Primer Plus] Factorial

You mayhap put down a numeric and get the factorial of it from the function main.

#include <iostream>
long long factorial(int);

long long factorial(int n)
{
	long long result = 1;
	if (n == 0) return result;
	for (int i = n; i > 0; i--) {
		result *= i;
		long long factorial(i); }
	return result;
}

int main()
{
	using namespace std;
	char choice; 
do
{
	cout << "\nEnter your number: ";
	int n;
	cin >> n;
	long long consequence = factorial(n);
	cout << n << "! = " << consequence << endl;
	cout << "Would you like to continue? [Y/n]\n";
	cin >> choice;
} while (choice != 'n');

	return 0;
}

[Mooc] Prime Number Selection (Pro edition available)

Put down a numeric as the end of range from 0 and the program will select all the prime numbers amid the range and print them.

#include <iostream>
#include <cmath>
using namespace std;
#define max_num 1000
char isPrime[max_num+10];

int main()
{		
	for (int  i =1; i <= max_num; i++)
		isPrime[i] = true;
	for (int  i = 2; i < max_num; i++) {
		if (isPrime[i])
		for (int  j = i*2; j <= max_num; j += i)
			isPrime[j] = false; }

	for (int  i =2; i <= max_num; i++) {
		if (isPrime[i])
		cout << i << " "; }
	cout << endl;
	return 0;
}

[Dec.12] The program has been improved to Pro edition with additional functions as following:

  • You can input the upper limit by yourself
  • The numbers output in fixed width of 8 bytes
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	cout << "Please enter the endpoint of the prime number selection range: ";
	long long max_num;
 	cin >> max_num;

	char isPrime[max_num +10];
	for (int i = 1; i<= max_num; i++)
		isPrime[i] = 1;
	for (int i = 2; i<= max_num; i++) {
		if (isPrime[i]) {
			for (int j = i*2; j <= max_num; j += i)
				isPrime[j] = 0; } }

	for (int i = 2; i<=max_num; i++) {
		if (isPrime[i])
			cout << setw(8) << i << " "; }
	cout << endl;
	return 0;
}

The output window is of nice outlook.

  • The output window:
    在這裏插入圖片描述

[Mooc] Matrix Multiplicative

Put down two multiplicable matrices according to the order "rows and volumes and the first matrix itself, then identical to the second matrix. The program will return the consequence.

#include <iostream>
using namespace std;
const int ROW = 8;
const int COL = 8;
int a [ROW][COL];
int b [ROW][COL];
int c [ROW][COL];

int main()
{
	int m,n,p,q;

	cin >> m >> n;
	for (int i = 0; i<m; i++) {
		for (int j = 0; j<n; j++)
			cin >> a[i][j]; }
	cout << "\n\n";

	cin >> p >> q;
	for (int i =0; i<p; i++) {
		for (int j = 0; j<q; j++)
			cin >> b[i][j]; }
	cout << "\n\n";

	for (int i = 0; i<m; i++) {
		for (int j =0; j< q; j++) {
			c[i][j] = 0; 
			for (int k = 0; k<n; k++)
				c[i][j] += a[i][k] * b[k][j]; } }
	 
	for (int i = 0; i<m; i++) {
 		for (int j = 0; j<q; j++)
			cout << c[i][j] << " ";
		cout << endl; }

	return 0;
}

[Mooc] Figure Out the Root with Newton-Raphson Method

[Dec.12] The program employ Newton-Raphson method(or, Newton’s method) to figure out the root of a certain number. First, let’s see what is Newton-Raphson method.
The Newton-Raphson method aims at getting a rather proper estimation of a root. Somehow it is Taylor Equation or Maclaurrin Equation that theoretically proved this method.
f(x)=Pn(x)=f(x0)+f(x0)(xa)+f(x0)2!(xa)2+f(x0)3!(xa)3++f(n)(x0)n!(xa)n+Rn(x) f(x) = P_n(x) = f(x_0)+f&#x27;(x_0)(x-a)+\frac{f&#x27;&#x27;(x_0)}{2!}(x-a)^2+\frac{f&#x27;&#x27;&#x27;(x_0)}{3!}(x-a)^3 +\cdots+\frac{f^{(n)}(x_0)}{n!}(x-a)^n+R_n(x)

  • Taylor Equation

f(x)=Pn(x)=f(0)+xf(0)+f(0)2!x2+f(0)3!x3++f(n)(0)n!xn+Rn(x) f(x) = P_n(x) = f(0)+xf&#x27;(0)+\frac{f&#x27;&#x27;(0)}{2!}x^2+\frac{f&#x27;&#x27;&#x27;(0)}{3!}x^3 +\cdots+\frac{f^{(n)}(0)}{n!}x^n+R_n(x)

  • Maclaurrin Equation

The Newton-Raphson method employed the first two sections of Maclaurrin Equation as f(x)f(0)+xf(0)f(x) \approx f(0)+xf&#x27;(0), which cut down the error term Rn(n)R_n(n) by definite or infinite iteration.

  • [Dec.14] Additional illustration:

As for a number and its root, we take them as the two variables in the function expression y=f(x)f(x)=x2y=f(x) |_{ f(x) = x^2}, where yy is the number with xx as its root. Certainly, here we define both of them positive. What plays the major role is the mapping relation f(x)=x2f(x)=x^2 with its first derivative f(x)=2xf&#x27;(x)=2x. They can be employed for an infinite estimation to the very accurate root xx, whose estimation xx&#x27; can be operated as the following:
x=limixi1+yx=xi1xi12 x&#x27; =\lim\limits_{i\to\infty} \frac{x_{i-1}+\frac{y|_{x=x_{i-1}}}{x_{i-1}}}{2}

Though an infinite estimation not available as for practice, let’s just shrivel the error to a certain interval. Once the gap between the former xi1x_{i-1} and latter xix_i is less than the interval, the estimation can be shut down. After all, they can be realized by the following code, with acceptable error.

# include <iostream>
# define EPS 1e-3
using namespace std;

double sqrt(double y)
{
	double x = 1, lastX = 0;						// There is factually no restriction to the first assignment, for its only function is to run the loop. You can assign those two value anyway you like, just make sure that they can start the loop.
	while (x-lastX > EPS || lastX-x > EPS) {
		lastX = x;
		x = (x+y/x)/2; }
	return x;
}

int main()
{
	cout << "Put down a number for its root: ";
	int square; cin >> square;
	double root = sqrt(square);
	cout << "Root: " << root << endl;
	return 0;
}

[Mooc] What day is the day? (On debugging, left unsolved)

[Dec.14] The program aims at figuring out the gap of given present time and future time and judge what day is the future day before print it out. Yet there does exist some puzzling troubles BEYOND my ability, far more complex than my comprehension of the integral overflow. Here I’d like to put down the error source code and troubles during debugging, in order to solve it someday.
The program first scan the given present time and future time, calculate the gap between those 2 dates and judge what day is the given day on acceleration of the present weekday.

  • Output window:


    在這裏插入圖片描述

  • The present time scanned in the format of WEEKDAY MONTH DAY YEAR where the future time of MONTH DAY YEAR.

As the illustration displays above, the calculating process is apparently of a terrible mass. I just cannot figure out why the calculation of those two numbers caused integral overflow, even with path which helps with siting the problem.

# include <iostream>
int calendar[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
using namespace std;

void weekdays(int);	

int main()
{
	int year, month, date, y,m,d, days,ds,present;
	cout << "Enter the present date: ";
	cin >> present >> m >> d >> y;
	cout << "Enter a future date: ";
	cin >> month >> date >> year;
	
	cout << "\n[path] Present time: " << present << " " << m << " " << d << " " << y;
	cout << "\n[path] Future time: " << month << " " << date << " " << year << endl;

	for (int i = y; i < year; i++) {
		cout << "[path] year loop activated!\n";
		if ( (!i%4) && i % 100) days += 366;
		else days += 365; 
		cout << "[path] days:" << days << endl;}
	for (int i=m; i<month;i++) {
		cout << "[path] month loop activated!\n";
		if (!year%4&&year%100) calendar[1] = 29;
		days += calendar[i]; 
		cout << "[path] days:" << days << endl; }
	days += (date-d);
	cout << "[path] " << date << " - " << d << " = " << days << endl;
	cout << "[path] days:" << days << endl;

	cout << "[path] model:" << (5+days-ds)%7 << endl;
	weekdays((present+days)%7);
	return 0;
}

void weekdays(int n)
{
	switch(n)
	{
	case 0: cout << "Sunday\n"; cout << "case 0\n"; break;
	case 1: cout << "Monday\n"; cout << "case 1\n"; break;
	case 2: cout << "Tuesday\n"; cout << "case 2\n"; break;
	case 3: cout << "Wednesday\n"; cout << "case 3\n"; break;
	case 4: cout << "Thursday\n"; cout << "case 4\n"; break;
	case 5: cout << "Friday\n"; cout << "case 5\n"; break;
	case 6: cout << "Saturday\n"; cout << "case 6\n";break;
	default: cout << "Illegal input!\n"; break;
	}
}

[Mooc] Recursion of Fibonacci Array (Supervised Edition)

[Dec.15] Recursion can be the most abstract and complex problem as for a program. On the other hand, it is exactly so abstract and complex recursion that achieve a certain function with much ease while editing the source code, by contrast with the great body of logic branch statements. To take command of recursion is nothing easy, and here we will start with the most typical Fibonacci Array.

  • Fibonacci Array:
    1,1,2,3,5,&ThinSpace;,an,an+1,(an+an+1), 1, 1, 2, 3, 5,\cdots,a_n,a_{n+1},(a_n+a_{n+1}),\cdots

  • An abstract code for example:

    在這裏插入圖片描述

If processed, the recalled function lies just like an unfolded infinitely or definitely folio paper (or some other inventory, anything’s just OK). Only when the logic branch receive a false signal does the function stop to call itself, yet it follows its path of recursion where it call itself, or, fold itself folio.
So what about employ such simple yet abstract programming techniques to estimate Fibonacci array? We are certainly acknowledged how Fibonacci array extends and it seems with no better method than enumeration, our program exactly about to perform. The question is how to combine the enumeration from the very beginning and the recursion?
Let’s take the first step of exactly the Fibonacci array, or, the recursion of Fibonacci array. There are supposed to be at least 2 numbers for further calculation of Fibonacci array. Interestingly, if we take the returned value from the function int Fib(int) as the result of (an+an+1)(a_n+a_{n+1}) in the local scope an,an+1,(an+an+1)a_n,a_{n+1},(a_n+a_{n+1}), the repeated process of the function calling is just opposite to the operation in natural mathematics way!

  • natural mathematical way:
a3
a3
a4
a4
a5
a5
A6
A6
1
2
1
3
5
8
  • C++ realization:
    在這裏插入圖片描述

The code of a frightening outlook displays the whole process of recursion. Factually, the complexity of the whole recursion grows exponentially as the recalled times grow. The framework of a folded recursion function is as simple as the following:

int Fib(int n)
{
	if (n==1||n==2) return 1;
	else return Fib(n-1)+Fib(n-2);
}

Yet it will be recalled and unfolded while the function is running, whole prolonged time can be sensed directly. To get a more obvious impression, the program will be supervised with an output of time.

# include <iostream>
using namespace std;
long long unit = 0;

long long fib(long long n)
{
	unit++;
	if (n==1 || n==2) return 1;
	else return fib(n-1)+fib(n-2);
}

int main()
{
	long long m; cin >> m;
	m = fib(m);
	cout << m << endl;
	cout << "The function fib is called " << unit << " times.\n";
	return 0;
}
  • The exponentially growing called times:
    在這裏插入圖片描述

A Brief Introduction to the KaTeX Function

Click KaTeX\KaTeX to view the website introduction or look up the following figure.

  • KaTeX\KaTeX function:
    在這裏插入圖片描述

A Brief Introduction to mermaid

To employ mermaid to better illustrate your view, Click
mermaid
to learn about the grammar of mermain.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章