Senin, 06 Desember 2010

Factorial Function

When I told to compute the factorial function, I think as a human being.

n! = n x (n-1) x (n-2) x (n-3) x ....


But then how we write a program that compute a factorial? Here's the factorial function


int factorial (int n)
{
int product = 1;
while (n>0)
{
product = n * product;
n--;
}
return product;
}

Kamis, 07 Oktober 2010

Koneksi ke Remote Postgresql Server

Tambahkan IP address pada pg_hba.conf

server# vim /etc/postgresql/8.3/main/pg_hba.conf
host all all 192.168.2.0/24 trust

Sebagai awalan saya set trust saja, untuk menguji coba dulu.

Lalu coba lakukan koneksi sekarang

remote$ psql -h server -U za -d template1
psql: could not connect to server: Connection refused
Is the server running on host "192.168.2.198" and accepting
TCP/IP connections on port 5432?

Eh, masih belum bisa juga. Ternyata masih ada 1 konfigurasi lagi yang perlu ditambahkan, yaitu postgresql.conf

server# vim /etc/postgresql/8.3/main/postgresql.conf
listen_addresses = '192.168.2.198'

Voila, coba lagi
remote$ psql -h server -U za -d template1
Password for user za:
psql (8.4.4, server 8.3.11)
WARNING: psql version 8.4, server version 8.3.
Some psql features might not work.
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.

template1=>

Jumat, 01 Oktober 2010

Multiple C++ Files

For beginners, there's a famous idiom in programming, called hello world! Maybe it's describing how a little baby who had just come out to the world. :-)

It's more common to see this hello world source code:

$ vim hello.cpp
#include

int
main(){
std::cout<<"hello world!\n";
return 0;
}


Compile it with g++ and execute it:

$ g++ -o hello hello.cpp
$ ./hello


It's the basic. How about if we manage to build a complex program? We divide it. We made several files. Here's the hello world with multiple files version. In this example there are three files:


  • helloworld.cpp

  • helloworld.h

  • main.cpp



First, the helloworld.cpp

#include "helloworld.h"
#include
using namespace std;

void hello_world(){
cout << "hello world!\n";
}


Then the helloworld.h

#ifndef _HELLOWORLD_H
#define _HELLOWORLD_H

#include

void hello_world();

#endif


Last, the main.cpp

#include "helloworld.h"
#include
using namespace std;

int main(){
hello_world();
return 0;
}


Then it's time to compile it. How to do it?

g++ -o hello main.cpp helloworld.cpp


Execute it!

$ ./hello
hello world!


Reference, accessed on 2010.10.01 14:20 (GMT+7).