Hey, what's going on?

Archive for the ‘programming’ Category

Repost: 15 Most Important Differences Between C And C++

Posted by Syeilendra Pramuditya on January 25, 2016

Just for my personal archive.

Source: http://studytipsandtricks.blogspot.co.id/2012/05/15-most-important-differences-between-c.html

C C++
1. C is Procedural Language. 1. C++ is non Procedural i.e Object oriented Language.
2. No virtual Functions are present in C 2. The concept of virtual Functions are used in C++.
3. In C, Polymorphism is not possible. 3. The concept of polymorphism is used in C++.
Polymorphism is the most Important Feature of OOPS.
4. Operator overloading is not possible in C. 4. Operator overloading is one of the greatest Feature of C++.
5. Top down approach is used in Program Design. 5. Bottom up approach adopted in Program Design.
6. No namespace Feature is present in C Language. 6. Namespace Feature is present in C++ for avoiding Name collision.
7. Multiple Declaration of global variables are allowed. 7. Multiple Declaration of global varioables are not allowed.
8. In C

  • scanf() Function used for Input.
  • printf() Function used for output.
8. In C++

  • Cin>> Function used for Input.
  • Cout<< Function used for output.
9. Mapping between Data and Function is difficult and complicated. 9. Mapping between Data and Function can be used using “Objects”
10. In C, we can call main() Function through other Functions 10. In C++, we cannot call main() Function through other functions.
11. C requires all the variables to be defined at the starting of a scope. 11. C++ allows the declaration of variable anywhere in the scope i.e at time of its First use.
12. No inheritance is possible in C. 12. Inheritance is possible in C++
13. In C, malloc() and calloc() Functions are used for Memory Allocation and free() function for memory Deallocating. 13.In C++,  new and delete operators are used for Memory Allocating and Deallocating.
14. It supports built-in and primitive data types. 14. It support both built-in and user define data types.
15. In C, Exception Handling is not present. 15. In C++, Exception Handling is done with Try and Catch block.

Posted in programming | Leave a Comment »

HTML form Autosave with Sisyphus.js: How to use it?

Posted by Syeilendra Pramuditya on July 13, 2015

Step 1

Download the source code from http://sisyphus-js.herokuapp.com

Step 2

Prepare a file named autosave.htm

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="sisyphus.min.js"></script>
<script>
$( function() {
$( "#form2" ).sisyphus(); //Autosave form2 only,
// or you can persist all forms data at one command
// $( "form" ).sisyphus();
} );
</script>
</head>
<body>

Form 1
<form action="" id="form1" name="form1" method="post">
Name:<br />
<input type="text" name="name" value="" /><br />
E-mail:<br />
<input type="text" name="mail" value="" /><br />
Comment:<br />
<input type="text" name="comment" value="" size="50" />
<br /><br />
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>

<br>
Form 2
<form action="" id="form2" name="form2" method="post">
Name:<br />
<input type="text" name="name" value="" /><br />
E-mail:<br />
<input type="text" name="mail" value="" /><br />
Comment:<br />
<input type="text" name="comment" value="" size="50" />
<br /><br />
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>

</body>
</html>


Step 3

Put the file in your web server and test it: open the page using browser, write something in the form, close the page, and open again. Watch how the magic works!

Posted in programming | Tagged: , , , | Leave a Comment »

Pemecahan persamaan difusi neutron 1D single-group secara numerik dalam c++

Posted by Syeilendra Pramuditya on June 11, 2014

//Pemecahan persamaan difusi neutron 1D single-group secara numerik dalam c++
//Created: 11 Juni 2014
//Last Modified: -
//Programmer: Syeilendra Pramuditya

#include <iostream>
#include <cmath>
using namespace std;

float findflux(float a, float b, float c, float d, float e, float f){
float out1;
//Diturunkan dari persamaan difusi neutron 1D single-group
//Metode finite difference
out1=(((a+b)/(c*c))+(d/e)) / ((f/e)+(2/(c*c)));
return out1;
}

int main(){

int nx,itermax,i,iter,konvergen;
float L,dx,sigma_a,D,errormax,error,dummy;
float FluxOld[50],FluxNew[50],S[50];

//=============input===================
nx=50;//jumlah partisi
L=3;//dimensi domain
dx=L/nx;//lebar partisi
sigma_a=1;//cross section makroskopik
D=1.0/6;//koefisien difusi
errormax=1e-3;//kriteria konvergensi
itermax=1000;//loop limit
//=====================================

//==========syarat batas===============
FluxOld[0]=0; //flux di x=0 harus nol
FluxOld[nx]=0; //flux di x=L harus nol
FluxNew[0]=0; //flux di x=0 harus nol
FluxNew[nx]=0; //flux di x=L harus nol
S[0]=0; //source di x=0 harus nol
S[nx]=0; //source di x=L harus nol
//=====================================

//==========tebakan awal===============
for (i=1; i<=nx-1; i++){
FluxOld[i]=300; //tebakan awal flux
S[i]=100; //tebakan awal source
};
//=====================================

iter=0;
do{
iter++;
konvergen=1;
	for(i=1;i<=nx-1;i++){
	FluxNew[i]=findflux(FluxOld[i+1],FluxOld[i-1],dx,S[i],D,sigma_a); //Jacobi	
	}
	
	for(i=0;i<=nx;i++){
	error=fabs(FluxNew[i]-FluxOld[i]);
	if (error > errormax) konvergen=0; 
	}
	
	for(i=1;i<=nx-1;i++){FluxOld[i]=FluxNew[i];}
cout<<"Iterasi "<<iter<<"\n";
}while(konvergen==0 && iter<itermax);

cout<<"\n";
cout<<"Distribusi Flux\n";
for (i=0; i<=nx; i++){
cout<<FluxNew[i]<<"\n";
}

}

Posted in nuclear engineering, programming | Leave a Comment »

Numerical Methods in Pascal: Polynomial Root Finder

Posted by Syeilendra Pramuditya on April 29, 2014

program polinomial; {Mencari seluruh akar polinom}
{https://syeilendrapramuditya.wordpress.com}
uses crt;

const
  max = 100;
var
  n  : integer;
  a  : array[-1..max] of real;
  b  : array[-1..max] of real;
  iter,i,j,k   : longint;
  x0,x1,t,beda : real;

begin
     clrscr;
     writeln;
     writeln('           Program Polinom');
     writeln('           ===============');
     writeln;
     write('Masukan nilai tebakan awal (misal : 5) : ');
     iter := 0;
     readln(x0);
     write('Masukan orde polinomial : ');readln(n);
     { misal (x-1)*(x+1)*(x+2) = x^3+2*x^2-x-2 }
     writeln('Masukan satu per satu koefisien polinom');
     writeln('dimulai dari suku polinom dengan orde terendah (x pangkat nol)');
     for i := 0 to n do
     begin
       write('masukan koefisien ke ',i,' : ');
       readln(a[i]);
     end;
     writeln;
     repeat
       repeat
           iter := iter +1;
          b[n-1] := a[n];
          t := b[n-1];
          for j := n downto 2 do
          begin
            b[j-2] := b[j-1]*x0+a[j-1];
            t := t*x0+b[j-2];
          end;
          b[-1] := b[0]*x0 + a[0];
          x1 := x0 - b[-1]/t;
          beda := (x1-x0)/x0;
          x0 := x1;
       until (abs(beda)<1e-6)or (iter>1e6);
     n := n-1;
     writeln('nilai x',n+1, ' adalah',x1:10:4 );
     for k := 0 to n do a[k] := b[k];
     until (n<=0);
     readln;
end.

Posted in programming | Leave a Comment »

Numerical Methods in Pascal: Euler 2D

Posted by Syeilendra Pramuditya on April 28, 2014

Program Planetary_Motion;
{https://syeilendrapramuditya.wordpress.com}
{Gerak bulan mengitari bumi karena pengaruh gaya gravitasi}
{Data t[i],x[i],y[i],vx[i],vy[i] ditulis ke dalam file excel (D:\orbit.xls) }
{Data D:\orbit.xls tersebut kemudian diplot menggunakan Excel}
uses crt;
const GM = 3.986344692e14;
Type
 DataPlot = record
            dataT,dataX,dataY,dataVX,dataVY : real;
            end;
 ListData = array[0..1000] of DataPlot;
var t0,x0,y0,vx0,vy0,m,k,b,t,x,y,vx,vy,x12,y12,vx12,vy12,dt : real;
    i,N : integer;
    FileData : text;
    S:ListData;

Begin
clrscr;
writeln;
writeln('      Solusi Numerik PDB Orde 2 Terkopling');
writeln('              Kasus  Gerak  Bulan         ');
writeln;
writeln('Nilai konstanta yang digunakan dalam perhitungan :');
writeln('Massa Bumi  = 5.9742e24 Kg               ');
writeln('Massa Bulan = 7.3500e24 Kg               ');
writeln('d           = 3.844e5 Km                 ');
writeln('T           = 27.322 hari                ');
writeln('G           = 6.6726e-11 N*sqr(m)/sqr(Kg ');
writeln;
writeln('Nilai parameter (default) yang digunakan dalam perhitungan :');
writeln;
writeln('x0  = 3.844e5 ');
writeln('y0  = 0       ');
writeln('vx0 = 0       ');
writeln('vy0 = 32000   ');
writeln('dt  = 1       ');
writeln('N   = 1000    ');
writeln;
write('Masukan nilai x0,y0,vx0,vy0 pada t = 0  : ');readln(x0,y0,vx0,vy0);
write('Masukan parameter iterasi (dt dan N) : ');readln(dt,N);
writeln;
t0 := 0;
Assign(FileData,'D:\orbit.xls');
rewrite(FileData);
 S[0].DataT  := 0;
 S[0].DataX  := x0;
 S[0].DataY  := y0;
 S[0].DataVX := vx0;
 S[0].DataVY := vy0;
 writeln(FileData,'      t               x               y               vx               vy');
 writeln(FileData,s[0].dataT:20:5,s[0].dataX:20:5,s[0].DataY:20:5,s[0].DataVX:20:5,s[0].DataVY:20:5);
for i := 1 to N do
 begin
 x12  := x0 + (dt/2) * vx0;
 vx12 := vx0 - ( (GM * x0) /  ( exp(1.5 * ln( sqr(x0) + sqr(y0) ))) ) * (dt/2);
 y12  := y0 + (dt/2) * vy0;
 vy12 := vy0 - ( (GM * y0) /  ( exp(1.5 * ln( sqr(x0) + sqr(y0) ))) ) * (dt/2);
 x  := x0 + dt * vx12;
 vx := vx0 - ( (GM * x12)  /  (exp(1.5 * ln(sqr(x0) + sqr(y0) )))) * (dt);
 y  := y0 + dt * vy12;
 vy := vy0 - ( (GM * y12)  /  (exp(1.5 * ln(sqr(x0) + sqr(y0) )))) * (dt);
 t := t0 + dt;
 S[i].DataT  :=  t;
 S[i].DataX  :=  x;
 S[i].DataY  :=  y;
 S[i].DataVX := vx;
 S[i].DataVY := vy;
 writeln(FileData,s[i].dataT:20:5,s[i].dataX:20:5,s[i].DataY:20:5,s[i].DataVX:20:5,s[i].DataVY:20:5);
 t0  :=  t;
 x0  :=  x;
 y0  :=  y;
 vx0 := vx;
 vy0 := vy;
 end;
close(FileData);
writeln('Data t,x,y,vx,vy telah disimpan di D:\orbit.xls , silahkan buka file tersebut');
readln;
end.

Posted in programming | Leave a Comment »

Numerical Methods in Pascal: Modified Euler

Posted by Syeilendra Pramuditya on April 28, 2014

Program Modified_Euler;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
var h,x0,y0,x12,y12,x,y : real;
    i,N : integer;

Function Fxy(x,y:real):real;
begin
Fxy := sqrt(x*x + y*y);
end;

Begin
clrscr;
write('Masukan nilai x0 dan y0 : ');readln(x0,y0);
write('Masukan nilai h dan N   : ');readln(h,N);
x:=x0;
for i := 1 to N do
 begin
 x12 := x0 + h/2;
 y12 := y0 + (h/2) * Fxy(x0,y0);
 x := x0 + h;
 y := y0 + h*Fxy(x12,y12);
 writeln('x = ',x:10:5,'    y = ',y:10:5);
 x0:=x;
 y0:=y;
 end;
readln;
end.

Posted in programming | Leave a Comment »

Numerical Methods in Pascal: Simple Euler

Posted by Syeilendra Pramuditya on April 28, 2014

Program Simple_Euler;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
var h,x0,y0,x,y : real;
    i,N : integer;

Function Fxy(x,y:real):real;
begin
Fxy := sqrt(x*x + y*y);
end;

Begin
clrscr;
write('Masukan nilai x0 dan y0 : ');readln(x0,y0);
write('Masukan nilai h dan N   : ');readln(h,N);
x:=x0;
for i := 1 to N do
 begin
 x := x0 + h;
 y := y0 + h*Fxy(x0,y0);
 writeln('x = ',x:10:5,'    y = ',y:10:5);
 x0:=x;
 y0:=y;
 end;
readln;
end.

Posted in programming | Leave a Comment »

Numerical Methods in Pascal: Root Finding by Hybrid Bisection-NR

Posted by Syeilendra Pramuditya on April 25, 2014

Program Hybrid; {Bisection - Newton Rhapson Combined}
{https://syeilendrapramuditya.wordpress.com}
uses crt;
Function f(x:real):real;
begin
f := sqr(x) - 9;
end;

Function df(x:real):real;
begin
df:=2*x;
end;

Procedure BacaInput(var a,b:real);
begin
clrscr;
repeat
write('Masukan interval evaluasi : ');readln(a,b);
until(f(a)*f(b) < 0);
end;

Procedure Bisection(var a,b,newrootB:real);
begin
NewRootB := (a+b)/2;
IF (f(a)*f(newrootB) < 0) then
b:=newrootB
else
a := newrootB;
end;

Procedure NewtonRhapson(var b:real;var newrootNR:real);
begin
newrootNR := b - f(b)/df(b);
end;

Procedure TulisOutput(newrootB:real);
begin
writeln('Akar : ',newrootB:10:2);
end;

var a,b,c,d,newrootNR,newrootB : real;
    iterNR,iterBi,iter:integer;

Begin
iterNR := 0;iterBi:=0;
BacaInput(a,b);
c:=a;d:=b;
Repeat
begin
NewtonRhapson(b,newrootNR);
IF (newrootNR >= c) and (newrootNR <= d) then
  begin
  inc(iterNR);
  b := newrootNR;
  NewtonRhapson(b,newrootNR);writeln('Newton Rhapson');
  end
else
  begin
   inc(iterBi);
   writeln('Bisection');
   Bisection(a,b,NewrootB);
   end;
end;
Until (abs(f(newrootNR)) < 1e-6) OR (abs(f(newrootB)) < 1e-6)  OR (iter = 10000);
writeln;
if abs(f(newrootNR)) < abs(f(newrootB)) then
writeln('Akar = ',newrootNR:10:2)
else
writeln('Akar = ',newrootB:10:2);
writeln;
writeln('Iterasi Bisection      : ',iterBi);
writeln('Iterasi Newton Rhapson : ',iterNR);
writeln('Iterasi Total          : ',iterBi + iterNR);
readln;
end.

Posted in programming | Leave a Comment »

Numerical Methods in Pascal: Root Finding by Regula False (RF)

Posted by Syeilendra Pramuditya on April 25, 2014

Program RegulaFalse;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
Function f(x:real):real;
begin
f := sqr(x) - 9;
end;

Procedure BacaInput(var a,b : real);
begin
repeat
write('Masukan batas kiri dan kanan : ');readln(a,b);
until (f(a)*f(b) < 0);
end;

Procedure Proses(a,b:real;var akar:real;var iter : integer);
var err : real;
begin
iter := 0;
repeat
inc(iter);
akar := a - f(a)*( (b-a) / (f(b) - f(a)) );
err := abs(f(akar));
if (f(a) * f(akar) < 0) then
b := akar
else
a := akar;
until (err < 1e-6) OR (iter = 10000);
end;

Procedure TulisOutput(akar:real);
begin
writeln('Akar = ',akar:10:3);
end;

var a,b,akar : real;
    iter : integer;

begin
clrscr;
BacaInput(a,b);
Proses(a,b,akar,iter);
TulisOutput(akar);
writeln('Iterasi = ',iter:10);
readln;
end.

Posted in programming | Leave a Comment »

Numerical Methods in Pascal: Root Finding by Newton-Rhapson (NR)

Posted by Syeilendra Pramuditya on April 25, 2014

Program NewtonRhapson;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
Function f(x:real):real;
begin
f:=sqr(x) - 9;
end;

Function df(x:real):real;
begin
df:=2*x;
end;

Procedure BacaInput(var x:real);
begin
write('Masukan tebakan awal : ');readln(x)
end;

Procedure Proses(var x:real;var iter :integer);
var err:real;
begin
iter := 0;
repeat
 begin
 inc(iter);
 x := x - f(x)/df(x);
 end;
until(abs(f(x)) < 1e-6);
end;

Procedure TulisOutput(x1:real);
begin
writeln('Akarnya adalah : ',x1:10:2);
end;

var x,x1:real;
    iter : integer;
begin
clrscr;
BacaInput(x);
Proses(x,iter);
TulisOutput(x);
writeln('Iterasi = ',iter:10);
readln;
end.

Posted in programming | Leave a Comment »

Numerical Methods in Pascal: Root Finding by Bisection

Posted by Syeilendra Pramuditya on April 25, 2014

Program Bisection;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
Function f(x:real):real;
begin
f := sqr(x) - 9;
end;

Procedure BacaInput(var a,b:real);
begin
repeat
write('Masukan batas kiri dan kanan : ');
Readln(a,b);
until (f(a)*f(b) < 0);
end;

Procedure Proses(a,b:real;var NewRoot:real;var iter :integer);

begin
iter:=0;
Repeat
begin
 inc(iter);
 NewRoot:=(a+b)/2;
 IF (f(a)*f(NewRoot) < 0) then
 b := NewRoot
 else
 a:=NewRoot;
 end;
until (abs(f(NewRoot)) < 1e-6) OR (iter = 10000);
end;

Procedure TulisAkar(NewRoot:real);
begin
writeln('Akar dari persamaan adalah : ',NewRoot:10:2);
end;

var a,b,NewRoot : real;
    iter:integer;
Begin
clrscr;
BacaInput(a,b);
Proses(a,b,NewRoot,iter);
TulisAkar(NewRoot);
writeln('Iterasi = ',iter:10);
readln;
end.

Posted in programming | Leave a Comment »

Returning Array from a Function in C++ (using pointer)

Posted by Syeilendra Pramuditya on April 17, 2014

/*
Returning Array from a Function in C++ (using pointer)
Source: http://stackoverflow.com/questions/3473438/c-return-array-in-a-function
*/
#include <iostream>
using namespace std;

int * fill_array(int array1[],int n){
int i;
    for (i=0;i<n;i++){
    array1[i]=3*i;
    }
return(array1);
}

int main(){
int const n=5;
int * array1;
int i,a[5];
array1=fill_array(a,n);

    for (i=0;i<n;i++){
    cout<<array1[i]<<endl;
    }

}

 

Posted in programming | Leave a Comment »

SPWebTemplate – A Flexible Template to Build Any Website

Posted by Syeilendra Pramuditya on September 1, 2013

SPWebTemplate

SPWebTemplate

This is a basic web template which can be used to develop any type of website.

It is written in PHP and HTML, and using MySQL database (to be precise, it also uses CSS and JavaScript).

The system has been derived from Pusat Data PPI Tokodai and PPI Jepang Publication (the author helped to initialize these two systems).

To install the template, follow these simple steps:

  1. unzip and copy all the files to your server
  2. create a MySQL database (you can use PHPMyAdmin interface to do this)
  3. edit a PHP file system/config.php according to your database setting
  4. with a web browser, open http://your-server-address.com/install
  5. that’s it.

Get the software here.

Posted in programming, software & simulation | Tagged: | Leave a Comment »