2009/11/17

[ C/C++ ] Matrix Multiplication by Pointers

矩陣乘法 Matrix Multiplication

// matrixMultiplication.cpp
//
// It's a 2x3 matrices program of matrix multiplication using pointers in C++
//
// Console-based
// Retruns real as well as comples roots.
// Author Jim.lin 2009

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int i, j, k, sum;
    // matrix space
    const int ROW_A = 2, COL_A = 3;
    const int ROW_B = 3, COL_B = 2;

    // define matrix value
    int a[ROW_A][COL_A] = {{1,3,1},{1,2,3}};
    int b[ROW_B][COL_B] = {{1,3},{3,5},{3,3}};
    int c[ROW_A][COL_B];

    // define 3 pointers to first address
    int *pa, *pb, *pc;

    pa = &a[0][0];
    pb = &b[0][0];
    pc = &c[0][0];

    // caclulation
    for ( i=0; i<ROW_A; ++i ) {
        for (j=0; j<COL_B; ++j) {
            sum = 0;
            for( k=0; k<COL_A; ++k ) {
                sum += *(pa+i*COL_A+k) * *(pb+k*COL_B+j);
            }
            *(pc+i*COL_B+j) = sum;
        }
    }
    // print matrix a
    for ( i=0; i<ROW_A; ++i) {
        for (j=0; j<COL_A; ++j) {
            cout << *(pa+i*COL_A+j) << " ";
        }
        cout << endl;
    }
    cout << '\n';

    // print matrix b
    for (i=0; i<ROW_B; ++i) {
        for (j=0; j<COL_B; ++j) {
            cout << *(pb+i*COL_B+j) << " ";
        }
        cout << endl;
    }
    cout << '\n';

    //print multiple matrix c
    for (i=0; i<ROW_A; ++i) {
        for (j=0; j<COL_B; ++j) {
            cout << *(pc+i*COL_B+j) << " ";
        }
        cout << endl;
    }
    return 0;
}


Ref.
http://en.wikipedia.org/wiki/Matrix_multiplication

2009/11/10

[ C/C++ ] Insertion Sort by C++

Insertion sort is an elementary sorting algorithm. The time complexity of Θ(n2).

// insertSort.cpp
//
// Sorting Algorithm (insertion sort) .
//
// Console-based
// Retruns real as well as comples roots.
// Author Jim.lin 2009

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    unsigned int i, j, k, tmp;
    cout << "Please Input a Number :";
    cin >> k;
    vector<int> data(k); //定義一個整數型態k大小的向量陣列

    cout << "Please Input " << k << " Number :";
    for ( i=0; i < data.size(); ++i )
    cin >> data[i];

    // Begin Sorting
    for ( i=1; i < data.size(); ++i )
    {
        for (j=i; j>0; --j)
        {
            if (data[j] >= data[j-1] )
                break;
            else
            {
                tmp = data[j];
                data[j] = data[j-1];
                data[j-1] = tmp;
            }

        }
    }
    // Output Sorted
    cout << "\n Sorted Resulte:";
    for ( i=0; i<data.size(); ++i )
    cout << data[i] << " ";
    cout << endl;

    return 0;
}  

2009/11/02

[ C/C++ ] Transfer to Binary

將輸入整數值轉換成二進位數值

// Binary.cpp
//
// transfer to binary
//
// Console-based
// Author Jim.lin 2009

#include <iostream>
  
using namespace std;
  
int main()
{
int N;
int v = 1;
cin >> N;
while(v <= N/2)
    v = 2*v;   // 此時 v 值為 <= N 的最大次方
  
int n = N;
while(v > 0)
    {
        if (n < v) { cout << "0"; }
        else { cout << "1"; n -= v; }
        v = v/2;
    }
}

2009/10/29

[ C/C++ ] Powers Of Two by C++

使用 C++ 輸出 2 的次方值 Power Of Two

// powersOfTwo.cpp
//
// Powers of Two, 2^i 
//
// Console-based
// Author Jim.lin 2009

#include <iostream>

using namespace std;

int main()
{
    int N;
    int v = 1;
    int i = 0;
    cin >> N;
    while(i <= N)
    {
        cout << i << " " << v << endl;
        v = 2 * v;
        i = i + 1;
    }
}

2009/10/25

[ C/C++ ] Leap Year Check

閏年定義(leap year or intercalary year) : 四年一閏,百年不閏,四百年閏,四千年閏

英文字典 leap year ?
A leap year is a year which has 366 days. The extra day is the 29th February. There is a leap year every four years.

// leapyear.cpp
//
// This program to determine leap year.
//
// Console-based
// Retruns real as well as comples roots.
// Author Jim.lin 2009

#include <iostream>

using namespace std;

int main()
{
    int year;
        cin >> year;
    if(( year%4==0 && year%100!=0 ) || year%400==0 )
        cout << "Leap Year ";
    else
        cout << "Not Leap Year ";
    return 0;
}
Ref. By Google.

2009/10/15

[ C/C++ ] Quadratic by C++

使用 C++ 解一元二次方程式

// quadratic.cpp
//
// This program solves a quadratic equation in standard form.
// ax^2 + bx + c = 0
//
// Console-based
// Retruns real as well as comples roots.
// Author Jim.lin 2009

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

double a,b,c;
double x1,x2;
double i;

// main function
int main()
{
    cout<<"Please Input Number a,b,c:\n";
    cin>>a>>b>>c;
    i=sqrt(b*b-4*a*c);
    if(i<0)
        cout<<"This is imaginary roots";
    else if(i==0)
    {
        x1=-b/(2*a);
        cout<<"The quadratic equation has one root" << x1 << endl;
    }
    else
    {
        x1=(-b+i)/(2*a);
        x2=(-b-i)/(2*a);
        cout<<"The quadratic equation has two roots" << endl;
        cout<<"x1=" << x1 << "  x2=" << x2 << endl;
    }
}
Best Code : http://jblanco_60.tripod.com/c_pp_quadratic.html

2009/06/19

[ FBSD ] FreeNAS Soft RAID 1 Operator

FreeNAS 軟體 RAID 的建立及操作,測試環境為 FreeNAS 0.69.2 Stable

(一) 建立 Soft RAID 1 ( Mirror 1 + 1 = 1 ) , 步驟如下:

1) 除了主系統碟 ad0 之外另外再裝入兩棵 1T Sata 介面的 HD 其磁碟代號為 ad2 及 ad3
2) 開起瀏覽器進入 FreeNAS 的 WebGUI 管理介面
3) 磁碟 > 管理 > + > ( 磁碟 : 選 ad2 , 描述 : 輸入 ad2 disk , S.M.A.R.T. : 打勾, 已格式化之檔案系統 : 選 Software RAID ) > 增加
4) 磁碟 > 管理 > + > ( 磁碟 : 選 ad3 , 描述 : 輸入 ad3 disk , S.M.A.R.T. : 打勾, 已格式化之檔案系統 : 選 Software RAID ) > 增加
5) 套用 : 會看到二棵 HD 都已經 ONLINE
6) 磁碟 > 軟體RAID > + > RAID 1 > ( RAID 名稱 : 輸入 sr1, 提供者 : 按 Ctrl 點選 ad2 及 ad3, 初始化 : 打勾 建立並初始 RAID ) > 增加
7) 套用 : 會看到 sr1 顯示 COMPLETE
8) 磁碟 > 格式化 > ( 磁碟 : 選 sr1, 檔案系統 : 選 UFS(GPT and soft Update), 磁碟標籤 : 輸入 mirror1 ) > 格式化磁碟
9) 磁碟 > 掛載點 > 管理 > + > ( 類別 : 選 Disk, Partition type : GPT partition, Partion number : 輸入 1, 檔案系統 : 選 UFS, Mount point name : 輸入 disk2 ) > 增加
10) 套用 : 會看到 OK 即完成


(二) 更換 Soft RAID1 壞掉的一棵硬碟 , 方式如下:

說明:當 Soft RAID 1 的硬碟 ad3 掛點時我們可以看到在 ( 磁碟 > 軟體RAID > RAID1 > 管理 > 狀態 : 會顯示 DEGRADED )。停機後換上新的硬碟,軟體 RAID 並不會自動重建,所以我們需要自己手動重建 Soft RAID 才行。

1) 完成更換新的硬碟動作,進入 FreeNAS 的網頁管理介面
2) 磁碟 > 軟體RAID > RAID 1 > 工具 ( 執行以下操作 )

Volume 名稱 : 選軟體 RAID 1 的那棵硬碟名稱 sr1
磁碟 : 選原本沒有壞的那一顆 ad2,給它下一個「forget」的命令
接著將換好的新硬碟 ad3 下一個「insert」的命令
最後將換好的新硬碟 ad3 下一個「rebuild」的命令

3) 磁碟 > 軟體RAID > RAID 1 > 工具 > 資訊,就會看到有兩顆硬碟囉!換好的那一顆會有一個訊息 State: SYNCHRONIZING

PS : 會同步很久 @@