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