SlideShare a Scribd company logo
1. Program untuk memperkirakan nilai Integral dari fungsi yang diberikan
menggunakan aturan kuadrat Gaussian di C ++ Programming
# include <iostream.h>
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <conio.h>
# include <math.h>
constint max_size=13;
int n=0;
int top=-1;
longdouble a=-1;
longdouble b=1;
longdouble two_point_result=0;
longdouble three_point_result=0;
char Fx[100]={NULL};
char Stack[30][30]={NULL};
char Postfix_expression[30][30]={NULL};
/*************************************************************************//
*************************************************************************///
------------------------ Funcion Prototypes -------------------------
///*************************************************************************
//*************************************************************************/
void push(constchar *);
void convert_ie_to_pe(constchar *);
constchar* pop( );
constlongdouble evaluate_postfix_expression(constlongdouble);
void show_screen( );
void clear_screen( );
void get_input( );
void change_limits( );
void apply_gaussian_quadrature_rule( );
void show_result( );
/*************************************************************************//
*************************************************************************///
------------------------------ main( ) ------------------------------
///*************************************************************************
//*************************************************************************/
int main( )
{
clrscr( );
textmode(C4350);
show_screen( );
get_input( );
show_result( );
getch( );
return 0;
}
/*************************************************************************//
*************************************************************************///
------------------------ Funcion Definitions ------------------------
///*************************************************************************
//*************************************************************************/
/*************************************************************************//
/-------------------------- show_screen( ) ---------------------------
///*************************************************************************
/void show_screen( )
{
cprintf("n******************************************************************
**************");
cprintf("***************************- -
**************************");
cprintf("*--------------------------- ");
textbackground(1);
cprintf("Integrasi numerik ");
textbackground(8);
cprintf(" --------------------------*");
cprintf("*-*************************- -
************************-*");
cprintf("*-
***************************************************************************
*-*");
for(int count=0;count<42;count++)
cprintf("*-* *-*");
gotoxy(1,46);
cprintf("*-
***************************************************************************
*-*");
cprintf("*------------------------------------------------------------------------------*");
cprintf("********************************************************************
************");
gotoxy(1,2);
}
/*************************************************************************//
/------------------------- clear_screen( ) ---------------------------
///*************************************************************************
/void clear_screen( )
{
for(int count=0;count<37;count++)
{
gotoxy(5,8+count);
cout<<" ";
}
gotoxy(1,2);
}
/*************************************************************************//
/-------------------------- push(const char*) ------------------------
///*************************************************************************
/void push(constchar* Operand)
{
if(top==(max_size-1))
{
cout<<"Error : Stack is full."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
top++;
strcpy(Stack[top],Operand);
}
}
/*************************************************************************//
/------------------------------ pop( ) -------------------------------
///*************************************************************************
/constchar* pop( )
{
char Operand[40]={NULL};
if(top==-1)
{
cout<<"Error : Stack is empty."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
strcpy(Operand,Stack[top]);
strset(Stack[top],NULL);
top--;
}
return Operand;
}
/*************************************************************************//
/-------------------- convert_ie_to_pe(const char*) ------------------
///*************************************************************************
/void convert_ie_to_pe(constchar* Expression)
{
char Infix_expression[100]={NULL};
char Symbol_scanned[30]={NULL};
push("(");
strcpy(Infix_expression,Expression);
strcat(Infix_expression,"+0)");
int flag=0;
int count_1=0;
int count_2=0;
int equation_length=strlen(Infix_expression);
if(Infix_expression[0]=='(')
flag=1;
do
{
strset(Symbol_scanned,NULL);
if(flag==0)
{
int count_3=0;
do
{
Symbol_scanned[count_3]=Infix_expression[count_1];
count_1++;
count_3++;
}
while(count_1<=equation_length &&
Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!=')');
flag=1;
}
elseif(flag==1)
{
Symbol_scanned[0]=Infix_expression[count_1];
count_1++;
if(Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!=')')
flag=0;
if(Infix_expression[count_1-1]=='(' &&
(Infix_expression[count_1]=='-' ||
Infix_expression[count_1]=='+'))
flag=0;
}
if(strcmp(Symbol_scanned,"(")==0)
push("(");
elseif(strcmp(Symbol_scanned,")")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
pop( );
}
elseif(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
if(strcmp(Symbol_scanned,"^")==0)
{ }
elseif(strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
while(strcmp(Stack[top],"^")==0 ||
strcmp(Stack[top],"*")==0 ||
strcmp(Stack[top],"/")==0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
}
elseif(strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
}
push(Symbol_scanned);
}
else
{
strcat(Postfix_expression[count_2],Symbol_scanned);
count_2++;
}
}
while(strcmp(Stack[top],NULL)!=0);
strcat(Postfix_expression[count_2],"=");
count_2++;
}
/*************************************************************************//
/---------- evaluate_postfix_expression(const long double) -----------
///*************************************************************************
/constlongdouble evaluate_postfix_expression(constlongdouble x)
{
longdouble function_value=0;
int count_1=-1;
char Symbol_scanned[30]={NULL};
do
{
count_1++;
strcpy(Symbol_scanned,Postfix_expression[count_1]);
if(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
char Result[30]={NULL};
char Operand[2][30]={NULL};
strcpy(Operand[0],pop( ));
strcpy(Operand[1],pop( ));
longdouble operand[2]={0};
longdouble result=0;
char *endptr;
for(int count_2=0;count_2<2;count_2++)
{
int flag=0;
if(Operand[count_2][0]=='-')
{
int length=strlen(Operand[count_2]);
for(int count_3=0;count_3<(length-1);count_3++)
Operand[count_2][count_3]=Operand[count_2][(count_3+1)];
Operand[count_2][count_3]=NULL;
flag=1;
}
if(strcmp(Operand[count_2],"x")==0)
operand[count_2]=x;
elseif(strcmp(Operand[count_2],"e")==0)
operand[count_2]=2.718282;
elseif(strcmp(Operand[count_2],"sinx")==0)
operand[count_2]=sinl(x);
elseif(strcmp(Operand[count_2],"cosx")==0)
operand[count_2]=cosl(x);
elseif(strcmp(Operand[count_2],"tanx")==0)
operand[count_2]=tanl(x);
elseif(strcmp(Operand[count_2],"lnx")==0)
operand[count_2]=logl(x);
elseif(strcmp(Operand[count_2],"logx")==0)
operand[count_2]=log10l(x);
else
operand[count_2]=strtod(Operand[count_2],&endptr);
if(flag)
operand[count_2]*=-1;
}
switch(Symbol_scanned[0])
{
case'^' : result=powl(operand[1],operand[0]);
break;
case'*' : result=operand[1]*operand[0];
break;
case'/' : result=operand[1]/operand[0];
break;
case'+' : result=operand[1]+operand[0];
break;
case'-' : result=operand[1]-operand[0];
break;
}
gcvt(result,25,Result);
push(Result);
}
elseif(strcmp(Symbol_scanned,"=")!=0)
push(Symbol_scanned);
}
while(strcmp(Symbol_scanned,"=")!=0);
char Function_value[30]={NULL};
char *endptr;
strcpy(Function_value,pop( ));
function_value=strtod(Function_value,&endptr);
return function_value;
}
/*************************************************************************//
/----------------------------- get_input( ) --------------------------
///*************************************************************************
/void get_input( )
{
clear_screen( );
gotoxy(6,10);
cout<<"Non-Linear Function :";
gotoxy(6,11);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(6,37);
cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";
gotoxy(6,40);
cout<<"Available Operators : ^ (raised to power) , * , / , + , -";
gotoxy(6,42);
cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";
gotoxy(6,44);
cout<<" n = any number";
gotoxy(6,14);
cout<<"Enter the Function : f(x) = ";
cin>>Fx;
convert_ie_to_pe(Fx);
}
/*************************************************************************//
/----------------- apply_gaussian_quadrature_rule( ) -----------------
///*************************************************************************
/void apply_gaussian_quadrature_rule( )
{
longdouble temp_1=0;
longdouble temp_2=0;
longdouble temp_3=0;
temp_1=evaluate_postfix_expression((-sqrtl(0.6)));
temp_1*=(0.555555556);
temp_2=evaluate_postfix_expression(0);
temp_2*=(0.888888889);
temp_3=evaluate_postfix_expression(sqrtl(0.6));
temp_3*=(0.555555556);
three_point_result=(temp_1+temp_2+temp_3);
temp_1=0;
temp_2=0;
temp_1=evaluate_postfix_expression((-1/sqrtl(3)));
temp_2=evaluate_postfix_expression((1/sqrtl(3)));
two_point_result=(temp_1+temp_2);
}
/*************************************************************************//
/---------------------------- show_result( ) -------------------------
///*************************************************************************
/void show_result( )
{
clear_screen( );
gotoxy(6,9);
cout<<"Gaussian Quadrature Rule:";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(24,12);
cout<<" 1ô";
gotoxy(6,13);
cout<<"Two Point Rule: ³f(x)dx ÷ f(-1/û3)+f(1/û3)";
gotoxy(24,14);
cout<<"-1õ";
gotoxy(24,16);
cout<<" 1ô";
gotoxy(6,17);
cout<<"Three Point Rule: ³f(x)dx ÷ (5/9)f(-û(3/5))+(8/9)f(0)+(5/9)f(û(3/5))";
gotoxy(24,18);
cout<<"-1õ";
apply_gaussian_quadrature_rule( );
gotoxy(6,22);
cout<<" 1ô";
gotoxy(6,24);
cout<<" -1õ";
gotoxy(6,23);
cout<<"Estimation of ³f(x)dx :";
gotoxy(6,25);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,27);
cout<<"Estimated Integral Value (using 2-Point Rule) = "<<two_point_result;
gotoxy(8,29);
cout<<"Estimated Integral Value (using 3-Point Rule) = "<<three_point_result;
gotoxy(1,2);
}
2. Program untuk memperkirakan nilai Integral dari fungsi di poin yang diberikan dari
data yang diberikan dengan Metode Romberg di C ++ Programming
# include <iostream.h>
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <conio.h>
# include <math.h>
constint max_size=8;
int n=0;
int top=-1;
longdouble h=0;
longdouble a=0;
longdouble b=0;
longdouble ri[max_size][max_size]={0};
longdouble xn[max_size]={0};
longdouble fx[max_size]={0};
char Fx[100]={NULL};
char Stack[30][30]={NULL};
char Postfix_expression[30][30]={NULL};
/*************************************************************************//
*************************************************************************///
------------------------ Funcion Prototypes -------------------------
///*************************************************************************
//*************************************************************************/
void push(constchar *);
void convert_ie_to_pe(constchar *);
constchar* pop( );
constlongdouble evaluate_postfix_expression(constlongdouble);
void show_screen( );
void clear_screen( );
void get_input( );
void apply_trapezoidal_rule( );
void apply_romberg_method( );
void show_result( );
/*************************************************************************//
*************************************************************************///
------------------------------ main( ) ------------------------------
///*************************************************************************
//*************************************************************************/
int main( )
{
clrscr( );
textmode(C4350);
show_screen( );
get_input( );
apply_trapezoidal_rule( );
apply_romberg_method( );
show_result( );
getch( );
return 0;
}
/*************************************************************************//
*************************************************************************///
------------------------ Funcion Definitions ------------------------
///*************************************************************************
//*************************************************************************/
/*************************************************************************//
/-------------------------- show_screen( ) ---------------------------
///*************************************************************************
/void show_screen( )
{
cprintf("n******************************************************************
**************");
cprintf("****************************- -
***************************");
cprintf("*---------------------------- ");
textbackground(1);
cprintf(" Romberg Integration ");
textbackground(8);
cprintf(" ---------------------------*");
cprintf("*-**************************- -
*************************-*");
cprintf("*-
***************************************************************************
*-*");
for(int count=0;count<42;count++)
cprintf("*-* *-*");
gotoxy(1,46);
cprintf("*-
***************************************************************************
*-*");
cprintf("*------------------------------------------------------------------------------*");
cprintf("********************************************************************
************");
gotoxy(1,2);
}
/*************************************************************************//
/------------------------- clear_screen( ) ---------------------------
///*************************************************************************
/void clear_screen( )
{
for(int count=0;count<37;count++)
{
gotoxy(5,8+count);
cout<<" ";
}
gotoxy(1,2);
}
/*************************************************************************//
/-------------------------- push(const char*) ------------------------
///*************************************************************************
/void push(constchar* Operand)
{
if(top==(max_size-1))
{
cout<<"Error : Stack is full."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
top++;
strcpy(Stack[top],Operand);
}
}
/*************************************************************************//
/------------------------------ pop( ) -------------------------------
///*************************************************************************
/constchar* pop( )
{
char Operand[40]={NULL};
if(top==-1)
{
cout<<"Error : Stack is empty."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
strcpy(Operand,Stack[top]);
strset(Stack[top],NULL);
top--;
}
return Operand;
}
/*************************************************************************//
/-------------------- convert_ie_to_pe(const char*) ------------------
///*************************************************************************
/void convert_ie_to_pe(constchar* Expression)
{
char Infix_expression[100]={NULL};
char Symbol_scanned[30]={NULL};
push("(");
strcpy(Infix_expression,Expression);
strcat(Infix_expression,"+0)");
int flag=0;
int count_1=0;
int count_2=0;
int equation_length=strlen(Infix_expression);
if(Infix_expression[0]=='(')
flag=1;
do
{
strset(Symbol_scanned,NULL);
if(flag==0)
{
int count_3=0;
do
{
Symbol_scanned[count_3]=Infix_expression[count_1];
count_1++;
count_3++;
}
while(count_1<=equation_length &&
Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!=')');
flag=1;
}
elseif(flag==1)
{
Symbol_scanned[0]=Infix_expression[count_1];
count_1++;
if(Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!=')')
flag=0;
if(Infix_expression[count_1-1]=='(' &&
(Infix_expression[count_1]=='-' ||
Infix_expression[count_1]=='+'))
flag=0;
}
if(strcmp(Symbol_scanned,"(")==0)
push("(");
elseif(strcmp(Symbol_scanned,")")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
pop( );
}
elseif(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
if(strcmp(Symbol_scanned,"^")==0)
{ }
elseif(strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
while(strcmp(Stack[top],"^")==0 ||
strcmp(Stack[top],"*")==0 ||
strcmp(Stack[top],"/")==0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
}
elseif(strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
}
push(Symbol_scanned);
}
else
{
strcat(Postfix_expression[count_2],Symbol_scanned);
count_2++;
}
}
while(strcmp(Stack[top],NULL)!=0);
strcat(Postfix_expression[count_2],"=");
count_2++;
}
/*************************************************************************//
/---------- evaluate_postfix_expression(const long double) -----------
///*************************************************************************
/constlongdouble evaluate_postfix_expression(constlongdouble x)
{
longdouble function_value=0;
int count_1=-1;
char Symbol_scanned[30]={NULL};
do
{
count_1++;
strcpy(Symbol_scanned,Postfix_expression[count_1]);
if(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
char Result[30]={NULL};
char Operand[2][30]={NULL};
strcpy(Operand[0],pop( ));
strcpy(Operand[1],pop( ));
longdouble operand[2]={0};
longdouble result=0;
char *endptr;
for(int count_2=0;count_2<2;count_2++)
{
int flag=0;
if(Operand[count_2][0]=='-')
{
int length=strlen(Operand[count_2]);
for(int count_3=0;count_3<(length-1);count_3++)
Operand[count_2][count_3]=Operand[count_2][(count_3+1)];
Operand[count_2][count_3]=NULL;
flag=1;
}
if(strcmp(Operand[count_2],"x")==0)
operand[count_2]=x;
elseif(strcmp(Operand[count_2],"e")==0)
operand[count_2]=2.718282;
elseif(strcmp(Operand[count_2],"sinx")==0)
operand[count_2]=sinl(x);
elseif(strcmp(Operand[count_2],"cosx")==0)
operand[count_2]=cosl(x);
elseif(strcmp(Operand[count_2],"tanx")==0)
operand[count_2]=tanl(x);
elseif(strcmp(Operand[count_2],"lnx")==0)
operand[count_2]=logl(x);
elseif(strcmp(Operand[count_2],"logx")==0)
operand[count_2]=log10l(x);
else
operand[count_2]=strtod(Operand[count_2],&endptr);
if(flag)
operand[count_2]*=-1;
}
switch(Symbol_scanned[0])
{
case'^' : result=powl(operand[1],operand[0]);
break;
case'*' : result=operand[1]*operand[0];
break;
case'/' : result=operand[1]/operand[0];
break;
case'+' : result=operand[1]+operand[0];
break;
case'-' : result=operand[1]-operand[0];
break;
}
gcvt(result,25,Result);
push(Result);
}
elseif(strcmp(Symbol_scanned,"=")!=0)
push(Symbol_scanned);
}
while(strcmp(Symbol_scanned,"=")!=0);
char Function_value[30]={NULL};
char *endptr;
strcpy(Function_value,pop( ));
function_value=strtod(Function_value,&endptr);
return function_value;
}
/*************************************************************************//
/----------------------------- get_input( ) --------------------------
///*************************************************************************
/void get_input( )
{
do
{
clear_screen( );
gotoxy(6,9);
cout<<"Input :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍ";
gotoxy(37,13);
cout<<"[ n = 2,4,8 ]";
gotoxy(6,12);
cout<<"Enter the max. number of sub-intervals = n = ";
cin>>n;
if(n!=2 && n!=4 && n!=8)
{
gotoxy(12,25);
cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";
gotoxy(12,26);
cout<<" to try again.";
n=int(getche( ));
if(n==27)
exit(0);
}
}
while(n!=2 && n!=4 && n!=8);
gotoxy(6,16);
cout<<"Enter the value of Lower limit = a = ";
cin>>a;
gotoxy(6,18);
cout<<"Enter the value of Upper Limit = b = ";
cin>>b;
gotoxy(25,43);
cout<<"Press any key to continue...";
getch( );
clear_screen( );
gotoxy(6,10);
cout<<"Non-Linear Function :";
gotoxy(6,11);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(6,37);
cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";
gotoxy(6,40);
cout<<"Available Operators : ^ (raised to power) , * , / , + , -";
gotoxy(6,42);
cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";
gotoxy(6,44);
cout<<" n = any number";
gotoxy(6,14);
cout<<"Enter the Function : f(x) = ";
cin>>Fx;
convert_ie_to_pe(Fx);
}
/*************************************************************************//
/---------------------- apply_trapezoidal_rule( ) --------------------
///*************************************************************************
/void apply_trapezoidal_rule( )
{
int count_1=0;
for(int count_2=1;count_2<=n;count_2+=count_2)
{
for(int count_3=0;count_3<max_size;count_3++)
{
xn[count_3]=0;
fx[count_3]=0;
}
h=((b-a)/count_2);
xn[0]=a;
for(int count_4=0;count_4<count_2;count_4++)
xn[(count_4+1)]=(xn[count_4]+h);
for(int count_5=0;count_5<=count_2;count_5++)
fx[count_5]=evaluate_postfix_expression(xn[count_5]);
for(int count_6=1;count_6<count_2;count_6++)
ri[count_1][0]+=fx[count_6];
ri[count_1][0]*=2;
ri[count_1][0]+=fx[0];
ri[count_1][0]+=fx[count_2];
ri[count_1][0]*=(h/2);
count_1++;
}
}
/*************************************************************************//
/---------------------- apply_romberg_method( ) ----------------------
///*************************************************************************
/void apply_romberg_method( )
{
int counter=0;
switch(n)
{
case 2 : counter=1;
break;
case 4 : counter=2;
break;
case 8 : counter=3;
break;
}
for(int count_1=1;count_1<=counter;count_1++)
{
for(int count_2=count_1;count_2<=counter;count_2++)
{
longdouble rjk_1=0;
longdouble rj_1k_1=0;
rjk_1=ri[count_2][(count_1-1)];
rj_1k_1=ri[(count_2-1)][(count_1-1)];
ri[count_2][count_1]=(((powl(4,count_1)*rjk_1)-rj_1k_1)/(powl(4,count_1)-1));
}
}
}
/*************************************************************************//
/---------------------------- show_result( ) -------------------------
///*************************************************************************
/void show_result( )
{
clear_screen( );
int counter=0;
switch(n)
{
case 2 : counter=1;
break;
case 4 : counter=2;
break;
case 8 : counter=3;
break;
}
gotoxy(6,10);
cout<<"Solution :";
gotoxy(6,11);
cout<<"ÍÍÍÍÍÍÍÍÍÍ";
if(n==1)
{
gotoxy(6,13);
cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
}
elseif(n==2)
{
gotoxy(6,13);
cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
}
elseif(n==4)
{
gotoxy(6,13);
cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
ÄÄÄÄÄÄÄ¿";
}
elseif(n==8)
{
gotoxy(6,13);
cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
}
if(n==1)
{
gotoxy(6,14);
cout<<"³ Number of ³ Trapezoidal ³";
}
elseif(n==2)
{
gotoxy(6,14);
cout<<"³ Number of ³ Trapezoidal ³ Romberg Values ³";
}
elseif(n==4)
{
gotoxy(6,14);
cout<<"³ Number of ³ Trapezoidal ³ Romberg Values ³";
}
elseif(n==8)
{
gotoxy(6,14);
cout<<"³ Number of ³ Trapezoidal ³ Romberg Values ³";
}
if(n==1)
{
gotoxy(6,15);
cout<<"³ ³ ³";
}
elseif(n==2)
{
gotoxy(6,15);
cout<<"³ ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
}
elseif(n==4)
{
gotoxy(6,15);
cout<<"³ ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
}
elseif(n==8)
{
gotoxy(6,15);
cout<<"³ ³
ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
}
if(n==1)
{
gotoxy(6,16);
cout<<"³ Intervals ³ Sums ³";
}
elseif(n==2)
{
gotoxy(6,16);
cout<<"³ Intervals ³ Sums ³ 1ts Order ³";
}
elseif(n==4)
{
gotoxy(6,16);
cout<<"³ Intervals ³ Sums ³ 1ts Order ³ 2nd Order ³";
}
elseif(n==8)
{
gotoxy(6,16);
cout<<"³ Intervals ³ Sums ³ 1ts Order ³ 2nd Order ³ 3rd Order ³";
}
if(n==1)
{
gotoxy(6,17);
cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
}
elseif(n==2)
{
gotoxy(6,17);
cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
}
elseif(n==4)
{
gotoxy(6,17);
cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ
ÄÄÄÄÄÄÄ´";
}
elseif(n==8)
{
gotoxy(6,17);
cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
}
if(n==1)
{
gotoxy(6,18);
cout<<"³ ³ ³";
}
elseif(n==2)
{
gotoxy(6,18);
cout<<"³ ³ ³ ³";
}
elseif(n==4)
{
gotoxy(6,18);
cout<<"³ ³ ³ ³ ³";
}
elseif(n==8)
{
gotoxy(6,18);
cout<<"³ ³ ³ ³ ³ ³";
}
int y_cord=19;
int intervals=1;
for(int count_1=0;count_1<=counter;count_1++)
{
if(n==1)
{
gotoxy(6,y_cord);
cout<<"³ ³ ³";
gotoxy(6,(y_cord+1));
cout<<"³ ³ ³";
}
elseif(n==2)
{
gotoxy(6,y_cord);
cout<<"³ ³ ³ ³";
gotoxy(6,(y_cord+1));
cout<<"³ ³ ³ ³";
}
elseif(n==4)
{
gotoxy(6,y_cord);
cout<<"³ ³ ³ ³ ³";
gotoxy(6,(y_cord+1));
cout<<"³ ³ ³ ³ ³";
}
elseif(n==8)
{
gotoxy(6,y_cord);
cout<<"³ ³ ³ ³ ³ ³";
gotoxy(6,(y_cord+1));
cout<<"³ ³ ³ ³ ³ ³";
}
gotoxy(11,(19+(count_1*2)));
cout<<intervals;
intervals+=intervals;
y_cord+=2;
}
for(int count_2=0;count_2<=counter;count_2++)
{
for(int count_3=0;count_3<=counter;count_3++)
{
if(count_2==0)
{
gotoxy(20,(19+(count_3*2)));
cout<<ri[count_3][0];
}
elseif(count_2==1 && count_3>=count_2)
{
gotoxy(34,(19+(count_3*2)));
cout<<ri[count_3][1];
}
elseif(count_2==2 && count_3>=count_2)
{
gotoxy(48,(19+(count_3*2)));
cout<<ri[count_3][2];
}
elseif(count_2==3 && count_3>=count_2)
{
gotoxy(62,(19+(count_3*2)));
cout<<ri[count_3][3];
}
}
}
if(n==1)
{
gotoxy(6,y_cord);
cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
}
elseif(n==2)
{
gotoxy(6,y_cord);
cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
}
elseif(n==4)
{
gotoxy(6,y_cord);
cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÙ";
}
elseif(n==8)
{
gotoxy(6,y_cord);
cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
}
gotoxy(6,32);
cout<<" bô";
gotoxy(6,34);
cout<<" aõ";
gotoxy(6,33);
cout<<"Estimation of ³f(x)dx :";
gotoxy(6,35);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,37);
cout<<"Estimated Integral Value = ";
cout<<ri[counter][counter];
gotoxy(1,2);
}
3. Program untuk memperkirakan nilai Integral dari fungsi di poin yang diberikan dari
data yang diberikan menggunakan 1/3 Aturan Simpson di C ++ Programming
# include <iostream.h>
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <conio.h>
# include <math.h>
constint max_size=13;
int n=0;
int top=-1;
int choice=0;
longdouble h=0;
longdouble a=0;
longdouble b=0;
longdouble estimated_value=0;
longdouble xn[max_size]={0};
longdouble fx[max_size]={0};
char Fx[100]={NULL};
char Stack[30][30]={NULL};
char Postfix_expression[30][30]={NULL};
/*************************************************************************//
*************************************************************************///
------------------------ Funcion Prototypes -------------------------
///*************************************************************************
//*************************************************************************/
void push(constchar *);
void convert_ie_to_pe(constchar *);
constchar* pop( );
constlongdouble evaluate_postfix_expression(constlongdouble);
void show_screen( );
void clear_screen( );
void get_input( );
void apply_simpsons_rule( );
void show_result( );
/*************************************************************************//
*************************************************************************///
------------------------------ main( ) ------------------------------
///*************************************************************************
//*************************************************************************/
int main( )
{
clrscr( );
textmode(C4350);
show_screen( );
get_input( );
apply_simpsons_rule( );
show_result( );
getch( );
return 0;
}
/*************************************************************************//
*************************************************************************///
------------------------ Funcion Definitions ------------------------
///*************************************************************************
//*************************************************************************/
/*************************************************************************//
/-------------------------- show_screen( ) ---------------------------
///*************************************************************************
/void show_screen( )
{
cprintf("n******************************************************************
**************");
cprintf("***************************- -
**************************");
cprintf("*--------------------------- ");
textbackground(1);
cprintf(" Numerical Integration ");
textbackground(8);
cprintf(" --------------------------*");
cprintf("*-*************************- -
************************-*");
cprintf("*-
***************************************************************************
*-*");
for(int count=0;count<42;count++)
cprintf("*-* *-*");
gotoxy(1,46);
cprintf("*-
***************************************************************************
*-*");
cprintf("*------------------------------------------------------------------------------*");
cprintf("********************************************************************
************");
gotoxy(1,2);
}
/*************************************************************************//
/------------------------- clear_screen( ) ---------------------------
///*************************************************************************
/void clear_screen( )
{
for(int count=0;count<37;count++)
{
gotoxy(5,8+count);
cout<<" ";
}
gotoxy(1,2);
}
/*************************************************************************//
/-------------------------- push(const char*) ------------------------
///*************************************************************************
/void push(constchar* Operand)
{
if(top==(max_size-1))
{
cout<<"Error : Stack is full."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
top++;
strcpy(Stack[top],Operand);
}
}
/*************************************************************************//
/------------------------------ pop( ) -------------------------------
///*************************************************************************
/constchar* pop( )
{
char Operand[40]={NULL};
if(top==-1)
{
cout<<"Error : Stack is empty."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
strcpy(Operand,Stack[top]);
strset(Stack[top],NULL);
top--;
}
return Operand;
}
/*************************************************************************//
/-------------------- convert_ie_to_pe(const char*) ------------------
///*************************************************************************
/void convert_ie_to_pe(constchar* Expression)
{
char Infix_expression[100]={NULL};
char Symbol_scanned[30]={NULL};
push("(");
strcpy(Infix_expression,Expression);
strcat(Infix_expression,"+0)");
int flag=0;
int count_1=0;
int count_2=0;
int equation_length=strlen(Infix_expression);
if(Infix_expression[0]=='(')
flag=1;
do
{
strset(Symbol_scanned,NULL);
if(flag==0)
{
int count_3=0;
do
{
Symbol_scanned[count_3]=Infix_expression[count_1];
count_1++;
count_3++;
}
while(count_1<=equation_length &&
Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!=')');
flag=1;
}
elseif(flag==1)
{
Symbol_scanned[0]=Infix_expression[count_1];
count_1++;
if(Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!=')')
flag=0;
if(Infix_expression[count_1-1]=='(' &&
(Infix_expression[count_1]=='-' ||
Infix_expression[count_1]=='+'))
flag=0;
}
if(strcmp(Symbol_scanned,"(")==0)
push("(");
elseif(strcmp(Symbol_scanned,")")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
pop( );
}
elseif(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
if(strcmp(Symbol_scanned,"^")==0)
{ }
elseif(strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
while(strcmp(Stack[top],"^")==0 ||
strcmp(Stack[top],"*")==0 ||
strcmp(Stack[top],"/")==0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
}
elseif(strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
}
push(Symbol_scanned);
}
else
{
strcat(Postfix_expression[count_2],Symbol_scanned);
count_2++;
}
}
while(strcmp(Stack[top],NULL)!=0);
strcat(Postfix_expression[count_2],"=");
count_2++;
}
/*************************************************************************//
/---------- evaluate_postfix_expression(const long double) -----------
///*************************************************************************
/constlongdouble evaluate_postfix_expression(constlongdouble x)
{
longdouble function_value=0;
int count_1=-1;
char Symbol_scanned[30]={NULL};
do
{
count_1++;
strcpy(Symbol_scanned,Postfix_expression[count_1]);
if(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
char Result[30]={NULL};
char Operand[2][30]={NULL};
strcpy(Operand[0],pop( ));
strcpy(Operand[1],pop( ));
longdouble operand[2]={0};
longdouble result=0;
char *endptr;
for(int count_2=0;count_2<2;count_2++)
{
int flag=0;
if(Operand[count_2][0]=='-')
{
int length=strlen(Operand[count_2]);
for(int count_3=0;count_3<(length-1);count_3++)
Operand[count_2][count_3]=Operand[count_2][(count_3+1)];
Operand[count_2][count_3]=NULL;
flag=1;
}
if(strcmp(Operand[count_2],"x")==0)
operand[count_2]=x;
elseif(strcmp(Operand[count_2],"e")==0)
operand[count_2]=2.718282;
elseif(strcmp(Operand[count_2],"sinx")==0)
operand[count_2]=sinl(x);
elseif(strcmp(Operand[count_2],"cosx")==0)
operand[count_2]=cosl(x);
elseif(strcmp(Operand[count_2],"tanx")==0)
operand[count_2]=tanl(x);
elseif(strcmp(Operand[count_2],"lnx")==0)
operand[count_2]=logl(x);
elseif(strcmp(Operand[count_2],"logx")==0)
operand[count_2]=log10l(x);
else
operand[count_2]=strtod(Operand[count_2],&endptr);
if(flag)
operand[count_2]*=-1;
}
switch(Symbol_scanned[0])
{
case'^' : result=powl(operand[1],operand[0]);
break;
case'*' : result=operand[1]*operand[0];
break;
case'/' : result=operand[1]/operand[0];
break;
case'+' : result=operand[1]+operand[0];
break;
case'-' : result=operand[1]-operand[0];
break;
}
gcvt(result,25,Result);
push(Result);
}
elseif(strcmp(Symbol_scanned,"=")!=0)
push(Symbol_scanned);
}
while(strcmp(Symbol_scanned,"=")!=0);
char Function_value[30]={NULL};
char *endptr;
strcpy(Function_value,pop( ));
function_value=strtod(Function_value,&endptr);
return function_value;
}
/*************************************************************************//
/----------------------------- get_input( ) --------------------------
///*************************************************************************
/void get_input( )
{
do
{
clear_screen( );
gotoxy(6,9);
cout<<"Number of Sub-Intervals :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(20,13);
cout<<"[ min. n = 2 | max. n = 12 ]";
gotoxy(6,12);
cout<<"Enter the max. number of sub-intervals = n = ";
cin>>n;
if(n<2 || n>12)
{
gotoxy(12,25);
cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";
gotoxy(12,26);
cout<<" to try again.";
n=int(getche( ));
if(n==27)
exit(0);
}
}
while(n<2 || n>12);
gotoxy(6,16);
cout<<"Enter the value of Lower limit = a = ";
cin>>a;
gotoxy(6,18);
cout<<"Enter the value of Upper Limit = b = ";
cin>>b;
h=((b-a)/n);
gotoxy(6,24);
cout<<"Input Mode :";
gotoxy(6,25);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,28);
cout<<"Press : ";
gotoxy(10,30);
cout<<"- 'Y' or <Enter> to enter function";
gotoxy(10,32);
cout<<"- 'N' or <Any other key> to enter values of the function";
gotoxy(8,35);
cout<<"Enter your choice : ";
char Choice=NULL;
Choice=getch( );
if(Choice=='y' || Choice=='Y' || int(Choice)==13)
{
choice=1;
gotoxy(28,35);
cout<<"Y";
}
else
{
gotoxy(28,35);
cout<<"N";
}
gotoxy(25,43);
cout<<"Press any key to continue...";
getch( );
if(choice)
{
clear_screen( );
gotoxy(6,10);
cout<<"Non-Linear Function :";
gotoxy(6,11);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(6,37);
cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";
gotoxy(6,40);
cout<<"Available Operators : ^ (raised to power) , * , / , + , -";
gotoxy(6,42);
cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";
gotoxy(6,44);
cout<<" n = any number";
gotoxy(6,14);
cout<<"Enter the Function : f(x) = ";
cin>>Fx;
convert_ie_to_pe(Fx);
}
clear_screen( );
gotoxy(6,9);
cout<<"Data Points & Values of Function :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(25,12);
cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(25,13);
cout<<"³ x ³ f(x) ³";
gotoxy(25,14);
cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(25,15);
cout<<"³ ³ ³";
for(int count_1=0;count_1<=n;count_1++)
{
gotoxy(25,(wherey( )+1));
cout<<"³ ³ ³";
gotoxy(25,(wherey( )+1));
cout<<"³ ³ ³";
}
gotoxy(25,(wherey( )+1));
cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
xn[0]=a;
for(int count_2=0;count_2<n;count_2++)
xn[(count_2+1)]=(xn[count_2]+h);
gotoxy(25,16);
for(int count_3=0;count_3<=n;count_3++)
{
gotoxy(27,wherey( ));
cout<<xn[count_3];
if(choice)
{
fx[count_3]=evaluate_postfix_expression(xn[count_3]);
gotoxy(43,wherey( ));
cout<<fx[count_3];
}
else
{
gotoxy(43,wherey( ));
cin>>fx[count_3];
}
if(choice)
gotoxy(25,(wherey( )+2));
else
gotoxy(25,(wherey( )+1));
}
gotoxy(25,43);
cout<<"Press any key to continue...";
getch( );
}
/*************************************************************************//
/---------------------- apply_simpsons_rule( ) -----------------------
///*************************************************************************
/void apply_simpsons_rule( )
{
longdouble temp=0;
estimated_value=(fx[0]+fx[n]);
estimated_value*=h;
estimated_value/=3;
temp=0;
for(int count_1=2;count_1<=(n-2);count_1+=2)
temp+=fx[count_1];
temp*=(2*h);
temp/=3;
estimated_value+=temp;
temp=0;
for(int count_2=1;count_2<=(n-1);count_2+=2)
temp+=fx[count_2];
temp*=(4*h);
temp/=3;
estimated_value+=temp;
}
/*************************************************************************//
/----------------------------- show_result( ) ------------------------
///*************************************************************************
/void show_result( )
{
clear_screen( );
gotoxy(6,9);
cout<<"Simpson's 1/3 Rule :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,12);
cout<<" bô";
gotoxy(10,13);
cout<<"³f(x)dx ÷ (h/3)(f0+fn) + (2h/3)[f2+f4+...+f(n-2)] +";
gotoxy(50,15);
cout<<"(4h/3)[f1+f3+...+f(n-1)]";
gotoxy(8,14);
cout<<" aõ";
gotoxy(6,17);
cout<<" bô";
gotoxy(6,19);
cout<<" aõ";
gotoxy(6,18);
cout<<"Estimation of ³f(x)dx :";
gotoxy(6,20);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,23);
cout<<"Estimated Integral Value = ";
cout<<estimated_value;
gotoxy(1,2);
}
4. Program untuk memperkirakan nilai Integral dari fungsi di poin yang diberikan dari
data yang diberikan menggunakan aturan trapesium di C ++ Programming
# include <iostream.h>
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <conio.h>
# include <math.h>
constint max_size=13;
int n=0;
int top=-1;
int choice=0;
longdouble h=0;
longdouble a=0;
longdouble b=0;
longdouble estimated_value=0;
longdouble xn[max_size]={0};
longdouble fx[max_size]={0};
char Fx[100]={NULL};
char Stack[30][30]={NULL};
char Postfix_expression[30][30]={NULL};
/*************************************************************************//
*************************************************************************///
------------------------ Funcion Prototypes -------------------------
///*************************************************************************
//*************************************************************************/
void push(constchar *);
void convert_ie_to_pe(constchar *);
constchar* pop( );
constlongdouble evaluate_postfix_expression(constlongdouble);
void show_screen( );
void clear_screen( );
void get_input( );
void apply_trapezoidal_rule( );
void show_result( );
/*************************************************************************//
*************************************************************************///
------------------------------ main( ) ------------------------------
///*************************************************************************
//*************************************************************************/
int main( )
{
clrscr( );
textmode(C4350);
show_screen( );
get_input( );
apply_trapezoidal_rule( );
show_result( );
getch( );
return 0;
}
/*************************************************************************//
*************************************************************************///
------------------------ Funcion Definitions ------------------------
///*************************************************************************
//*************************************************************************/
/*************************************************************************//
/-------------------------- show_screen( ) ---------------------------
///*************************************************************************
/void show_screen( )
{
cprintf("n******************************************************************
**************");
cprintf("***************************- -
**************************");
cprintf("*--------------------------- ");
textbackground(1);
cprintf(" Numerical Integration ");
textbackground(8);
cprintf(" --------------------------*");
cprintf("*-*************************- -
************************-*");
cprintf("*-
***************************************************************************
*-*");
for(int count=0;count<42;count++)
cprintf("*-* *-*");
gotoxy(1,46);
cprintf("*-
***************************************************************************
*-*");
cprintf("*------------------------------------------------------------------------------*");
cprintf("********************************************************************
************");
gotoxy(1,2);
}
/*************************************************************************//
/------------------------- clear_screen( ) ---------------------------
///*************************************************************************
/void clear_screen( )
{
for(int count=0;count<37;count++)
{
gotoxy(5,8+count);
cout<<" ";
}
gotoxy(1,2);
}
/*************************************************************************//
/-------------------------- push(const char*) ------------------------
///*************************************************************************
/void push(constchar* Operand)
{
if(top==(max_size-1))
{
cout<<"Error : Stack is full."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
top++;
strcpy(Stack[top],Operand);
}
}
/*************************************************************************//
/------------------------------ pop( ) -------------------------------
///*************************************************************************
/constchar* pop( )
{
char Operand[40]={NULL};
if(top==-1)
{
cout<<"Error : Stack is empty."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
strcpy(Operand,Stack[top]);
strset(Stack[top],NULL);
top--;
}
return Operand;
}
/*************************************************************************//
/-------------------- convert_ie_to_pe(const char*) ------------------
///*************************************************************************
/void convert_ie_to_pe(constchar* Expression)
{
char Infix_expression[100]={NULL};
char Symbol_scanned[30]={NULL};
push("(");
strcpy(Infix_expression,Expression);
strcat(Infix_expression,"+0)");
int flag=0;
int count_1=0;
int count_2=0;
int equation_length=strlen(Infix_expression);
if(Infix_expression[0]=='(')
flag=1;
do
{
strset(Symbol_scanned,NULL);
if(flag==0)
{
int count_3=0;
do
{
Symbol_scanned[count_3]=Infix_expression[count_1];
count_1++;
count_3++;
}
while(count_1<=equation_length &&
Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!=')');
flag=1;
}
elseif(flag==1)
{
Symbol_scanned[0]=Infix_expression[count_1];
count_1++;
if(Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!=')')
flag=0;
if(Infix_expression[count_1-1]=='(' &&
(Infix_expression[count_1]=='-' ||
Infix_expression[count_1]=='+'))
flag=0;
}
if(strcmp(Symbol_scanned,"(")==0)
push("(");
elseif(strcmp(Symbol_scanned,")")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
pop( );
}
elseif(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
if(strcmp(Symbol_scanned,"^")==0)
{ }
elseif(strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
while(strcmp(Stack[top],"^")==0 ||
strcmp(Stack[top],"*")==0 ||
strcmp(Stack[top],"/")==0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
}
elseif(strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[count_2],pop( ));
count_2++;
}
}
push(Symbol_scanned);
}
else
{
strcat(Postfix_expression[count_2],Symbol_scanned);
count_2++;
}
}
while(strcmp(Stack[top],NULL)!=0);
strcat(Postfix_expression[count_2],"=");
count_2++;
}
/*************************************************************************//
/---------- evaluate_postfix_expression(const long double) -----------
///*************************************************************************
/constlongdouble evaluate_postfix_expression(constlongdouble x)
{
longdouble function_value=0;
int count_1=-1;
char Symbol_scanned[30]={NULL};
do
{
count_1++;
strcpy(Symbol_scanned,Postfix_expression[count_1]);
if(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
char Result[30]={NULL};
char Operand[2][30]={NULL};
strcpy(Operand[0],pop( ));
strcpy(Operand[1],pop( ));
longdouble operand[2]={0};
longdouble result=0;
char *endptr;
for(int count_2=0;count_2<2;count_2++)
{
int flag=0;
if(Operand[count_2][0]=='-')
{
int length=strlen(Operand[count_2]);
for(int count_3=0;count_3<(length-1);count_3++)
Operand[count_2][count_3]=Operand[count_2][(count_3+1)];
Operand[count_2][count_3]=NULL;
flag=1;
}
if(strcmp(Operand[count_2],"x")==0)
operand[count_2]=x;
elseif(strcmp(Operand[count_2],"e")==0)
operand[count_2]=2.718282;
elseif(strcmp(Operand[count_2],"sinx")==0)
operand[count_2]=sinl(x);
elseif(strcmp(Operand[count_2],"cosx")==0)
operand[count_2]=cosl(x);
elseif(strcmp(Operand[count_2],"tanx")==0)
operand[count_2]=tanl(x);
elseif(strcmp(Operand[count_2],"lnx")==0)
operand[count_2]=logl(x);
elseif(strcmp(Operand[count_2],"logx")==0)
operand[count_2]=log10l(x);
else
operand[count_2]=strtod(Operand[count_2],&endptr);
if(flag)
operand[count_2]*=-1;
}
switch(Symbol_scanned[0])
{
case'^' : result=powl(operand[1],operand[0]);
break;
case'*' : result=operand[1]*operand[0];
break;
case'/' : result=operand[1]/operand[0];
break;
case'+' : result=operand[1]+operand[0];
break;
case'-' : result=operand[1]-operand[0];
break;
}
gcvt(result,25,Result);
push(Result);
}
elseif(strcmp(Symbol_scanned,"=")!=0)
push(Symbol_scanned);
}
while(strcmp(Symbol_scanned,"=")!=0);
char Function_value[30]={NULL};
char *endptr;
strcpy(Function_value,pop( ));
function_value=strtod(Function_value,&endptr);
return function_value;
}
/*************************************************************************//
/----------------------------- get_input( ) --------------------------
///*************************************************************************
/void get_input( )
{
do
{
clear_screen( );
gotoxy(6,9);
cout<<"Number of Sub-Intervals :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(20,13);
cout<<"[ min. n = 3 | max. n = 12 ]";
gotoxy(6,12);
cout<<"Enter the max. number of sub-intervals = n = ";
cin>>n;
if(n<3 || n>12)
{
gotoxy(12,25);
cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";
gotoxy(12,26);
cout<<" to try again.";
n=int(getche( ));
if(n==27)
exit(0);
}
}
while(n<3 || n>12);
gotoxy(6,16);
cout<<"Enter the value of Lower limit = a = ";
cin>>a;
gotoxy(6,18);
cout<<"Enter the value of Upper Limit = b = ";
cin>>b;
h=((b-a)/n);
gotoxy(6,24);
cout<<"Input Mode :";
gotoxy(6,25);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,28);
cout<<"Press : ";
gotoxy(10,30);
cout<<"- 'Y' or <Enter> to enter function";
gotoxy(10,32);
cout<<"- 'N' or <Any other key> to enter values of the function";
gotoxy(8,35);
cout<<"Enter your choice : ";
char Choice=NULL;
Choice=getch( );
if(Choice=='y' || Choice=='Y' || int(Choice)==13)
{
choice=1;
gotoxy(28,35);
cout<<"Y";
}
else
{
gotoxy(28,35);
cout<<"N";
}
gotoxy(25,43);
cout<<"Press any key to continue...";
getch( );
if(choice)
{
clear_screen( );
gotoxy(6,10);
cout<<"Non-Linear Function :";
gotoxy(6,11);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(6,37);
cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";
gotoxy(6,40);
cout<<"Available Operators : ^ (raised to power) , * , / , + , -";
gotoxy(6,42);
cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";
gotoxy(6,44);
cout<<" n = any number";
gotoxy(6,14);
cout<<"Enter the Function : f(x) = ";
cin>>Fx;
convert_ie_to_pe(Fx);
}
clear_screen( );
gotoxy(6,9);
cout<<"Data Points & Values of Function :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(25,12);
cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(25,13);
cout<<"³ x ³ f(x) ³";
gotoxy(25,14);
cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(25,15);
cout<<"³ ³ ³";
for(int count_1=0;count_1<=n;count_1++)
{
gotoxy(25,(wherey( )+1));
cout<<"³ ³ ³";
gotoxy(25,(wherey( )+1));
cout<<"³ ³ ³";
}
gotoxy(25,(wherey( )+1));
cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
xn[0]=a;
for(int count_2=0;count_2<n;count_2++)
xn[(count_2+1)]=(xn[count_2]+h);
gotoxy(25,16);
for(int count_3=0;count_3<=n;count_3++)
{
gotoxy(27,wherey( ));
cout<<xn[count_3];
if(choice)
{
fx[count_3]=evaluate_postfix_expression(xn[count_3]);
gotoxy(43,wherey( ));
cout<<fx[count_3];
}
else
{
gotoxy(43,wherey( ));
cin>>fx[count_3];
}
if(choice)
gotoxy(25,(wherey( )+2));
else
gotoxy(25,(wherey( )+1));
}
gotoxy(25,43);
cout<<"Press any key to continue...";
getch( );
}
/*************************************************************************//
/---------------------- apply_trapezoidal_rule( ) --------------------
///*************************************************************************
/void apply_trapezoidal_rule( )
{
for(int count=1;count<n;count++)
estimated_value+=fx[count];
estimated_value*=2;
estimated_value+=fx[0];
estimated_value+=fx[n];
estimated_value*=(h/2);
}
/*************************************************************************//
/--------------------------- show_result( ) --------------------------
///*************************************************************************
/void show_result( )
{
clear_screen( );
gotoxy(6,9);
cout<<"Trapeziodal Rule :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,12);
cout<<" bô";
gotoxy(10,13);
cout<<"³f(x)dx ÷ (h/2)[f0+{2*(f1+f2+...+fn-1)}+fn]";
gotoxy(8,14);
cout<<" aõ";
gotoxy(6,17);
cout<<" bô";
gotoxy(6,19);
cout<<" aõ";
gotoxy(6,18);
cout<<"Estimation of ³f(x)dx :";
gotoxy(6,20);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,23);
cout<<"Estimated Integral Value = ";
cout<<estimated_value;
gotoxy(1,2);
}
5. Program untuk memperkirakan nilai derivatif pertama dari fungsi pada titik-titik
tertentu dari data yang diberikan menggunakan Tengah Perbedaan Formula di C ++
Programming
# include <iostream.h>
# include <stdlib.h>
# include <string.h>
# include <stdio.h>
# include <conio.h>
# include <math.h>
constint max_size=13;
int n=0;
int top=-1;
int choice=0;
longdouble h=0;
longdouble x0=0;
longdouble xn[max_size]={0};
longdouble fx[max_size]={0};
char Fx[100]={NULL};
char Dfx[100]={NULL};
char Stack[30][30]={NULL};
char Postfix_expression[2][30][30]={NULL};
void push(constchar *);
void convert_ie_to_pe(constchar *,constint);
constchar* pop( );
constlongdouble evaluate_postfix_expression(constlongdouble,constint);
void show_screen( );
void clear_screen( );
void get_input( );
void estimate_dfx( );
constint get_index(constlongdouble);
int main( )
{
clrscr( );
textmode(C4350);
show_screen( );
get_input( );
estimate_dfx( );
return 0;
}
/*************************************************************************//
/-------------------------- show_screen( ) ---------------------------
///*************************************************************************
/void show_screen( )
{
cprintf("n******************************************************************
**************");
cprintf("*************************- -
************************");
cprintf("*------------------------- ");
textbackground(1);
cprintf(" Numerical Differentiation ");
textbackground(8);
cprintf(" ------------------------*");
cprintf("*-***********************- -**********************-
*");
cprintf("*-
***************************************************************************
*-*");
for(int count=0;count<42;count++)
cprintf("*-* *-*");
gotoxy(1,46);
cprintf("*-
***************************************************************************
*-*");
cprintf("*------------------------------------------------------------------------------*");
cprintf("********************************************************************
************");
gotoxy(1,2);
}
/*************************************************************************//
/------------------------- clear_screen( ) ---------------------------
///*************************************************************************
/void clear_screen( )
{
for(int count=0;count<37;count++)
{
gotoxy(5,8+count);
cout<<" ";
}
gotoxy(1,2);
}
/*************************************************************************//
/-------------------------- push(const char*) ------------------------
///*************************************************************************
/void push(constchar* Operand)
{
if(top==(max_size-1))
{
cout<<"Error : Stack is full."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
top++;
strcpy(Stack[top],Operand);
}
}
/*************************************************************************//
/------------------------------ pop( ) -------------------------------
///*************************************************************************
/constchar* pop( )
{
char Operand[40]={NULL};
if(top==-1)
{
cout<<"Error : Stack is empty."<<endl;
cout<<"n Press any key to exit.";
getch( );
exit(0);
}
else
{
strcpy(Operand,Stack[top]);
strset(Stack[top],NULL);
top--;
}
return Operand;
}
/*************************************************************************//
/---------------- convert_ie_to_pe(const char*,const int) ------------
///*************************************************************************
/void convert_ie_to_pe(constchar* Expression,constint index)
{
char Infix_expression[100]={NULL};
char Symbol_scanned[30]={NULL};
push("(");
strcpy(Infix_expression,Expression);
strcat(Infix_expression,"+0)");
int flag=0;
int count_1=0;
int count_2=0;
int equation_length=strlen(Infix_expression);
if(Infix_expression[0]=='(')
flag=1;
do
{
strset(Symbol_scanned,NULL);
if(flag==0)
{
int count_3=0;
do
{
Symbol_scanned[count_3]=Infix_expression[count_1];
count_1++;
count_3++;
}
while(count_1<=equation_length &&
Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!=')');
flag=1;
}
elseif(flag==1)
{
Symbol_scanned[0]=Infix_expression[count_1];
count_1++;
if(Infix_expression[count_1]!='(' &&
Infix_expression[count_1]!='^' &&
Infix_expression[count_1]!='*' &&
Infix_expression[count_1]!='/' &&
Infix_expression[count_1]!='+' &&
Infix_expression[count_1]!='-' &&
Infix_expression[count_1]!=')')
flag=0;
if(Infix_expression[count_1-1]=='(' &&
(Infix_expression[count_1]=='-' ||
Infix_expression[count_1]=='+'))
flag=0;
}
if(strcmp(Symbol_scanned,"(")==0)
push("(");
elseif(strcmp(Symbol_scanned,")")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[index][count_2],pop( ));
count_2++;
}
pop( );
}
elseif(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
if(strcmp(Symbol_scanned,"^")==0)
{ }
elseif(strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0)
{
while(strcmp(Stack[top],"^")==0 ||
strcmp(Stack[top],"*")==0 ||
strcmp(Stack[top],"/")==0)
{
strcpy(Postfix_expression[index][count_2],pop( ));
count_2++;
}
}
elseif(strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
while(strcmp(Stack[top],"(")!=0)
{
strcpy(Postfix_expression[index][count_2],pop( ));
count_2++;
}
}
push(Symbol_scanned);
}
else
{
strcat(Postfix_expression[index][count_2],Symbol_scanned);
count_2++;
}
}
while(strcmp(Stack[top],NULL)!=0);
strcat(Postfix_expression[index][count_2],"=");
count_2++;
}
/*************************************************************************//
/----- evaluate_postfix_expression(const long double,const int) ------
///*************************************************************************
/constlongdouble evaluate_postfix_expression(
constlongdouble x,constint index)
{
longdouble function_value=0;
int count_1=-1;
char Symbol_scanned[30]={NULL};
do
{
count_1++;
strcpy(Symbol_scanned,Postfix_expression[index][count_1]);
if(strcmp(Symbol_scanned,"^")==0 ||
strcmp(Symbol_scanned,"*")==0 ||
strcmp(Symbol_scanned,"/")==0 ||
strcmp(Symbol_scanned,"+")==0 ||
strcmp(Symbol_scanned,"-")==0)
{
char Result[30]={NULL};
char Operand[2][30]={NULL};
strcpy(Operand[0],pop( ));
strcpy(Operand[1],pop( ));
longdouble operand[2]={0};
longdouble result=0;
char *endptr;
for(int count_2=0;count_2<2;count_2++)
{
int flag=0;
if(Operand[count_2][0]=='-')
{
int length=strlen(Operand[count_2]);
for(int count_3=0;count_3<(length-1);count_3++)
Operand[count_2][count_3]=Operand[count_2][(count_3+1)];
Operand[count_2][count_3]=NULL;
flag=1;
}
if(strcmp(Operand[count_2],"x")==0)
operand[count_2]=x;
elseif(strcmp(Operand[count_2],"e")==0)
operand[count_2]=2.718282;
elseif(strcmp(Operand[count_2],"sinx")==0)
operand[count_2]=sinl(x);
elseif(strcmp(Operand[count_2],"cosx")==0)
operand[count_2]=cosl(x);
elseif(strcmp(Operand[count_2],"tanx")==0)
operand[count_2]=tanl(x);
elseif(strcmp(Operand[count_2],"lnx")==0)
operand[count_2]=logl(x);
elseif(strcmp(Operand[count_2],"logx")==0)
operand[count_2]=log10l(x);
else
operand[count_2]=strtod(Operand[count_2],&endptr);
if(flag)
operand[count_2]*=-1;
}
switch(Symbol_scanned[0])
{
case'^' : result=powl(operand[1],operand[0]);
break;
case'*' : result=operand[1]*operand[0];
break;
case'/' : result=operand[1]/operand[0];
break;
case'+' : result=operand[1]+operand[0];
break;
case'-' : result=operand[1]-operand[0];
break;
}
gcvt(result,25,Result);
push(Result);
}
elseif(strcmp(Symbol_scanned,"=")!=0)
push(Symbol_scanned);
}
while(strcmp(Symbol_scanned,"=")!=0);
char Function_value[30]={NULL};
char *endptr;
strcpy(Function_value,pop( ));
function_value=strtod(Function_value,&endptr);
return function_value;
}
/*************************************************************************//
/----------------------------- get_input( ) --------------------------
///*************************************************************************
/void get_input( )
{
do
{
clear_screen( );
gotoxy(6,9);
cout<<"Number of Distinct Data Points :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(27,13);
cout<<"[ min. n = 3 | max. n = 12 ]";
gotoxy(6,12);
cout<<"Enter the max. number of distinct data points = n = ";
cin>>n;
if(n<3 || n>12)
{
gotoxy(12,25);
cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";
gotoxy(12,26);
cout<<" to try again.";
n=int(getche( ));
if(n==27)
exit(0);
}
}
while(n<3 || n>12);
gotoxy(6,16);
cout<<"Enter the value of x0 = ";
cin>>x0;
gotoxy(6,18);
cout<<"Enter the value of h = ";
cin>>h;
gotoxy(6,24);
cout<<"Input Mode :";
gotoxy(6,25);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,28);
cout<<"Press : ";
gotoxy(10,30);
cout<<"- 'Y' or <Enter> to enter function";
gotoxy(10,32);
cout<<"- 'N' or <Any other key> to enter values of the function";
gotoxy(8,35);
cout<<"Enter your choice : ";
char Choice=NULL;
Choice=getch( );
if(Choice=='y' || Choice=='Y' || int(Choice)==13)
{
choice=1;
gotoxy(28,35);
cout<<"Y";
}
else
{
gotoxy(28,35);
cout<<"N";
}
gotoxy(25,43);
cout<<"Press any key to continue...";
getch( );
if(choice)
{
clear_screen( );
gotoxy(6,11);
cout<<"Non-Linear Function :";
gotoxy(6,12);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(6,37);
cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";
gotoxy(6,40);
cout<<"Available Operators : ^ (raised to power) , * , / , + , -";
gotoxy(6,42);
cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,";
gotoxy(6,44);
cout<<" n = any number";
gotoxy(6,14);
cout<<"Enter the Function : f(x) = ";
cin>>Fx;
gotoxy(6,17);
cout<<"Enter the Differential Function : f'(x) = ";
cin>>Dfx;
convert_ie_to_pe(Fx,0);
convert_ie_to_pe(Dfx,1);
}
clear_screen( );
gotoxy(6,9);
cout<<"Data Points & Values of Function :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(25,12);
cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(25,13);
cout<<"³ x ³ f(x) ³";
gotoxy(25,14);
cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(25,15);
cout<<"³ ³ ³";
for(int count_1=0;count_1<n;count_1++)
{
gotoxy(25,(wherey( )+1));
cout<<"³ ³ ³";
gotoxy(25,(wherey( )+1));
cout<<"³ ³ ³";
}
gotoxy(25,(wherey( )+1));
cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
xn[0]=x0;
for(int count_2=0;count_2<(n-1);count_2++)
xn[(count_2+1)]=(xn[count_2]+h);
gotoxy(25,16);
for(int count_3=0;count_3<n;count_3++)
{
gotoxy(27,wherey( ));
cout<<xn[count_3];
if(choice)
{
fx[count_3]=evaluate_postfix_expression(xn[count_3],0);
gotoxy(43,wherey( ));
cout<<fx[count_3];
}
else
{
gotoxy(43,wherey( ));
cin>>fx[count_3];
}
if(choice)
gotoxy(25,(wherey( )+2));
else
gotoxy(25,(wherey( )+1));
}
gotoxy(25,43);
cout<<"Press any key to continue...";
getch( );
}
/*************************************************************************//
/------------------- get_index(const long double) --------------------
///*************************************************************************
/constint get_index(constlongdouble x)
{
for(int count=0;count<n;count++)
{
if(xn[count]==x)
break;
}
return count;
}
/*************************************************************************//
/---------------------------- estimate_dfx( ) ------------------------
///*************************************************************************
/void estimate_dfx( )
{
clear_screen( );
gotoxy(6,9);
cout<<"Centeral Difference Formula of Order 2 :";
gotoxy(6,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
gotoxy(8,13);
cout<<"f'(x) ÷ [f(x+h)-f(x-h)]/2h";
gotoxy(6,17);
cout<<"Estimation of f'(x) :";
gotoxy(6,18);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
char Choice=NULL;
longdouble x=0;
longdouble dfx=0;
longdouble actual_dfx=0;
int error_flag=0;
do
{
Choice=NULL;
x=0;
dfx=0;
actual_dfx=0;
error_flag=0;
gotoxy(10,20);
cout<<"Enter the value of x = ";
cin>>x;
if(x<=xn[0] || x>=xn[(n-1)])
{
error_flag=1;
gotoxy(10,23);
cout<<"Error: Please enter x greater than x(0) and less than x(n).";
}
else
{
int index=0;
index=get_index(x);
longdouble fxph=fx[(index+1)];
longdouble fxmh=fx[(index-1)];
dfx=((fxph-fxmh)/(2*h));
gotoxy(10,23);
cout<<"The estimated value of f'("<<x<<") ÷ "<<dfx;
}
if(choice && !error_flag)
{
actual_dfx=evaluate_postfix_expression(x,1);
gotoxy(10,25);
cout<<"The Actual value of f'("<<x<<") = "<<actual_dfx;
gotoxy(10,28);
cout<<"Absolute Error = E(abs) = "<<fabs((actual_dfx-dfx));
}
gotoxy(15,42);
cout<<"Press <Esc> to exit or any other key to continue...";
Choice=getch( );
if(int(Choice)!=27)
{
gotoxy(10,20);
cout<<" ";
gotoxy(10,23);
cout<<" ";
gotoxy(10,25);
cout<<" ";
gotoxy(10,28);
cout<<" ";
gotoxy(15,42);
cout<<" ";
}
elseif(int(Choice)==27)
exit(0);
}
while(1);
}
6. Program untuk membaca Sistem Linear dari Persamaan, kemudian mengevaluasi
dengan menggunakan Gauss-Seidel Metode Iteratif dan menampilkan hasil di C ++
Programming
# include <iostream.h>
# include <stdlib.h>
# include <stdio.h>
# include <conio.h>
# include <math.h>
constint max_size=5;
int n=0;
int iterations=0;
char Diagonally_dominant=NULL;
longdouble input[max_size][max_size]={0};
longdouble previous_output[max_size]={0};
longdouble output[max_size]={0};
void show_screen( );
void clear_screen( );
void get_size_of_linear_equations( );
void show_input(constint,constint);
void get_input_linear_equation( );
void sort_system_of_linear_equations( );
void apply_guass_seidel_iterative_method( );
void show_result( );
int main( )
{
clrscr( );
textmode(C4350);
show_screen( );
get_size_of_linear_equations( );
get_input_linear_equation( );
if(Diagonally_dominant=='Y' || Diagonally_dominant=='y')
sort_system_of_linear_equations( );
apply_guass_seidel_iterative_method( );
show_result( );
getch( );
return 0;
}
/*************************************************************************//
/-------------------------- show_screen( ) ---------------------------
///*************************************************************************
/void show_screen( )
{
cprintf("n******************************************************************
**************");
cprintf("**********************- -**********************");
cprintf("*---------------------- ");
textbackground(1);
cprintf(" Guass-Seidel's Itrative Method ");
textbackground(8);
cprintf(" ----------------------*");
cprintf("**********************- -**********************");
cprintf("********************************************************************
************");
for(int count=0;count<42;count++)
cprintf("* *");
gotoxy(1,46);
cprintf("********************************************************************
************");
cprintf("*------------------------------------------------------------------------------*");
cprintf("********************************************************************
************");
gotoxy(1,2);
}
/*************************************************************************//
/------------------------- clear_screen( ) ---------------------------
///*************************************************************************
/void clear_screen( )
{
textbackground(8);
for(int count=0;count<37;count++)
{
gotoxy(3,8+count);
cout<<" ";
}
gotoxy(1,2);
}
/*************************************************************************//
/------------------ get_size_of_linear_equations( ) ------------------
///*************************************************************************
/void get_size_of_linear_equations( )
{
do
{
clear_screen( );
gotoxy(38,11);
cout<<"[ Maximum size = 4 ]";
gotoxy(4,9);
cout<<"Enter the size of the System of Linear Equations = n = ";
cin>>n;
if(n<=0 || n>max_size)
{
gotoxy(12,25);
cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";
gotoxy(12,26);
cout<<" to try again.";
n=int(getche( ));
if(n==27)
exit(0);
}
}
while(n<=0 || n>max_size);
gotoxy(4,15);
cout<<"Enter the number of iterations that you want to perform = ";
cin>>iterations;
gotoxy(4,18);
cout<<"Do you want to make the System Diagonally Dominant : (Y/N) ? : ";
cin>>Diagonally_dominant;
gotoxy(1,2);
}
/*************************************************************************//
/------------------ show_input(const int,const int) ------------------
///*************************************************************************
/void show_input(constint x,constint y)
{
int counter=0;
int number_of_inputs=((x*n)+y+x);
for(int count_1=0;count_1<n;count_1++)
{
gotoxy(4,(17+(count_1*2)));
cout<<" ";
gotoxy(4,(17+(count_1*2)));
for(int count_2=0;count_2<=n;count_2++)
{
if(counter==(number_of_inputs+1))
textbackground(12);
else
textbackground(8);
if(count_2==n)
gotoxy((wherex( )+5),(17+(count_1*2)));
gotoxy(wherex( ),(17+(count_1*2)));
cprintf(" ");
if(count_2==n)
gotoxy((wherex( )-5),(17+(count_1*2)));
gotoxy((wherex( )-6),(17+(count_1*2)));
if(counter<=number_of_inputs && counter!=-1)
{
if(count_2==n)
{
cout<<" = ";
cout<<input[count_1][count_2];
}
elseif(count_2==0)
cout<<input[count_1][count_2]<<" x"<<(count_2+1);
else
cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1);
}
elseif(count_2==n)
cout<<" = b"<<(count_1+1);
else
cout<<"a"<<(count_1+1)<<(count_2+1)<<" x"<<(count_2+1);
if(count_2<(n-1) && input[count_1][(count_2+1)]>=0)
cout<<" + ";
elseif(count_2<(n-1))
cout<<" - ";
counter++;
}
}
}
/*************************************************************************//
/-------------------- get_input_linear_equation( ) -------------------
///*************************************************************************
/void get_input_linear_equation( )
{
clear_screen( );
gotoxy(4,9);
cout<<"Size of the System of Linear Equations = n = "<<n;
gotoxy(4,13);
cout<<"System of Linear Equations :";
gotoxy(4,14);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
show_input(0,-1);
for(int count_1=0;count_1<n;count_1++)
{
for(int count_2=0;count_2<=n;count_2++)
{
gotoxy(4,35);
cout<<" ";
gotoxy(4,35);
if(count_2<n)
cout<<"Enter the value of a"<<(count_1+1)<<(count_2+1)<<" = ";
else
cout<<"Enter the value of b"<<(count_1+1)<<" = ";
cin>>input[count_1][count_2];
show_input(count_1,count_2);
}
}
}
/*************************************************************************//
/----------------- sort_system_of_linear_equations( ) ----------------
///*************************************************************************
/void sort_system_of_linear_equations( )
{
for(int count_1=0;count_1<(n-1);count_1++)
{
for(int count_2=count_1;count_2<(n-1);count_2++)
{
for(int count_3=count_1;count_3<(n-1);count_3++)
{
longdouble temp[max_size]={0};
if(fabs(input[count_3][count_1])<
fabs(input[(count_3+1)][count_1]))
{
for(int count_4=0;count_4<=n;count_4++)
temp[count_4]=input[count_3][count_4];
for(int count_5=0;count_5<=n;count_5++)
input[count_3][count_5]=
input[(count_3+1)][count_5];
for(int count_6=0;count_6<=n;count_6++)
input[(count_3+1)][count_6]=temp[count_6];
}
}
}
}
}
/*************************************************************************//
/--------------- apply_guass_seidel_iterative_method( ) --------------
///*************************************************************************
/void apply_guass_seidel_iterative_method( )
{
clear_screen( );
gotoxy(4,10);
cout<<"Solution :";
gotoxy(4,11);
cout<<"ÍÍÍÍÍÍÍÍÍÍ";
if(n==1)
{
gotoxy(4,13);
cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(4,14);
cout<<"³ n ³ X1 ³";
gotoxy(4,15);
cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(4,16);
cout<<"³ ³ ³";
}
elseif(n==2)
{
gotoxy(4,13);
cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(4,14);
cout<<"³ n ³ X1 ³ X2 ³";
gotoxy(4,15);
cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(4,16);
cout<<"³ ³ ³ ³";
}
elseif(n==3)
{
gotoxy(4,13);
cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄ¿";
gotoxy(4,14);
cout<<"³ n ³ X1 ³ X2 ³ X3 ³";
gotoxy(4,15);
cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄ´";
gotoxy(4,16);
cout<<"³ ³ ³ ³ ³";
}
elseif(n==4)
{
gotoxy(4,13);
cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(4,14);
cout<<"³ n ³ X1 ³ X2 ³ X3 ³ X4 ³";
gotoxy(4,15);
cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(4,16);
cout<<"³ ³ ³ ³ ³ ³";
}
for(int count_1=0;count_1<n;count_1++)
{
longdouble temp[max_size]={0};
for(int count_2=0;count_2<=n;count_2++)
{
if(count_2!=count_1)
temp[count_2]=(input[count_1][count_2]/input[count_1][count_1]);
}
for(int count_3=0;count_3<=n;count_3++)
input[count_1][count_3]=temp[count_3];
}
for(int count_4=0;count_4<n;count_4++)
{
for(int count_5=0;count_5<n;count_5++)
input[count_4][count_5]*=-1;
}
int x_cord=4;
int y_cord=17;
for(int count_6=0;count_6<=iterations;count_6++)
{
if(n==1)
{
gotoxy(x_cord,y_cord);
cout<<"³ ³ ³";
gotoxy(x_cord,(y_cord+1));
cout<<"³ ³ ³";
}
elseif(n==2)
{
gotoxy(x_cord,y_cord);
cout<<"³ ³ ³ ³";
gotoxy(x_cord,(y_cord+1));
cout<<"³ ³ ³ ³";
}
elseif(n==3)
{
gotoxy(x_cord,y_cord);
cout<<"³ ³ ³ ³ ³";
gotoxy(x_cord,(y_cord+1));
cout<<"³ ³ ³ ³ ³";
}
elseif(n==4)
{
gotoxy(x_cord,y_cord);
cout<<"³ ³ ³ ³ ³ ³";
gotoxy(x_cord,(y_cord+1));
cout<<"³ ³ ³ ³ ³ ³";
}
gotoxy((x_cord+3),y_cord);
cout<<count_6;
gotoxy((x_cord+10),y_cord);
cout<<output[0];
if(n>=2)
{
gotoxy((x_cord+26),y_cord);
cout<<output[1];
}
if(n>=3)
{
gotoxy((x_cord+42),y_cord);
cout<<output[2];
}
if(n>=4)
{
gotoxy((x_cord+58),y_cord);
cout<<output[3];
}
for(int count_7=0;count_7<n;count_7++)
{
output[count_7]=0;
for(int count_8=0;count_8<n;count_8++)
{
if(count_8!=count_7)
output[count_7]+=(input[count_7][count_8]*previous_output[count_8]);
}
output[count_7]+=input[count_7][count_8];
previous_output[count_7]=output[count_7];
}
y_cord+=2;
if((count_6%12)==0 && count_6<iterations && count_6>0)
{
y_cord=17;
gotoxy(30,44);
cout<<"Press any key to continue...";
getch( );
for(int count_9=1;count_9<25;count_9++)
{
gotoxy(3,(16+count_9));
cout<<" ";
}
}
}
if(n==1)
{
gotoxy(x_cord,y_cord);
cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
}
elseif(n==2)
{
gotoxy(x_cord,y_cord);
cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
}
elseif(n==3)
{
gotoxy(x_cord,y_cord);
cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄÙ";
}
elseif(n==4)
{
gotoxy(x_cord,y_cord);
cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";
}
gotoxy(30,44);
cout<<"Press any key to continue...";
getch( );
}
/*************************************************************************//
/----------------------------- show_result( ) ------------------------
///*************************************************************************
/void show_result( )
{
clear_screen( );
gotoxy(4,9);
cout<<"Gauss-Seidel System of Linear Equations :";
gotoxy(4,10);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
for(int count_1=0;count_1<n;count_1++)
{
gotoxy(4,(13+(count_1*3)));
cout<<"x"<<(count_1+1);
gotoxy((wherex( )-1),(wherey( )-1));
cout<<"(k+1)";
gotoxy((wherex( )-3),(wherey( )+1));
cout<<" = ";
for(int count_2=0;count_2<=n;count_2++)
{
gotoxy(wherex( ),(13+(count_1*3)));
if(count_2!=count_1 && count_2<n)
{
if(count_2!=0)
cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1);
else
cout<<input[count_1][count_2]<<" x"<<(count_2+1);
gotoxy((wherex( )-1),(wherey( )-1));
if(count_2<count_1)
cout<<"(k+1)";
else
cout<<"(k)";
gotoxy((wherex( )-1),(wherey( )+1));
if(input[count_1][(count_2+1)]>=0)
cout<<" + ";
else
cout<<" - ";
}
elseif(count_2!=count_1)
cout<<fabs(input[count_1][count_2]);
}
}
if(Diagonally_dominant=='Y' || Diagonally_dominant=='y')
{
gotoxy(4,26);
cout<<"Note: The given System of Linear Equations is solved by making it";
gotoxy(10,28);
cout<<"Diagonally Dominant.";
}
gotoxy(4,31);
cout<<"Result of Given System of Linear Equations :";
gotoxy(4,32);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
for(int count_3=0;count_3<n;count_3++)
{
gotoxy(8,(34+count_3+count_3));
cout<<"x"<<(count_3+1)<<" = "<<output[count_3];
}
gotoxy(4,(36+count_3+count_3));
cout<<"* Number of Iterations for the above result = "<<iterations<<".";
gotoxy(1,2);
}
7. Program untuk membaca Sistem Linear dari Persamaan, kemudian mengevaluasi
dengan menggunakan Metode Itrative Jacobi dan menampilkan hasil di C ++
Programming
# include <iostream.h>
# include <stdlib.h>
# include <stdio.h>
# include <conio.h>
# include <math.h>
constint max_size=5;
int n=0;
int iterations=0;
char Diagonally_dominant=NULL;
longdouble input[max_size][max_size]={0};
longdouble previous_output[max_size]={0};
longdouble output[max_size]={0};
void show_screen( );
void clear_screen( );
void get_size_of_linear_equations( );
void show_input(constint,constint);
void get_input_linear_equation( );
void sort_system_of_linear_equations( );
void apply_jacobi_iterative_method( );
void show_result( );
int main( )
{
clrscr( );
textmode(C4350);
show_screen( );
get_size_of_linear_equations( );
get_input_linear_equation( );
if(Diagonally_dominant=='Y' || Diagonally_dominant=='y')
sort_system_of_linear_equations( );
apply_jacobi_iterative_method( );
show_result( );
getch( );
return 0;
}
/*************************************************************************//
/-------------------------- show_screen( ) ---------------------------
///*************************************************************************
/void show_screen( )
{
cprintf("n******************************************************************
**************");
cprintf("*************************- -
*************************");
cprintf("*------------------------- ");
textbackground(1);
cprintf(" Jacobi's Itrative Method ");
textbackground(8);
cprintf(" -------------------------*");
cprintf("************************- -
*************************");
cprintf("********************************************************************
************");
for(int count=0;count<42;count++)
cprintf("* *");
gotoxy(1,46);
cprintf("********************************************************************
************");
cprintf("*------------------------------------------------------------------------------*");
cprintf("********************************************************************
************");
gotoxy(1,2);
}
/*************************************************************************//
/------------------------- clear_screen( ) ---------------------------
///*************************************************************************
/void clear_screen( )
{
textbackground(8);
for(int count=0;count<37;count++)
{
gotoxy(3,8+count);
cout<<" ";
}
gotoxy(1,2);
}
/*************************************************************************//
/------------------ get_size_of_linear_equations( ) ------------------
///*************************************************************************
/void get_size_of_linear_equations( )
{
do
{
clear_screen( );
gotoxy(38,11);
cout<<"[ Maximum size = 4 ]";
gotoxy(4,9);
cout<<"Enter the size of the System of Linear Equations = n = ";
cin>>n;
if(n<=0 || n>max_size)
{
gotoxy(12,25);
cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";
gotoxy(12,26);
cout<<" to try again.";
n=int(getche( ));
if(n==27)
exit(0);
}
}
while(n<=0 || n>max_size);
gotoxy(4,15);
cout<<"Enter the number of iterations that you want to perform = ";
cin>>iterations;
gotoxy(4,18);
cout<<"Do you want to make the System Diagonally Dominant : (Y/N) ? : ";
cin>>Diagonally_dominant;
gotoxy(1,2);
}
/*************************************************************************//
/----------------- show_input(const int,const int) -------------------
///*************************************************************************
/void show_input(constint x,constint y)
{
int counter=0;
int number_of_inputs=((x*n)+y+x);
for(int count_1=0;count_1<n;count_1++)
{
gotoxy(4,(17+(count_1*2)));
cout<<" ";
gotoxy(4,(17+(count_1*2)));
for(int count_2=0;count_2<=n;count_2++)
{
if(counter==(number_of_inputs+1))
textbackground(12);
else
textbackground(8);
if(count_2==n)
gotoxy((wherex( )+5),(17+(count_1*2)));
gotoxy(wherex( ),(17+(count_1*2)));
cprintf(" ");
if(count_2==n)
gotoxy((wherex( )-5),(17+(count_1*2)));
gotoxy((wherex( )-6),(17+(count_1*2)));
if(counter<=number_of_inputs && counter!=-1)
{
if(count_2==n)
{
cout<<" = ";
cout<<input[count_1][count_2];
}
elseif(count_2==0)
cout<<input[count_1][count_2]<<" x"<<(count_2+1);
else
cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1);
}
elseif(count_2==n)
cout<<" = b"<<(count_1+1);
else
cout<<"a"<<(count_1+1)<<(count_2+1)<<" x"<<(count_2+1);
if(count_2<(n-1) && input[count_1][(count_2+1)]>=0)
cout<<" + ";
elseif(count_2<(n-1))
cout<<" - ";
counter++;
}
}
}
/*************************************************************************//
/-------------------- get_input_linear_equation( ) -------------------
///*************************************************************************
/void get_input_linear_equation( )
{
clear_screen( );
gotoxy(4,9);
cout<<"Size of the System of Linear Equations = n = "<<n;
gotoxy(4,13);
cout<<"System of Linear Equations :";
gotoxy(4,14);
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
show_input(0,-1);
for(int count_1=0;count_1<n;count_1++)
{
for(int count_2=0;count_2<=n;count_2++)
{
gotoxy(4,35);
cout<<" ";
gotoxy(4,35);
if(count_2<n)
cout<<"Enter the value of a"<<(count_1+1)<<(count_2+1)<<" = ";
else
cout<<"Enter the value of b"<<(count_1+1)<<" = ";
cin>>input[count_1][count_2];
show_input(count_1,count_2);
}
}
}
/*************************************************************************//
/----------------- sort_system_of_linear_equations( ) ----------------
///*************************************************************************
/void sort_system_of_linear_equations( )
{
for(int count_1=0;count_1<(n-1);count_1++)
{
for(int count_2=count_1;count_2<(n-1);count_2++)
{
for(int count_3=count_1;count_3<(n-1);count_3++)
{
longdouble temp[max_size]={0};
if(fabs(input[count_3][count_1])<
fabs(input[(count_3+1)][count_1]))
{
for(int count_4=0;count_4<=n;count_4++)
temp[count_4]=input[count_3][count_4];
for(int count_5=0;count_5<=n;count_5++)
input[count_3][count_5]=
input[(count_3+1)][count_5];
for(int count_6=0;count_6<=n;count_6++)
input[(count_3+1)][count_6]=temp[count_6];
}
}
}
}
}
/*************************************************************************//
/------------------ apply_jacobi_iterative_method( ) -----------------
///*************************************************************************
/void apply_jacobi_iterative_method( )
{
clear_screen( );
gotoxy(4,10);
cout<<"Solution :";
gotoxy(4,11);
cout<<"ÍÍÍÍÍÍÍÍÍÍ";
if(n==1)
{
gotoxy(4,13);
cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(4,14);
cout<<"³ n ³ X1 ³";
gotoxy(4,15);
cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(4,16);
cout<<"³ ³ ³";
}
elseif(n==2)
{
gotoxy(4,13);
cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(4,14);
cout<<"³ n ³ X1 ³ X2 ³";
gotoxy(4,15);
cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(4,16);
cout<<"³ ³ ³ ³";
}
elseif(n==3)
{
gotoxy(4,13);
cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄ¿";
gotoxy(4,14);
cout<<"³ n ³ X1 ³ X2 ³ X3 ³";
gotoxy(4,15);
cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄ´";
gotoxy(4,16);
cout<<"³ ³ ³ ³ ³";
}
elseif(n==4)
{
gotoxy(4,13);
cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";
gotoxy(4,14);
cout<<"³ n ³ X1 ³ X2 ³ X3 ³ X4 ³";
gotoxy(4,15);
cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ
ÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
gotoxy(4,16);
cout<<"³ ³ ³ ³ ³ ³";
}
for(int count_1=0;count_1<n;count_1++)
{
longdouble temp[max_size]={0};
for(int count_2=0;count_2<=n;count_2++)
{
if(count_2!=count_1)
temp[count_2]=(input[count_1][count_2]/input[count_1][count_1]);
}
for(int count_3=0;count_3<=n;count_3++)
input[count_1][count_3]=temp[count_3];
}
for(int count_4=0;count_4<n;count_4++)
{
for(int count_5=0;count_5<n;count_5++)
input[count_4][count_5]*=-1;
}
int x_cord=4;
int y_cord=17;
for(int count_6=0;count_6<=iterations;count_6++)
{
if(n==1)
{
gotoxy(x_cord,y_cord);
cout<<"³ ³ ³";
gotoxy(x_cord,(y_cord+1));
cout<<"³ ³ ³";
}
elseif(n==2)
{
gotoxy(x_cord,y_cord);
cout<<"³ ³ ³ ³";
gotoxy(x_cord,(y_cord+1));
cout<<"³ ³ ³ ³";
}
elseif(n==3)
{
gotoxy(x_cord,y_cord);
cout<<"³ ³ ³ ³ ³";
gotoxy(x_cord,(y_cord+1));
cout<<"³ ³ ³ ³ ³";
}
elseif(n==4)
{
gotoxy(x_cord,y_cord);
cout<<"³ ³ ³ ³ ³ ³";
gotoxy(x_cord,(y_cord+1));
cout<<"³ ³ ³ ³ ³ ³";
}
gotoxy((x_cord+3),y_cord);
cout<<count_6;
gotoxy((x_cord+10),y_cord);
cout<<output[0];
if(n>=2)
{
gotoxy((x_cord+26),y_cord);
cout<<output[1];
}
if(n>=3)
{
gotoxy((x_cord+42),y_cord);
cout<<output[2];
}
if(n>=4)
{
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++
Aplikasi menghitung matematika dengan c++

More Related Content

What's hot

4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
Chhom Karath
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
C tech questions
C tech questionsC tech questions
C tech questions
vijay00791
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
Chhom Karath
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
vikram mahendra
 
Railwaynew
RailwaynewRailwaynew
Railwaynew
Arsh Vishwakarma
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Alex Penso Romero
 
Junaid program assignment
Junaid program assignmentJunaid program assignment
Junaid program assignment
Junaid Ahmed
 
Introduction to AspectJ
Introduction to AspectJIntroduction to AspectJ
Introduction to AspectJmukhtarhudaya
 
LET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERSLET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERS
KavyaSharma65
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
vrgokila
 
ML: A Strongly Typed Functional Language
ML: A Strongly Typed Functional LanguageML: A Strongly Typed Functional Language
ML: A Strongly Typed Functional Languagelijx127
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
Farhan Ab Rahman
 
User Account Access Graphs
User Account Access GraphsUser Account Access Graphs
User Account Access Graphs
National Chengchi University
 
C++ TUTORIAL 10
C++ TUTORIAL 10C++ TUTORIAL 10
C++ TUTORIAL 10
Farhan Ab Rahman
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
Ankit Kumar
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservationSwarup Kumar Boro
 
Python real time tutorial
Python real time tutorialPython real time tutorial
Python real time tutorial
Rajeev Kumar
 
Implementing string
Implementing stringImplementing string
Implementing string
mohamed sikander
 

What's hot (20)

4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
C tech questions
C tech questionsC tech questions
C tech questions
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
 
Railwaynew
RailwaynewRailwaynew
Railwaynew
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))
 
pointers 1
pointers 1pointers 1
pointers 1
 
Junaid program assignment
Junaid program assignmentJunaid program assignment
Junaid program assignment
 
Introduction to AspectJ
Introduction to AspectJIntroduction to AspectJ
Introduction to AspectJ
 
LET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERSLET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERS
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
ML: A Strongly Typed Functional Language
ML: A Strongly Typed Functional LanguageML: A Strongly Typed Functional Language
ML: A Strongly Typed Functional Language
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
User Account Access Graphs
User Account Access GraphsUser Account Access Graphs
User Account Access Graphs
 
C++ TUTORIAL 10
C++ TUTORIAL 10C++ TUTORIAL 10
C++ TUTORIAL 10
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
Python real time tutorial
Python real time tutorialPython real time tutorial
Python real time tutorial
 
Implementing string
Implementing stringImplementing string
Implementing string
 

Viewers also liked

Batxillerat tecnologic
Batxillerat tecnologicBatxillerat tecnologic
Batxillerat tecnologic
andreaisonia
 
Redundancy insurance
Redundancy insuranceRedundancy insurance
Redundancy insurance
redundancy23
 
ΣΤ Ιστορία Β8. Αδαμάντιος Κοραής
ΣΤ Ιστορία Β8. Αδαμάντιος ΚοραήςΣΤ Ιστορία Β8. Αδαμάντιος Κοραής
ΣΤ Ιστορία Β8. Αδαμάντιος Κοραής
George Giotis
 
Drive Rapid and Constant Growth in Your Online Business
Drive Rapid and Constant Growth in Your Online BusinessDrive Rapid and Constant Growth in Your Online Business
Drive Rapid and Constant Growth in Your Online BusinessMelbourne IT
 
Mengenal tokoh1
Mengenal tokoh1Mengenal tokoh1
Mengenal tokoh1
radar radius
 
Visual troubleshooting
Visual troubleshootingVisual troubleshooting
Visual troubleshooting
Veronica Alejandro
 
Placement training
Placement trainingPlacement training
Placement training
Arjun Pillai
 
Chapter 4.ppt
Chapter 4.pptChapter 4.ppt
Chapter 4.ppt
drmarjie12
 
Top 8 placement officer resume samples
Top 8 placement officer resume samplesTop 8 placement officer resume samples
Top 8 placement officer resume sampleskicarjom
 
Law and Justice Chapter 2 power point
Law and Justice Chapter 2 power pointLaw and Justice Chapter 2 power point
Law and Justice Chapter 2 power point
mckenziewood
 

Viewers also liked (12)

Batxillerat tecnologic
Batxillerat tecnologicBatxillerat tecnologic
Batxillerat tecnologic
 
Redundancy insurance
Redundancy insuranceRedundancy insurance
Redundancy insurance
 
ΣΤ Ιστορία Β8. Αδαμάντιος Κοραής
ΣΤ Ιστορία Β8. Αδαμάντιος ΚοραήςΣΤ Ιστορία Β8. Αδαμάντιος Κοραής
ΣΤ Ιστορία Β8. Αδαμάντιος Κοραής
 
Drive Rapid and Constant Growth in Your Online Business
Drive Rapid and Constant Growth in Your Online BusinessDrive Rapid and Constant Growth in Your Online Business
Drive Rapid and Constant Growth in Your Online Business
 
Mengenal tokoh1
Mengenal tokoh1Mengenal tokoh1
Mengenal tokoh1
 
Visual troubleshooting
Visual troubleshootingVisual troubleshooting
Visual troubleshooting
 
Placement training
Placement trainingPlacement training
Placement training
 
MOOCS الموك
MOOCS الموكMOOCS الموك
MOOCS الموك
 
Chapter 4.ppt
Chapter 4.pptChapter 4.ppt
Chapter 4.ppt
 
Top 8 placement officer resume samples
Top 8 placement officer resume samplesTop 8 placement officer resume samples
Top 8 placement officer resume samples
 
Law and Justice Chapter 2 power point
Law and Justice Chapter 2 power pointLaw and Justice Chapter 2 power point
Law and Justice Chapter 2 power point
 
NeKoLX5_Gen5Manual
NeKoLX5_Gen5ManualNeKoLX5_Gen5Manual
NeKoLX5_Gen5Manual
 

Similar to Aplikasi menghitung matematika dengan c++

Lập trình C
Lập trình CLập trình C
Lập trình C
Viet NguyenHoang
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
apleather
 
I am trying to implement timing on this program and cannot do it. Wh.pdf
I am trying to implement timing on this program and cannot do it. Wh.pdfI am trying to implement timing on this program and cannot do it. Wh.pdf
I am trying to implement timing on this program and cannot do it. Wh.pdf
allystraders
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
Kandarp Tiwari
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
Ashutoshprasad27
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
MomenMostafa
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
MomenMostafa
 
C file
C fileC file
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8alish sha
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8alish sha
 
Bank management system project in c++ with graphics
Bank management system project in c++ with graphicsBank management system project in c++ with graphics
Bank management system project in c++ with graphics
Vtech Academy of Computers
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
Saket Pathak
 
Useful c programs
Useful c programsUseful c programs
Useful c programs
MD SHAH FAHAD
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
yap_raiza
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdf
Asrinath1
 

Similar to Aplikasi menghitung matematika dengan c++ (20)

Lập trình C
Lập trình CLập trình C
Lập trình C
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
 
I am trying to implement timing on this program and cannot do it. Wh.pdf
I am trying to implement timing on this program and cannot do it. Wh.pdfI am trying to implement timing on this program and cannot do it. Wh.pdf
I am trying to implement timing on this program and cannot do it. Wh.pdf
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
C file
C fileC file
C file
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Bank management system project in c++ with graphics
Bank management system project in c++ with graphicsBank management system project in c++ with graphics
Bank management system project in c++ with graphics
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Useful c programs
Useful c programsUseful c programs
Useful c programs
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
data structure and algorithm.pdf
data structure and algorithm.pdfdata structure and algorithm.pdf
data structure and algorithm.pdf
 

More from radar radius

Gas Air Mata: Zat Kimia, Metode Penyebaran, dan Efek.docx
Gas Air Mata: Zat Kimia, Metode Penyebaran, dan Efek.docxGas Air Mata: Zat Kimia, Metode Penyebaran, dan Efek.docx
Gas Air Mata: Zat Kimia, Metode Penyebaran, dan Efek.docx
radar radius
 
IPA SMP_KK C.pdf
IPA SMP_KK C.pdfIPA SMP_KK C.pdf
IPA SMP_KK C.pdf
radar radius
 
IPA SMP_KK B.pdf
IPA SMP_KK B.pdfIPA SMP_KK B.pdf
IPA SMP_KK B.pdf
radar radius
 
الإيمان
الإيمانالإيمان
الإيمان
radar radius
 
Wahyu
WahyuWahyu
Urutan peristiwa kiamat besar
Urutan peristiwa kiamat besarUrutan peristiwa kiamat besar
Urutan peristiwa kiamat besar
radar radius
 
Toleransi
ToleransiToleransi
Toleransi
radar radius
 
Toleransi (hadis)
Toleransi (hadis)Toleransi (hadis)
Toleransi (hadis)
radar radius
 
Tauhid dalam konsep islam
Tauhid dalam konsep islamTauhid dalam konsep islam
Tauhid dalam konsep islam
radar radius
 
Tajwid (mad)
Tajwid (mad)Tajwid (mad)
Tajwid (mad)
radar radius
 
Qurdis 7 1
Qurdis 7 1Qurdis 7 1
Qurdis 7 1
radar radius
 
Qurban
QurbanQurban
Qurban
radar radius
 
Problematika dakwah
Problematika dakwahProblematika dakwah
Problematika dakwah
radar radius
 
Melestarikan alam (hadis)
Melestarikan alam (hadis)Melestarikan alam (hadis)
Melestarikan alam (hadis)
radar radius
 
Materi qurdis IX 2
Materi qurdis IX 2Materi qurdis IX 2
Materi qurdis IX 2
radar radius
 
Materi Quran Hadits VIII 2
Materi Quran Hadits VIII 2Materi Quran Hadits VIII 2
Materi Quran Hadits VIII 2
radar radius
 
Fenomena alam kiamat
Fenomena alam kiamatFenomena alam kiamat
Fenomena alam kiamat
radar radius
 
Materi Quran Hadist IX 2
Materi Quran Hadist IX 2Materi Quran Hadist IX 2
Materi Quran Hadist IX 2
radar radius
 
Doa nur buwwah lengkap
Doa nur buwwah lengkapDoa nur buwwah lengkap
Doa nur buwwah lengkap
radar radius
 
Gerak pada Tumbuhan dan Hewan
Gerak pada Tumbuhan dan HewanGerak pada Tumbuhan dan Hewan
Gerak pada Tumbuhan dan Hewan
radar radius
 

More from radar radius (20)

Gas Air Mata: Zat Kimia, Metode Penyebaran, dan Efek.docx
Gas Air Mata: Zat Kimia, Metode Penyebaran, dan Efek.docxGas Air Mata: Zat Kimia, Metode Penyebaran, dan Efek.docx
Gas Air Mata: Zat Kimia, Metode Penyebaran, dan Efek.docx
 
IPA SMP_KK C.pdf
IPA SMP_KK C.pdfIPA SMP_KK C.pdf
IPA SMP_KK C.pdf
 
IPA SMP_KK B.pdf
IPA SMP_KK B.pdfIPA SMP_KK B.pdf
IPA SMP_KK B.pdf
 
الإيمان
الإيمانالإيمان
الإيمان
 
Wahyu
WahyuWahyu
Wahyu
 
Urutan peristiwa kiamat besar
Urutan peristiwa kiamat besarUrutan peristiwa kiamat besar
Urutan peristiwa kiamat besar
 
Toleransi
ToleransiToleransi
Toleransi
 
Toleransi (hadis)
Toleransi (hadis)Toleransi (hadis)
Toleransi (hadis)
 
Tauhid dalam konsep islam
Tauhid dalam konsep islamTauhid dalam konsep islam
Tauhid dalam konsep islam
 
Tajwid (mad)
Tajwid (mad)Tajwid (mad)
Tajwid (mad)
 
Qurdis 7 1
Qurdis 7 1Qurdis 7 1
Qurdis 7 1
 
Qurban
QurbanQurban
Qurban
 
Problematika dakwah
Problematika dakwahProblematika dakwah
Problematika dakwah
 
Melestarikan alam (hadis)
Melestarikan alam (hadis)Melestarikan alam (hadis)
Melestarikan alam (hadis)
 
Materi qurdis IX 2
Materi qurdis IX 2Materi qurdis IX 2
Materi qurdis IX 2
 
Materi Quran Hadits VIII 2
Materi Quran Hadits VIII 2Materi Quran Hadits VIII 2
Materi Quran Hadits VIII 2
 
Fenomena alam kiamat
Fenomena alam kiamatFenomena alam kiamat
Fenomena alam kiamat
 
Materi Quran Hadist IX 2
Materi Quran Hadist IX 2Materi Quran Hadist IX 2
Materi Quran Hadist IX 2
 
Doa nur buwwah lengkap
Doa nur buwwah lengkapDoa nur buwwah lengkap
Doa nur buwwah lengkap
 
Gerak pada Tumbuhan dan Hewan
Gerak pada Tumbuhan dan HewanGerak pada Tumbuhan dan Hewan
Gerak pada Tumbuhan dan Hewan
 

Recently uploaded

The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 

Recently uploaded (20)

The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 

Aplikasi menghitung matematika dengan c++

  • 1. 1. Program untuk memperkirakan nilai Integral dari fungsi yang diberikan menggunakan aturan kuadrat Gaussian di C ++ Programming # include <iostream.h> # include <stdlib.h> # include <string.h> # include <stdio.h> # include <conio.h> # include <math.h> constint max_size=13; int n=0; int top=-1; longdouble a=-1; longdouble b=1; longdouble two_point_result=0; longdouble three_point_result=0; char Fx[100]={NULL}; char Stack[30][30]={NULL}; char Postfix_expression[30][30]={NULL}; /*************************************************************************// *************************************************************************/// ------------------------ Funcion Prototypes ------------------------- ///************************************************************************* //*************************************************************************/ void push(constchar *); void convert_ie_to_pe(constchar *); constchar* pop( ); constlongdouble evaluate_postfix_expression(constlongdouble); void show_screen( ); void clear_screen( ); void get_input( ); void change_limits( ); void apply_gaussian_quadrature_rule( ); void show_result( ); /*************************************************************************// *************************************************************************/// ------------------------------ main( ) ------------------------------ ///************************************************************************* //*************************************************************************/ int main( ) {
  • 2. clrscr( ); textmode(C4350); show_screen( ); get_input( ); show_result( ); getch( ); return 0; } /*************************************************************************// *************************************************************************/// ------------------------ Funcion Definitions ------------------------ ///************************************************************************* //*************************************************************************/ /*************************************************************************// /-------------------------- show_screen( ) --------------------------- ///************************************************************************* /void show_screen( ) { cprintf("n****************************************************************** **************"); cprintf("***************************- - **************************"); cprintf("*--------------------------- "); textbackground(1); cprintf("Integrasi numerik "); textbackground(8); cprintf(" --------------------------*"); cprintf("*-*************************- - ************************-*"); cprintf("*- *************************************************************************** *-*"); for(int count=0;count<42;count++) cprintf("*-* *-*"); gotoxy(1,46); cprintf("*- *************************************************************************** *-*"); cprintf("*------------------------------------------------------------------------------*");
  • 3. cprintf("******************************************************************** ************"); gotoxy(1,2); } /*************************************************************************// /------------------------- clear_screen( ) --------------------------- ///************************************************************************* /void clear_screen( ) { for(int count=0;count<37;count++) { gotoxy(5,8+count); cout<<" "; } gotoxy(1,2); } /*************************************************************************// /-------------------------- push(const char*) ------------------------ ///************************************************************************* /void push(constchar* Operand) { if(top==(max_size-1)) { cout<<"Error : Stack is full."<<endl; cout<<"n Press any key to exit."; getch( ); exit(0); } else { top++; strcpy(Stack[top],Operand); } } /*************************************************************************// /------------------------------ pop( ) ------------------------------- ///************************************************************************* /constchar* pop( ) {
  • 4. char Operand[40]={NULL}; if(top==-1) { cout<<"Error : Stack is empty."<<endl; cout<<"n Press any key to exit."; getch( ); exit(0); } else { strcpy(Operand,Stack[top]); strset(Stack[top],NULL); top--; } return Operand; } /*************************************************************************// /-------------------- convert_ie_to_pe(const char*) ------------------ ///************************************************************************* /void convert_ie_to_pe(constchar* Expression) { char Infix_expression[100]={NULL}; char Symbol_scanned[30]={NULL}; push("("); strcpy(Infix_expression,Expression); strcat(Infix_expression,"+0)"); int flag=0; int count_1=0; int count_2=0; int equation_length=strlen(Infix_expression); if(Infix_expression[0]=='(') flag=1; do { strset(Symbol_scanned,NULL); if(flag==0) { int count_3=0;
  • 5. do { Symbol_scanned[count_3]=Infix_expression[count_1]; count_1++; count_3++; } while(count_1<=equation_length && Infix_expression[count_1]!='(' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!=')'); flag=1; } elseif(flag==1) { Symbol_scanned[0]=Infix_expression[count_1]; count_1++; if(Infix_expression[count_1]!='(' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!=')') flag=0; if(Infix_expression[count_1-1]=='(' && (Infix_expression[count_1]=='-' || Infix_expression[count_1]=='+')) flag=0; } if(strcmp(Symbol_scanned,"(")==0) push("("); elseif(strcmp(Symbol_scanned,")")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[count_2],pop( ));
  • 6. count_2++; } pop( ); } elseif(strcmp(Symbol_scanned,"^")==0 || strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0) { if(strcmp(Symbol_scanned,"^")==0) { } elseif(strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0) { while(strcmp(Stack[top],"^")==0 || strcmp(Stack[top],"*")==0 || strcmp(Stack[top],"/")==0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } elseif(strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } push(Symbol_scanned); } else { strcat(Postfix_expression[count_2],Symbol_scanned); count_2++; } } while(strcmp(Stack[top],NULL)!=0);
  • 7. strcat(Postfix_expression[count_2],"="); count_2++; } /*************************************************************************// /---------- evaluate_postfix_expression(const long double) ----------- ///************************************************************************* /constlongdouble evaluate_postfix_expression(constlongdouble x) { longdouble function_value=0; int count_1=-1; char Symbol_scanned[30]={NULL}; do { count_1++; strcpy(Symbol_scanned,Postfix_expression[count_1]); if(strcmp(Symbol_scanned,"^")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0 || strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { char Result[30]={NULL}; char Operand[2][30]={NULL}; strcpy(Operand[0],pop( )); strcpy(Operand[1],pop( )); longdouble operand[2]={0}; longdouble result=0; char *endptr; for(int count_2=0;count_2<2;count_2++) { int flag=0; if(Operand[count_2][0]=='-') { int length=strlen(Operand[count_2]); for(int count_3=0;count_3<(length-1);count_3++)
  • 8. Operand[count_2][count_3]=Operand[count_2][(count_3+1)]; Operand[count_2][count_3]=NULL; flag=1; } if(strcmp(Operand[count_2],"x")==0) operand[count_2]=x; elseif(strcmp(Operand[count_2],"e")==0) operand[count_2]=2.718282; elseif(strcmp(Operand[count_2],"sinx")==0) operand[count_2]=sinl(x); elseif(strcmp(Operand[count_2],"cosx")==0) operand[count_2]=cosl(x); elseif(strcmp(Operand[count_2],"tanx")==0) operand[count_2]=tanl(x); elseif(strcmp(Operand[count_2],"lnx")==0) operand[count_2]=logl(x); elseif(strcmp(Operand[count_2],"logx")==0) operand[count_2]=log10l(x); else operand[count_2]=strtod(Operand[count_2],&endptr); if(flag) operand[count_2]*=-1; } switch(Symbol_scanned[0]) { case'^' : result=powl(operand[1],operand[0]); break; case'*' : result=operand[1]*operand[0]; break; case'/' : result=operand[1]/operand[0]; break; case'+' : result=operand[1]+operand[0]; break; case'-' : result=operand[1]-operand[0];
  • 9. break; } gcvt(result,25,Result); push(Result); } elseif(strcmp(Symbol_scanned,"=")!=0) push(Symbol_scanned); } while(strcmp(Symbol_scanned,"=")!=0); char Function_value[30]={NULL}; char *endptr; strcpy(Function_value,pop( )); function_value=strtod(Function_value,&endptr); return function_value; } /*************************************************************************// /----------------------------- get_input( ) -------------------------- ///************************************************************************* /void get_input( ) { clear_screen( ); gotoxy(6,10); cout<<"Non-Linear Function :"; gotoxy(6,11); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(6,37); cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3"; gotoxy(6,40); cout<<"Available Operators : ^ (raised to power) , * , / , + , -"; gotoxy(6,42); cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,"; gotoxy(6,44); cout<<" n = any number"; gotoxy(6,14); cout<<"Enter the Function : f(x) = ";
  • 10. cin>>Fx; convert_ie_to_pe(Fx); } /*************************************************************************// /----------------- apply_gaussian_quadrature_rule( ) ----------------- ///************************************************************************* /void apply_gaussian_quadrature_rule( ) { longdouble temp_1=0; longdouble temp_2=0; longdouble temp_3=0; temp_1=evaluate_postfix_expression((-sqrtl(0.6))); temp_1*=(0.555555556); temp_2=evaluate_postfix_expression(0); temp_2*=(0.888888889); temp_3=evaluate_postfix_expression(sqrtl(0.6)); temp_3*=(0.555555556); three_point_result=(temp_1+temp_2+temp_3); temp_1=0; temp_2=0; temp_1=evaluate_postfix_expression((-1/sqrtl(3))); temp_2=evaluate_postfix_expression((1/sqrtl(3))); two_point_result=(temp_1+temp_2); } /*************************************************************************// /---------------------------- show_result( ) ------------------------- ///************************************************************************* /void show_result( ) { clear_screen( ); gotoxy(6,9); cout<<"Gaussian Quadrature Rule:"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
  • 11. gotoxy(24,12); cout<<" 1ô"; gotoxy(6,13); cout<<"Two Point Rule: ³f(x)dx ÷ f(-1/û3)+f(1/û3)"; gotoxy(24,14); cout<<"-1õ"; gotoxy(24,16); cout<<" 1ô"; gotoxy(6,17); cout<<"Three Point Rule: ³f(x)dx ÷ (5/9)f(-û(3/5))+(8/9)f(0)+(5/9)f(û(3/5))"; gotoxy(24,18); cout<<"-1õ"; apply_gaussian_quadrature_rule( ); gotoxy(6,22); cout<<" 1ô"; gotoxy(6,24); cout<<" -1õ"; gotoxy(6,23); cout<<"Estimation of ³f(x)dx :"; gotoxy(6,25); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(8,27); cout<<"Estimated Integral Value (using 2-Point Rule) = "<<two_point_result; gotoxy(8,29); cout<<"Estimated Integral Value (using 3-Point Rule) = "<<three_point_result; gotoxy(1,2); } 2. Program untuk memperkirakan nilai Integral dari fungsi di poin yang diberikan dari data yang diberikan dengan Metode Romberg di C ++ Programming # include <iostream.h> # include <stdlib.h> # include <string.h> # include <stdio.h> # include <conio.h> # include <math.h>
  • 12. constint max_size=8; int n=0; int top=-1; longdouble h=0; longdouble a=0; longdouble b=0; longdouble ri[max_size][max_size]={0}; longdouble xn[max_size]={0}; longdouble fx[max_size]={0}; char Fx[100]={NULL}; char Stack[30][30]={NULL}; char Postfix_expression[30][30]={NULL}; /*************************************************************************// *************************************************************************/// ------------------------ Funcion Prototypes ------------------------- ///************************************************************************* //*************************************************************************/ void push(constchar *); void convert_ie_to_pe(constchar *); constchar* pop( ); constlongdouble evaluate_postfix_expression(constlongdouble); void show_screen( ); void clear_screen( ); void get_input( ); void apply_trapezoidal_rule( ); void apply_romberg_method( ); void show_result( ); /*************************************************************************// *************************************************************************/// ------------------------------ main( ) ------------------------------ ///************************************************************************* //*************************************************************************/ int main( ) { clrscr( ); textmode(C4350); show_screen( ); get_input( );
  • 13. apply_trapezoidal_rule( ); apply_romberg_method( ); show_result( ); getch( ); return 0; } /*************************************************************************// *************************************************************************/// ------------------------ Funcion Definitions ------------------------ ///************************************************************************* //*************************************************************************/ /*************************************************************************// /-------------------------- show_screen( ) --------------------------- ///************************************************************************* /void show_screen( ) { cprintf("n****************************************************************** **************"); cprintf("****************************- - ***************************"); cprintf("*---------------------------- "); textbackground(1); cprintf(" Romberg Integration "); textbackground(8); cprintf(" ---------------------------*"); cprintf("*-**************************- - *************************-*"); cprintf("*- *************************************************************************** *-*"); for(int count=0;count<42;count++) cprintf("*-* *-*"); gotoxy(1,46); cprintf("*- *************************************************************************** *-*"); cprintf("*------------------------------------------------------------------------------*"); cprintf("******************************************************************** ************"); gotoxy(1,2);
  • 14. } /*************************************************************************// /------------------------- clear_screen( ) --------------------------- ///************************************************************************* /void clear_screen( ) { for(int count=0;count<37;count++) { gotoxy(5,8+count); cout<<" "; } gotoxy(1,2); } /*************************************************************************// /-------------------------- push(const char*) ------------------------ ///************************************************************************* /void push(constchar* Operand) { if(top==(max_size-1)) { cout<<"Error : Stack is full."<<endl; cout<<"n Press any key to exit."; getch( ); exit(0); } else { top++; strcpy(Stack[top],Operand); } } /*************************************************************************// /------------------------------ pop( ) ------------------------------- ///************************************************************************* /constchar* pop( ) { char Operand[40]={NULL}; if(top==-1) { cout<<"Error : Stack is empty."<<endl;
  • 15. cout<<"n Press any key to exit."; getch( ); exit(0); } else { strcpy(Operand,Stack[top]); strset(Stack[top],NULL); top--; } return Operand; } /*************************************************************************// /-------------------- convert_ie_to_pe(const char*) ------------------ ///************************************************************************* /void convert_ie_to_pe(constchar* Expression) { char Infix_expression[100]={NULL}; char Symbol_scanned[30]={NULL}; push("("); strcpy(Infix_expression,Expression); strcat(Infix_expression,"+0)"); int flag=0; int count_1=0; int count_2=0; int equation_length=strlen(Infix_expression); if(Infix_expression[0]=='(') flag=1; do { strset(Symbol_scanned,NULL); if(flag==0) { int count_3=0; do { Symbol_scanned[count_3]=Infix_expression[count_1]; count_1++;
  • 16. count_3++; } while(count_1<=equation_length && Infix_expression[count_1]!='(' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!=')'); flag=1; } elseif(flag==1) { Symbol_scanned[0]=Infix_expression[count_1]; count_1++; if(Infix_expression[count_1]!='(' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!=')') flag=0; if(Infix_expression[count_1-1]=='(' && (Infix_expression[count_1]=='-' || Infix_expression[count_1]=='+')) flag=0; } if(strcmp(Symbol_scanned,"(")==0) push("("); elseif(strcmp(Symbol_scanned,")")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } pop( ); }
  • 17. elseif(strcmp(Symbol_scanned,"^")==0 || strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0) { if(strcmp(Symbol_scanned,"^")==0) { } elseif(strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0) { while(strcmp(Stack[top],"^")==0 || strcmp(Stack[top],"*")==0 || strcmp(Stack[top],"/")==0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } elseif(strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } push(Symbol_scanned); } else { strcat(Postfix_expression[count_2],Symbol_scanned); count_2++; } } while(strcmp(Stack[top],NULL)!=0); strcat(Postfix_expression[count_2],"="); count_2++; }
  • 18. /*************************************************************************// /---------- evaluate_postfix_expression(const long double) ----------- ///************************************************************************* /constlongdouble evaluate_postfix_expression(constlongdouble x) { longdouble function_value=0; int count_1=-1; char Symbol_scanned[30]={NULL}; do { count_1++; strcpy(Symbol_scanned,Postfix_expression[count_1]); if(strcmp(Symbol_scanned,"^")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0 || strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { char Result[30]={NULL}; char Operand[2][30]={NULL}; strcpy(Operand[0],pop( )); strcpy(Operand[1],pop( )); longdouble operand[2]={0}; longdouble result=0; char *endptr; for(int count_2=0;count_2<2;count_2++) { int flag=0; if(Operand[count_2][0]=='-') { int length=strlen(Operand[count_2]); for(int count_3=0;count_3<(length-1);count_3++) Operand[count_2][count_3]=Operand[count_2][(count_3+1)]; Operand[count_2][count_3]=NULL; flag=1;
  • 19. } if(strcmp(Operand[count_2],"x")==0) operand[count_2]=x; elseif(strcmp(Operand[count_2],"e")==0) operand[count_2]=2.718282; elseif(strcmp(Operand[count_2],"sinx")==0) operand[count_2]=sinl(x); elseif(strcmp(Operand[count_2],"cosx")==0) operand[count_2]=cosl(x); elseif(strcmp(Operand[count_2],"tanx")==0) operand[count_2]=tanl(x); elseif(strcmp(Operand[count_2],"lnx")==0) operand[count_2]=logl(x); elseif(strcmp(Operand[count_2],"logx")==0) operand[count_2]=log10l(x); else operand[count_2]=strtod(Operand[count_2],&endptr); if(flag) operand[count_2]*=-1; } switch(Symbol_scanned[0]) { case'^' : result=powl(operand[1],operand[0]); break; case'*' : result=operand[1]*operand[0]; break; case'/' : result=operand[1]/operand[0]; break; case'+' : result=operand[1]+operand[0]; break; case'-' : result=operand[1]-operand[0]; break; } gcvt(result,25,Result);
  • 20. push(Result); } elseif(strcmp(Symbol_scanned,"=")!=0) push(Symbol_scanned); } while(strcmp(Symbol_scanned,"=")!=0); char Function_value[30]={NULL}; char *endptr; strcpy(Function_value,pop( )); function_value=strtod(Function_value,&endptr); return function_value; } /*************************************************************************// /----------------------------- get_input( ) -------------------------- ///************************************************************************* /void get_input( ) { do { clear_screen( ); gotoxy(6,9); cout<<"Input :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍ"; gotoxy(37,13); cout<<"[ n = 2,4,8 ]"; gotoxy(6,12); cout<<"Enter the max. number of sub-intervals = n = "; cin>>n; if(n!=2 && n!=4 && n!=8) { gotoxy(12,25); cout<<"Error : Wrong Input. Press <Esc> to exit or any other key"; gotoxy(12,26); cout<<" to try again."; n=int(getche( ));
  • 21. if(n==27) exit(0); } } while(n!=2 && n!=4 && n!=8); gotoxy(6,16); cout<<"Enter the value of Lower limit = a = "; cin>>a; gotoxy(6,18); cout<<"Enter the value of Upper Limit = b = "; cin>>b; gotoxy(25,43); cout<<"Press any key to continue..."; getch( ); clear_screen( ); gotoxy(6,10); cout<<"Non-Linear Function :"; gotoxy(6,11); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(6,37); cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3"; gotoxy(6,40); cout<<"Available Operators : ^ (raised to power) , * , / , + , -"; gotoxy(6,42); cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,"; gotoxy(6,44); cout<<" n = any number"; gotoxy(6,14); cout<<"Enter the Function : f(x) = "; cin>>Fx; convert_ie_to_pe(Fx); }
  • 22. /*************************************************************************// /---------------------- apply_trapezoidal_rule( ) -------------------- ///************************************************************************* /void apply_trapezoidal_rule( ) { int count_1=0; for(int count_2=1;count_2<=n;count_2+=count_2) { for(int count_3=0;count_3<max_size;count_3++) { xn[count_3]=0; fx[count_3]=0; } h=((b-a)/count_2); xn[0]=a; for(int count_4=0;count_4<count_2;count_4++) xn[(count_4+1)]=(xn[count_4]+h); for(int count_5=0;count_5<=count_2;count_5++) fx[count_5]=evaluate_postfix_expression(xn[count_5]); for(int count_6=1;count_6<count_2;count_6++) ri[count_1][0]+=fx[count_6]; ri[count_1][0]*=2; ri[count_1][0]+=fx[0]; ri[count_1][0]+=fx[count_2]; ri[count_1][0]*=(h/2); count_1++; } } /*************************************************************************// /---------------------- apply_romberg_method( ) ---------------------- ///************************************************************************* /void apply_romberg_method( ) { int counter=0; switch(n) { case 2 : counter=1; break;
  • 23. case 4 : counter=2; break; case 8 : counter=3; break; } for(int count_1=1;count_1<=counter;count_1++) { for(int count_2=count_1;count_2<=counter;count_2++) { longdouble rjk_1=0; longdouble rj_1k_1=0; rjk_1=ri[count_2][(count_1-1)]; rj_1k_1=ri[(count_2-1)][(count_1-1)]; ri[count_2][count_1]=(((powl(4,count_1)*rjk_1)-rj_1k_1)/(powl(4,count_1)-1)); } } } /*************************************************************************// /---------------------------- show_result( ) ------------------------- ///************************************************************************* /void show_result( ) { clear_screen( ); int counter=0; switch(n) { case 2 : counter=1; break; case 4 : counter=2; break; case 8 : counter=3; break; } gotoxy(6,10); cout<<"Solution :"; gotoxy(6,11); cout<<"ÍÍÍÍÍÍÍÍÍÍ";
  • 24. if(n==1) { gotoxy(6,13); cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; } elseif(n==2) { gotoxy(6,13); cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; } elseif(n==4) { gotoxy(6,13); cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ ÄÄÄÄÄÄÄ¿"; } elseif(n==8) { gotoxy(6,13); cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; } if(n==1) { gotoxy(6,14); cout<<"³ Number of ³ Trapezoidal ³"; } elseif(n==2) { gotoxy(6,14); cout<<"³ Number of ³ Trapezoidal ³ Romberg Values ³"; } elseif(n==4) { gotoxy(6,14); cout<<"³ Number of ³ Trapezoidal ³ Romberg Values ³"; } elseif(n==8) { gotoxy(6,14);
  • 25. cout<<"³ Number of ³ Trapezoidal ³ Romberg Values ³"; } if(n==1) { gotoxy(6,15); cout<<"³ ³ ³"; } elseif(n==2) { gotoxy(6,15); cout<<"³ ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; } elseif(n==4) { gotoxy(6,15); cout<<"³ ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; } elseif(n==8) { gotoxy(6,15); cout<<"³ ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; } if(n==1) { gotoxy(6,16); cout<<"³ Intervals ³ Sums ³"; } elseif(n==2) { gotoxy(6,16); cout<<"³ Intervals ³ Sums ³ 1ts Order ³"; } elseif(n==4) { gotoxy(6,16); cout<<"³ Intervals ³ Sums ³ 1ts Order ³ 2nd Order ³"; } elseif(n==8) { gotoxy(6,16); cout<<"³ Intervals ³ Sums ³ 1ts Order ³ 2nd Order ³ 3rd Order ³";
  • 27. gotoxy(6,18); cout<<"³ ³ ³ ³ ³ ³"; } int y_cord=19; int intervals=1; for(int count_1=0;count_1<=counter;count_1++) { if(n==1) { gotoxy(6,y_cord); cout<<"³ ³ ³"; gotoxy(6,(y_cord+1)); cout<<"³ ³ ³"; } elseif(n==2) { gotoxy(6,y_cord); cout<<"³ ³ ³ ³"; gotoxy(6,(y_cord+1)); cout<<"³ ³ ³ ³"; } elseif(n==4) { gotoxy(6,y_cord); cout<<"³ ³ ³ ³ ³"; gotoxy(6,(y_cord+1)); cout<<"³ ³ ³ ³ ³"; } elseif(n==8) { gotoxy(6,y_cord); cout<<"³ ³ ³ ³ ³ ³"; gotoxy(6,(y_cord+1)); cout<<"³ ³ ³ ³ ³ ³"; } gotoxy(11,(19+(count_1*2))); cout<<intervals; intervals+=intervals; y_cord+=2;
  • 28. } for(int count_2=0;count_2<=counter;count_2++) { for(int count_3=0;count_3<=counter;count_3++) { if(count_2==0) { gotoxy(20,(19+(count_3*2))); cout<<ri[count_3][0]; } elseif(count_2==1 && count_3>=count_2) { gotoxy(34,(19+(count_3*2))); cout<<ri[count_3][1]; } elseif(count_2==2 && count_3>=count_2) { gotoxy(48,(19+(count_3*2))); cout<<ri[count_3][2]; } elseif(count_2==3 && count_3>=count_2) { gotoxy(62,(19+(count_3*2))); cout<<ri[count_3][3]; } } } if(n==1) { gotoxy(6,y_cord); cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; } elseif(n==2) { gotoxy(6,y_cord); cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; } elseif(n==4) { gotoxy(6,y_cord);
  • 29. cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ ÄÄÄÄÄÄÄÙ"; } elseif(n==8) { gotoxy(6,y_cord); cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ ÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; } gotoxy(6,32); cout<<" bô"; gotoxy(6,34); cout<<" aõ"; gotoxy(6,33); cout<<"Estimation of ³f(x)dx :"; gotoxy(6,35); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(8,37); cout<<"Estimated Integral Value = "; cout<<ri[counter][counter]; gotoxy(1,2); } 3. Program untuk memperkirakan nilai Integral dari fungsi di poin yang diberikan dari data yang diberikan menggunakan 1/3 Aturan Simpson di C ++ Programming # include <iostream.h> # include <stdlib.h> # include <string.h> # include <stdio.h> # include <conio.h> # include <math.h> constint max_size=13; int n=0; int top=-1; int choice=0; longdouble h=0; longdouble a=0;
  • 30. longdouble b=0; longdouble estimated_value=0; longdouble xn[max_size]={0}; longdouble fx[max_size]={0}; char Fx[100]={NULL}; char Stack[30][30]={NULL}; char Postfix_expression[30][30]={NULL}; /*************************************************************************// *************************************************************************/// ------------------------ Funcion Prototypes ------------------------- ///************************************************************************* //*************************************************************************/ void push(constchar *); void convert_ie_to_pe(constchar *); constchar* pop( ); constlongdouble evaluate_postfix_expression(constlongdouble); void show_screen( ); void clear_screen( ); void get_input( ); void apply_simpsons_rule( ); void show_result( ); /*************************************************************************// *************************************************************************/// ------------------------------ main( ) ------------------------------ ///************************************************************************* //*************************************************************************/ int main( ) { clrscr( ); textmode(C4350); show_screen( ); get_input( ); apply_simpsons_rule( ); show_result( ); getch( ); return 0; } /*************************************************************************//
  • 31. *************************************************************************/// ------------------------ Funcion Definitions ------------------------ ///************************************************************************* //*************************************************************************/ /*************************************************************************// /-------------------------- show_screen( ) --------------------------- ///************************************************************************* /void show_screen( ) { cprintf("n****************************************************************** **************"); cprintf("***************************- - **************************"); cprintf("*--------------------------- "); textbackground(1); cprintf(" Numerical Integration "); textbackground(8); cprintf(" --------------------------*"); cprintf("*-*************************- - ************************-*"); cprintf("*- *************************************************************************** *-*"); for(int count=0;count<42;count++) cprintf("*-* *-*"); gotoxy(1,46); cprintf("*- *************************************************************************** *-*"); cprintf("*------------------------------------------------------------------------------*"); cprintf("******************************************************************** ************"); gotoxy(1,2); } /*************************************************************************// /------------------------- clear_screen( ) --------------------------- ///************************************************************************* /void clear_screen( ) { for(int count=0;count<37;count++) {
  • 32. gotoxy(5,8+count); cout<<" "; } gotoxy(1,2); } /*************************************************************************// /-------------------------- push(const char*) ------------------------ ///************************************************************************* /void push(constchar* Operand) { if(top==(max_size-1)) { cout<<"Error : Stack is full."<<endl; cout<<"n Press any key to exit."; getch( ); exit(0); } else { top++; strcpy(Stack[top],Operand); } } /*************************************************************************// /------------------------------ pop( ) ------------------------------- ///************************************************************************* /constchar* pop( ) { char Operand[40]={NULL}; if(top==-1) { cout<<"Error : Stack is empty."<<endl; cout<<"n Press any key to exit."; getch( ); exit(0); } else { strcpy(Operand,Stack[top]); strset(Stack[top],NULL);
  • 33. top--; } return Operand; } /*************************************************************************// /-------------------- convert_ie_to_pe(const char*) ------------------ ///************************************************************************* /void convert_ie_to_pe(constchar* Expression) { char Infix_expression[100]={NULL}; char Symbol_scanned[30]={NULL}; push("("); strcpy(Infix_expression,Expression); strcat(Infix_expression,"+0)"); int flag=0; int count_1=0; int count_2=0; int equation_length=strlen(Infix_expression); if(Infix_expression[0]=='(') flag=1; do { strset(Symbol_scanned,NULL); if(flag==0) { int count_3=0; do { Symbol_scanned[count_3]=Infix_expression[count_1]; count_1++; count_3++; } while(count_1<=equation_length && Infix_expression[count_1]!='(' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!=')');
  • 34. flag=1; } elseif(flag==1) { Symbol_scanned[0]=Infix_expression[count_1]; count_1++; if(Infix_expression[count_1]!='(' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!=')') flag=0; if(Infix_expression[count_1-1]=='(' && (Infix_expression[count_1]=='-' || Infix_expression[count_1]=='+')) flag=0; } if(strcmp(Symbol_scanned,"(")==0) push("("); elseif(strcmp(Symbol_scanned,")")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } pop( ); } elseif(strcmp(Symbol_scanned,"^")==0 || strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0) { if(strcmp(Symbol_scanned,"^")==0) { }
  • 35. elseif(strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0) { while(strcmp(Stack[top],"^")==0 || strcmp(Stack[top],"*")==0 || strcmp(Stack[top],"/")==0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } elseif(strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } push(Symbol_scanned); } else { strcat(Postfix_expression[count_2],Symbol_scanned); count_2++; } } while(strcmp(Stack[top],NULL)!=0); strcat(Postfix_expression[count_2],"="); count_2++; } /*************************************************************************// /---------- evaluate_postfix_expression(const long double) ----------- ///************************************************************************* /constlongdouble evaluate_postfix_expression(constlongdouble x) { longdouble function_value=0; int count_1=-1;
  • 36. char Symbol_scanned[30]={NULL}; do { count_1++; strcpy(Symbol_scanned,Postfix_expression[count_1]); if(strcmp(Symbol_scanned,"^")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0 || strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { char Result[30]={NULL}; char Operand[2][30]={NULL}; strcpy(Operand[0],pop( )); strcpy(Operand[1],pop( )); longdouble operand[2]={0}; longdouble result=0; char *endptr; for(int count_2=0;count_2<2;count_2++) { int flag=0; if(Operand[count_2][0]=='-') { int length=strlen(Operand[count_2]); for(int count_3=0;count_3<(length-1);count_3++) Operand[count_2][count_3]=Operand[count_2][(count_3+1)]; Operand[count_2][count_3]=NULL; flag=1; } if(strcmp(Operand[count_2],"x")==0) operand[count_2]=x; elseif(strcmp(Operand[count_2],"e")==0) operand[count_2]=2.718282; elseif(strcmp(Operand[count_2],"sinx")==0) operand[count_2]=sinl(x);
  • 37. elseif(strcmp(Operand[count_2],"cosx")==0) operand[count_2]=cosl(x); elseif(strcmp(Operand[count_2],"tanx")==0) operand[count_2]=tanl(x); elseif(strcmp(Operand[count_2],"lnx")==0) operand[count_2]=logl(x); elseif(strcmp(Operand[count_2],"logx")==0) operand[count_2]=log10l(x); else operand[count_2]=strtod(Operand[count_2],&endptr); if(flag) operand[count_2]*=-1; } switch(Symbol_scanned[0]) { case'^' : result=powl(operand[1],operand[0]); break; case'*' : result=operand[1]*operand[0]; break; case'/' : result=operand[1]/operand[0]; break; case'+' : result=operand[1]+operand[0]; break; case'-' : result=operand[1]-operand[0]; break; } gcvt(result,25,Result); push(Result); } elseif(strcmp(Symbol_scanned,"=")!=0) push(Symbol_scanned); } while(strcmp(Symbol_scanned,"=")!=0); char Function_value[30]={NULL}; char *endptr;
  • 38. strcpy(Function_value,pop( )); function_value=strtod(Function_value,&endptr); return function_value; } /*************************************************************************// /----------------------------- get_input( ) -------------------------- ///************************************************************************* /void get_input( ) { do { clear_screen( ); gotoxy(6,9); cout<<"Number of Sub-Intervals :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(20,13); cout<<"[ min. n = 2 | max. n = 12 ]"; gotoxy(6,12); cout<<"Enter the max. number of sub-intervals = n = "; cin>>n; if(n<2 || n>12) { gotoxy(12,25); cout<<"Error : Wrong Input. Press <Esc> to exit or any other key"; gotoxy(12,26); cout<<" to try again."; n=int(getche( )); if(n==27) exit(0); } } while(n<2 || n>12); gotoxy(6,16); cout<<"Enter the value of Lower limit = a = ";
  • 39. cin>>a; gotoxy(6,18); cout<<"Enter the value of Upper Limit = b = "; cin>>b; h=((b-a)/n); gotoxy(6,24); cout<<"Input Mode :"; gotoxy(6,25); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(8,28); cout<<"Press : "; gotoxy(10,30); cout<<"- 'Y' or <Enter> to enter function"; gotoxy(10,32); cout<<"- 'N' or <Any other key> to enter values of the function"; gotoxy(8,35); cout<<"Enter your choice : "; char Choice=NULL; Choice=getch( ); if(Choice=='y' || Choice=='Y' || int(Choice)==13) { choice=1; gotoxy(28,35); cout<<"Y"; } else { gotoxy(28,35); cout<<"N"; } gotoxy(25,43); cout<<"Press any key to continue..."; getch( );
  • 40. if(choice) { clear_screen( ); gotoxy(6,10); cout<<"Non-Linear Function :"; gotoxy(6,11); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(6,37); cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3"; gotoxy(6,40); cout<<"Available Operators : ^ (raised to power) , * , / , + , -"; gotoxy(6,42); cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,"; gotoxy(6,44); cout<<" n = any number"; gotoxy(6,14); cout<<"Enter the Function : f(x) = "; cin>>Fx; convert_ie_to_pe(Fx); } clear_screen( ); gotoxy(6,9); cout<<"Data Points & Values of Function :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(25,12); cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(25,13); cout<<"³ x ³ f(x) ³"; gotoxy(25,14); cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; gotoxy(25,15); cout<<"³ ³ ³";
  • 41. for(int count_1=0;count_1<=n;count_1++) { gotoxy(25,(wherey( )+1)); cout<<"³ ³ ³"; gotoxy(25,(wherey( )+1)); cout<<"³ ³ ³"; } gotoxy(25,(wherey( )+1)); cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; xn[0]=a; for(int count_2=0;count_2<n;count_2++) xn[(count_2+1)]=(xn[count_2]+h); gotoxy(25,16); for(int count_3=0;count_3<=n;count_3++) { gotoxy(27,wherey( )); cout<<xn[count_3]; if(choice) { fx[count_3]=evaluate_postfix_expression(xn[count_3]); gotoxy(43,wherey( )); cout<<fx[count_3]; } else { gotoxy(43,wherey( )); cin>>fx[count_3]; } if(choice) gotoxy(25,(wherey( )+2)); else gotoxy(25,(wherey( )+1)); } gotoxy(25,43); cout<<"Press any key to continue..."; getch( ); }
  • 42. /*************************************************************************// /---------------------- apply_simpsons_rule( ) ----------------------- ///************************************************************************* /void apply_simpsons_rule( ) { longdouble temp=0; estimated_value=(fx[0]+fx[n]); estimated_value*=h; estimated_value/=3; temp=0; for(int count_1=2;count_1<=(n-2);count_1+=2) temp+=fx[count_1]; temp*=(2*h); temp/=3; estimated_value+=temp; temp=0; for(int count_2=1;count_2<=(n-1);count_2+=2) temp+=fx[count_2]; temp*=(4*h); temp/=3; estimated_value+=temp; } /*************************************************************************// /----------------------------- show_result( ) ------------------------ ///************************************************************************* /void show_result( ) { clear_screen( ); gotoxy(6,9); cout<<"Simpson's 1/3 Rule :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(8,12); cout<<" bô";
  • 43. gotoxy(10,13); cout<<"³f(x)dx ÷ (h/3)(f0+fn) + (2h/3)[f2+f4+...+f(n-2)] +"; gotoxy(50,15); cout<<"(4h/3)[f1+f3+...+f(n-1)]"; gotoxy(8,14); cout<<" aõ"; gotoxy(6,17); cout<<" bô"; gotoxy(6,19); cout<<" aõ"; gotoxy(6,18); cout<<"Estimation of ³f(x)dx :"; gotoxy(6,20); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(8,23); cout<<"Estimated Integral Value = "; cout<<estimated_value; gotoxy(1,2); } 4. Program untuk memperkirakan nilai Integral dari fungsi di poin yang diberikan dari data yang diberikan menggunakan aturan trapesium di C ++ Programming # include <iostream.h> # include <stdlib.h> # include <string.h> # include <stdio.h> # include <conio.h> # include <math.h> constint max_size=13; int n=0; int top=-1; int choice=0; longdouble h=0; longdouble a=0; longdouble b=0; longdouble estimated_value=0;
  • 44. longdouble xn[max_size]={0}; longdouble fx[max_size]={0}; char Fx[100]={NULL}; char Stack[30][30]={NULL}; char Postfix_expression[30][30]={NULL}; /*************************************************************************// *************************************************************************/// ------------------------ Funcion Prototypes ------------------------- ///************************************************************************* //*************************************************************************/ void push(constchar *); void convert_ie_to_pe(constchar *); constchar* pop( ); constlongdouble evaluate_postfix_expression(constlongdouble); void show_screen( ); void clear_screen( ); void get_input( ); void apply_trapezoidal_rule( ); void show_result( ); /*************************************************************************// *************************************************************************/// ------------------------------ main( ) ------------------------------ ///************************************************************************* //*************************************************************************/ int main( ) { clrscr( ); textmode(C4350); show_screen( ); get_input( ); apply_trapezoidal_rule( ); show_result( ); getch( ); return 0; } /*************************************************************************// *************************************************************************/// ------------------------ Funcion Definitions ------------------------
  • 45. ///************************************************************************* //*************************************************************************/ /*************************************************************************// /-------------------------- show_screen( ) --------------------------- ///************************************************************************* /void show_screen( ) { cprintf("n****************************************************************** **************"); cprintf("***************************- - **************************"); cprintf("*--------------------------- "); textbackground(1); cprintf(" Numerical Integration "); textbackground(8); cprintf(" --------------------------*"); cprintf("*-*************************- - ************************-*"); cprintf("*- *************************************************************************** *-*"); for(int count=0;count<42;count++) cprintf("*-* *-*"); gotoxy(1,46); cprintf("*- *************************************************************************** *-*"); cprintf("*------------------------------------------------------------------------------*"); cprintf("******************************************************************** ************"); gotoxy(1,2); } /*************************************************************************// /------------------------- clear_screen( ) --------------------------- ///************************************************************************* /void clear_screen( ) { for(int count=0;count<37;count++) { gotoxy(5,8+count); cout<<" ";
  • 46. } gotoxy(1,2); } /*************************************************************************// /-------------------------- push(const char*) ------------------------ ///************************************************************************* /void push(constchar* Operand) { if(top==(max_size-1)) { cout<<"Error : Stack is full."<<endl; cout<<"n Press any key to exit."; getch( ); exit(0); } else { top++; strcpy(Stack[top],Operand); } } /*************************************************************************// /------------------------------ pop( ) ------------------------------- ///************************************************************************* /constchar* pop( ) { char Operand[40]={NULL}; if(top==-1) { cout<<"Error : Stack is empty."<<endl; cout<<"n Press any key to exit."; getch( ); exit(0); } else { strcpy(Operand,Stack[top]); strset(Stack[top],NULL); top--; }
  • 47. return Operand; } /*************************************************************************// /-------------------- convert_ie_to_pe(const char*) ------------------ ///************************************************************************* /void convert_ie_to_pe(constchar* Expression) { char Infix_expression[100]={NULL}; char Symbol_scanned[30]={NULL}; push("("); strcpy(Infix_expression,Expression); strcat(Infix_expression,"+0)"); int flag=0; int count_1=0; int count_2=0; int equation_length=strlen(Infix_expression); if(Infix_expression[0]=='(') flag=1; do { strset(Symbol_scanned,NULL); if(flag==0) { int count_3=0; do { Symbol_scanned[count_3]=Infix_expression[count_1]; count_1++; count_3++; } while(count_1<=equation_length && Infix_expression[count_1]!='(' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!=')');
  • 48. flag=1; } elseif(flag==1) { Symbol_scanned[0]=Infix_expression[count_1]; count_1++; if(Infix_expression[count_1]!='(' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!=')') flag=0; if(Infix_expression[count_1-1]=='(' && (Infix_expression[count_1]=='-' || Infix_expression[count_1]=='+')) flag=0; } if(strcmp(Symbol_scanned,"(")==0) push("("); elseif(strcmp(Symbol_scanned,")")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } pop( ); } elseif(strcmp(Symbol_scanned,"^")==0 || strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0) { if(strcmp(Symbol_scanned,"^")==0) { } elseif(strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0)
  • 49. { while(strcmp(Stack[top],"^")==0 || strcmp(Stack[top],"*")==0 || strcmp(Stack[top],"/")==0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } elseif(strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } push(Symbol_scanned); } else { strcat(Postfix_expression[count_2],Symbol_scanned); count_2++; } } while(strcmp(Stack[top],NULL)!=0); strcat(Postfix_expression[count_2],"="); count_2++; } /*************************************************************************// /---------- evaluate_postfix_expression(const long double) ----------- ///************************************************************************* /constlongdouble evaluate_postfix_expression(constlongdouble x) { longdouble function_value=0; int count_1=-1; char Symbol_scanned[30]={NULL};
  • 50. do { count_1++; strcpy(Symbol_scanned,Postfix_expression[count_1]); if(strcmp(Symbol_scanned,"^")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0 || strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { char Result[30]={NULL}; char Operand[2][30]={NULL}; strcpy(Operand[0],pop( )); strcpy(Operand[1],pop( )); longdouble operand[2]={0}; longdouble result=0; char *endptr; for(int count_2=0;count_2<2;count_2++) { int flag=0; if(Operand[count_2][0]=='-') { int length=strlen(Operand[count_2]); for(int count_3=0;count_3<(length-1);count_3++) Operand[count_2][count_3]=Operand[count_2][(count_3+1)]; Operand[count_2][count_3]=NULL; flag=1; } if(strcmp(Operand[count_2],"x")==0) operand[count_2]=x; elseif(strcmp(Operand[count_2],"e")==0) operand[count_2]=2.718282; elseif(strcmp(Operand[count_2],"sinx")==0) operand[count_2]=sinl(x); elseif(strcmp(Operand[count_2],"cosx")==0)
  • 51. operand[count_2]=cosl(x); elseif(strcmp(Operand[count_2],"tanx")==0) operand[count_2]=tanl(x); elseif(strcmp(Operand[count_2],"lnx")==0) operand[count_2]=logl(x); elseif(strcmp(Operand[count_2],"logx")==0) operand[count_2]=log10l(x); else operand[count_2]=strtod(Operand[count_2],&endptr); if(flag) operand[count_2]*=-1; } switch(Symbol_scanned[0]) { case'^' : result=powl(operand[1],operand[0]); break; case'*' : result=operand[1]*operand[0]; break; case'/' : result=operand[1]/operand[0]; break; case'+' : result=operand[1]+operand[0]; break; case'-' : result=operand[1]-operand[0]; break; } gcvt(result,25,Result); push(Result); } elseif(strcmp(Symbol_scanned,"=")!=0) push(Symbol_scanned); } while(strcmp(Symbol_scanned,"=")!=0); char Function_value[30]={NULL}; char *endptr; strcpy(Function_value,pop( ));
  • 52. function_value=strtod(Function_value,&endptr); return function_value; } /*************************************************************************// /----------------------------- get_input( ) -------------------------- ///************************************************************************* /void get_input( ) { do { clear_screen( ); gotoxy(6,9); cout<<"Number of Sub-Intervals :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(20,13); cout<<"[ min. n = 3 | max. n = 12 ]"; gotoxy(6,12); cout<<"Enter the max. number of sub-intervals = n = "; cin>>n; if(n<3 || n>12) { gotoxy(12,25); cout<<"Error : Wrong Input. Press <Esc> to exit or any other key"; gotoxy(12,26); cout<<" to try again."; n=int(getche( )); if(n==27) exit(0); } } while(n<3 || n>12); gotoxy(6,16); cout<<"Enter the value of Lower limit = a = "; cin>>a;
  • 53. gotoxy(6,18); cout<<"Enter the value of Upper Limit = b = "; cin>>b; h=((b-a)/n); gotoxy(6,24); cout<<"Input Mode :"; gotoxy(6,25); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(8,28); cout<<"Press : "; gotoxy(10,30); cout<<"- 'Y' or <Enter> to enter function"; gotoxy(10,32); cout<<"- 'N' or <Any other key> to enter values of the function"; gotoxy(8,35); cout<<"Enter your choice : "; char Choice=NULL; Choice=getch( ); if(Choice=='y' || Choice=='Y' || int(Choice)==13) { choice=1; gotoxy(28,35); cout<<"Y"; } else { gotoxy(28,35); cout<<"N"; } gotoxy(25,43); cout<<"Press any key to continue..."; getch( ); if(choice) {
  • 54. clear_screen( ); gotoxy(6,10); cout<<"Non-Linear Function :"; gotoxy(6,11); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(6,37); cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3"; gotoxy(6,40); cout<<"Available Operators : ^ (raised to power) , * , / , + , -"; gotoxy(6,42); cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,"; gotoxy(6,44); cout<<" n = any number"; gotoxy(6,14); cout<<"Enter the Function : f(x) = "; cin>>Fx; convert_ie_to_pe(Fx); } clear_screen( ); gotoxy(6,9); cout<<"Data Points & Values of Function :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(25,12); cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(25,13); cout<<"³ x ³ f(x) ³"; gotoxy(25,14); cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; gotoxy(25,15); cout<<"³ ³ ³"; for(int count_1=0;count_1<=n;count_1++) {
  • 55. gotoxy(25,(wherey( )+1)); cout<<"³ ³ ³"; gotoxy(25,(wherey( )+1)); cout<<"³ ³ ³"; } gotoxy(25,(wherey( )+1)); cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; xn[0]=a; for(int count_2=0;count_2<n;count_2++) xn[(count_2+1)]=(xn[count_2]+h); gotoxy(25,16); for(int count_3=0;count_3<=n;count_3++) { gotoxy(27,wherey( )); cout<<xn[count_3]; if(choice) { fx[count_3]=evaluate_postfix_expression(xn[count_3]); gotoxy(43,wherey( )); cout<<fx[count_3]; } else { gotoxy(43,wherey( )); cin>>fx[count_3]; } if(choice) gotoxy(25,(wherey( )+2)); else gotoxy(25,(wherey( )+1)); } gotoxy(25,43); cout<<"Press any key to continue..."; getch( ); }
  • 56. /*************************************************************************// /---------------------- apply_trapezoidal_rule( ) -------------------- ///************************************************************************* /void apply_trapezoidal_rule( ) { for(int count=1;count<n;count++) estimated_value+=fx[count]; estimated_value*=2; estimated_value+=fx[0]; estimated_value+=fx[n]; estimated_value*=(h/2); } /*************************************************************************// /--------------------------- show_result( ) -------------------------- ///************************************************************************* /void show_result( ) { clear_screen( ); gotoxy(6,9); cout<<"Trapeziodal Rule :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(8,12); cout<<" bô"; gotoxy(10,13); cout<<"³f(x)dx ÷ (h/2)[f0+{2*(f1+f2+...+fn-1)}+fn]"; gotoxy(8,14); cout<<" aõ"; gotoxy(6,17); cout<<" bô"; gotoxy(6,19); cout<<" aõ"; gotoxy(6,18); cout<<"Estimation of ³f(x)dx :"; gotoxy(6,20); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
  • 57. gotoxy(8,23); cout<<"Estimated Integral Value = "; cout<<estimated_value; gotoxy(1,2); } 5. Program untuk memperkirakan nilai derivatif pertama dari fungsi pada titik-titik tertentu dari data yang diberikan menggunakan Tengah Perbedaan Formula di C ++ Programming # include <iostream.h> # include <stdlib.h> # include <string.h> # include <stdio.h> # include <conio.h> # include <math.h> constint max_size=13; int n=0; int top=-1; int choice=0; longdouble h=0; longdouble x0=0; longdouble xn[max_size]={0}; longdouble fx[max_size]={0}; char Fx[100]={NULL}; char Dfx[100]={NULL}; char Stack[30][30]={NULL}; char Postfix_expression[2][30][30]={NULL}; void push(constchar *); void convert_ie_to_pe(constchar *,constint); constchar* pop( ); constlongdouble evaluate_postfix_expression(constlongdouble,constint); void show_screen( ); void clear_screen( ); void get_input( ); void estimate_dfx( ); constint get_index(constlongdouble); int main( ) { clrscr( );
  • 58. textmode(C4350); show_screen( ); get_input( ); estimate_dfx( ); return 0; } /*************************************************************************// /-------------------------- show_screen( ) --------------------------- ///************************************************************************* /void show_screen( ) { cprintf("n****************************************************************** **************"); cprintf("*************************- - ************************"); cprintf("*------------------------- "); textbackground(1); cprintf(" Numerical Differentiation "); textbackground(8); cprintf(" ------------------------*"); cprintf("*-***********************- -**********************- *"); cprintf("*- *************************************************************************** *-*"); for(int count=0;count<42;count++) cprintf("*-* *-*"); gotoxy(1,46); cprintf("*- *************************************************************************** *-*"); cprintf("*------------------------------------------------------------------------------*"); cprintf("******************************************************************** ************"); gotoxy(1,2); }
  • 59. /*************************************************************************// /------------------------- clear_screen( ) --------------------------- ///************************************************************************* /void clear_screen( ) { for(int count=0;count<37;count++) { gotoxy(5,8+count); cout<<" "; } gotoxy(1,2); } /*************************************************************************// /-------------------------- push(const char*) ------------------------ ///************************************************************************* /void push(constchar* Operand) { if(top==(max_size-1)) { cout<<"Error : Stack is full."<<endl; cout<<"n Press any key to exit."; getch( ); exit(0); } else { top++; strcpy(Stack[top],Operand); } } /*************************************************************************// /------------------------------ pop( ) ------------------------------- ///************************************************************************* /constchar* pop( ) { char Operand[40]={NULL}; if(top==-1) { cout<<"Error : Stack is empty."<<endl; cout<<"n Press any key to exit.";
  • 60. getch( ); exit(0); } else { strcpy(Operand,Stack[top]); strset(Stack[top],NULL); top--; } return Operand; } /*************************************************************************// /---------------- convert_ie_to_pe(const char*,const int) ------------ ///************************************************************************* /void convert_ie_to_pe(constchar* Expression,constint index) { char Infix_expression[100]={NULL}; char Symbol_scanned[30]={NULL}; push("("); strcpy(Infix_expression,Expression); strcat(Infix_expression,"+0)"); int flag=0; int count_1=0; int count_2=0; int equation_length=strlen(Infix_expression); if(Infix_expression[0]=='(') flag=1; do { strset(Symbol_scanned,NULL); if(flag==0) { int count_3=0; do { Symbol_scanned[count_3]=Infix_expression[count_1]; count_1++; count_3++; }
  • 61. while(count_1<=equation_length && Infix_expression[count_1]!='(' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!=')'); flag=1; } elseif(flag==1) { Symbol_scanned[0]=Infix_expression[count_1]; count_1++; if(Infix_expression[count_1]!='(' && Infix_expression[count_1]!='^' && Infix_expression[count_1]!='*' && Infix_expression[count_1]!='/' && Infix_expression[count_1]!='+' && Infix_expression[count_1]!='-' && Infix_expression[count_1]!=')') flag=0; if(Infix_expression[count_1-1]=='(' && (Infix_expression[count_1]=='-' || Infix_expression[count_1]=='+')) flag=0; } if(strcmp(Symbol_scanned,"(")==0) push("("); elseif(strcmp(Symbol_scanned,")")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[index][count_2],pop( )); count_2++; } pop( ); } elseif(strcmp(Symbol_scanned,"^")==0 ||
  • 62. strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0) { if(strcmp(Symbol_scanned,"^")==0) { } elseif(strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0) { while(strcmp(Stack[top],"^")==0 || strcmp(Stack[top],"*")==0 || strcmp(Stack[top],"/")==0) { strcpy(Postfix_expression[index][count_2],pop( )); count_2++; } } elseif(strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { while(strcmp(Stack[top],"(")!=0) { strcpy(Postfix_expression[index][count_2],pop( )); count_2++; } } push(Symbol_scanned); } else { strcat(Postfix_expression[index][count_2],Symbol_scanned); count_2++; } } while(strcmp(Stack[top],NULL)!=0); strcat(Postfix_expression[index][count_2],"="); count_2++; } /*************************************************************************//
  • 63. /----- evaluate_postfix_expression(const long double,const int) ------ ///************************************************************************* /constlongdouble evaluate_postfix_expression( constlongdouble x,constint index) { longdouble function_value=0; int count_1=-1; char Symbol_scanned[30]={NULL}; do { count_1++; strcpy(Symbol_scanned,Postfix_expression[index][count_1]); if(strcmp(Symbol_scanned,"^")==0 || strcmp(Symbol_scanned,"*")==0 || strcmp(Symbol_scanned,"/")==0 || strcmp(Symbol_scanned,"+")==0 || strcmp(Symbol_scanned,"-")==0) { char Result[30]={NULL}; char Operand[2][30]={NULL}; strcpy(Operand[0],pop( )); strcpy(Operand[1],pop( )); longdouble operand[2]={0}; longdouble result=0; char *endptr; for(int count_2=0;count_2<2;count_2++) { int flag=0; if(Operand[count_2][0]=='-') { int length=strlen(Operand[count_2]); for(int count_3=0;count_3<(length-1);count_3++) Operand[count_2][count_3]=Operand[count_2][(count_3+1)]; Operand[count_2][count_3]=NULL; flag=1; }
  • 64. if(strcmp(Operand[count_2],"x")==0) operand[count_2]=x; elseif(strcmp(Operand[count_2],"e")==0) operand[count_2]=2.718282; elseif(strcmp(Operand[count_2],"sinx")==0) operand[count_2]=sinl(x); elseif(strcmp(Operand[count_2],"cosx")==0) operand[count_2]=cosl(x); elseif(strcmp(Operand[count_2],"tanx")==0) operand[count_2]=tanl(x); elseif(strcmp(Operand[count_2],"lnx")==0) operand[count_2]=logl(x); elseif(strcmp(Operand[count_2],"logx")==0) operand[count_2]=log10l(x); else operand[count_2]=strtod(Operand[count_2],&endptr); if(flag) operand[count_2]*=-1; } switch(Symbol_scanned[0]) { case'^' : result=powl(operand[1],operand[0]); break; case'*' : result=operand[1]*operand[0]; break; case'/' : result=operand[1]/operand[0]; break; case'+' : result=operand[1]+operand[0]; break; case'-' : result=operand[1]-operand[0]; break; } gcvt(result,25,Result); push(Result);
  • 65. } elseif(strcmp(Symbol_scanned,"=")!=0) push(Symbol_scanned); } while(strcmp(Symbol_scanned,"=")!=0); char Function_value[30]={NULL}; char *endptr; strcpy(Function_value,pop( )); function_value=strtod(Function_value,&endptr); return function_value; } /*************************************************************************// /----------------------------- get_input( ) -------------------------- ///************************************************************************* /void get_input( ) { do { clear_screen( ); gotoxy(6,9); cout<<"Number of Distinct Data Points :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(27,13); cout<<"[ min. n = 3 | max. n = 12 ]"; gotoxy(6,12); cout<<"Enter the max. number of distinct data points = n = "; cin>>n; if(n<3 || n>12) { gotoxy(12,25); cout<<"Error : Wrong Input. Press <Esc> to exit or any other key"; gotoxy(12,26); cout<<" to try again."; n=int(getche( ));
  • 66. if(n==27) exit(0); } } while(n<3 || n>12); gotoxy(6,16); cout<<"Enter the value of x0 = "; cin>>x0; gotoxy(6,18); cout<<"Enter the value of h = "; cin>>h; gotoxy(6,24); cout<<"Input Mode :"; gotoxy(6,25); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(8,28); cout<<"Press : "; gotoxy(10,30); cout<<"- 'Y' or <Enter> to enter function"; gotoxy(10,32); cout<<"- 'N' or <Any other key> to enter values of the function"; gotoxy(8,35); cout<<"Enter your choice : "; char Choice=NULL; Choice=getch( ); if(Choice=='y' || Choice=='Y' || int(Choice)==13) { choice=1; gotoxy(28,35); cout<<"Y"; } else { gotoxy(28,35); cout<<"N";
  • 67. } gotoxy(25,43); cout<<"Press any key to continue..."; getch( ); if(choice) { clear_screen( ); gotoxy(6,11); cout<<"Non-Linear Function :"; gotoxy(6,12); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(6,37); cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3"; gotoxy(6,40); cout<<"Available Operators : ^ (raised to power) , * , / , + , -"; gotoxy(6,42); cout<<"Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,"; gotoxy(6,44); cout<<" n = any number"; gotoxy(6,14); cout<<"Enter the Function : f(x) = "; cin>>Fx; gotoxy(6,17); cout<<"Enter the Differential Function : f'(x) = "; cin>>Dfx; convert_ie_to_pe(Fx,0); convert_ie_to_pe(Dfx,1); } clear_screen( ); gotoxy(6,9); cout<<"Data Points & Values of Function :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
  • 68. gotoxy(25,12); cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(25,13); cout<<"³ x ³ f(x) ³"; gotoxy(25,14); cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; gotoxy(25,15); cout<<"³ ³ ³"; for(int count_1=0;count_1<n;count_1++) { gotoxy(25,(wherey( )+1)); cout<<"³ ³ ³"; gotoxy(25,(wherey( )+1)); cout<<"³ ³ ³"; } gotoxy(25,(wherey( )+1)); cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; xn[0]=x0; for(int count_2=0;count_2<(n-1);count_2++) xn[(count_2+1)]=(xn[count_2]+h); gotoxy(25,16); for(int count_3=0;count_3<n;count_3++) { gotoxy(27,wherey( )); cout<<xn[count_3]; if(choice) { fx[count_3]=evaluate_postfix_expression(xn[count_3],0); gotoxy(43,wherey( )); cout<<fx[count_3]; } else { gotoxy(43,wherey( )); cin>>fx[count_3]; }
  • 69. if(choice) gotoxy(25,(wherey( )+2)); else gotoxy(25,(wherey( )+1)); } gotoxy(25,43); cout<<"Press any key to continue..."; getch( ); } /*************************************************************************// /------------------- get_index(const long double) -------------------- ///************************************************************************* /constint get_index(constlongdouble x) { for(int count=0;count<n;count++) { if(xn[count]==x) break; } return count; } /*************************************************************************// /---------------------------- estimate_dfx( ) ------------------------ ///************************************************************************* /void estimate_dfx( ) { clear_screen( ); gotoxy(6,9); cout<<"Centeral Difference Formula of Order 2 :"; gotoxy(6,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; gotoxy(8,13); cout<<"f'(x) ÷ [f(x+h)-f(x-h)]/2h"; gotoxy(6,17); cout<<"Estimation of f'(x) :"; gotoxy(6,18);
  • 70. cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; char Choice=NULL; longdouble x=0; longdouble dfx=0; longdouble actual_dfx=0; int error_flag=0; do { Choice=NULL; x=0; dfx=0; actual_dfx=0; error_flag=0; gotoxy(10,20); cout<<"Enter the value of x = "; cin>>x; if(x<=xn[0] || x>=xn[(n-1)]) { error_flag=1; gotoxy(10,23); cout<<"Error: Please enter x greater than x(0) and less than x(n)."; } else { int index=0; index=get_index(x); longdouble fxph=fx[(index+1)]; longdouble fxmh=fx[(index-1)]; dfx=((fxph-fxmh)/(2*h)); gotoxy(10,23); cout<<"The estimated value of f'("<<x<<") ÷ "<<dfx; } if(choice && !error_flag) { actual_dfx=evaluate_postfix_expression(x,1);
  • 71. gotoxy(10,25); cout<<"The Actual value of f'("<<x<<") = "<<actual_dfx; gotoxy(10,28); cout<<"Absolute Error = E(abs) = "<<fabs((actual_dfx-dfx)); } gotoxy(15,42); cout<<"Press <Esc> to exit or any other key to continue..."; Choice=getch( ); if(int(Choice)!=27) { gotoxy(10,20); cout<<" "; gotoxy(10,23); cout<<" "; gotoxy(10,25); cout<<" "; gotoxy(10,28); cout<<" "; gotoxy(15,42); cout<<" "; } elseif(int(Choice)==27) exit(0); } while(1); } 6. Program untuk membaca Sistem Linear dari Persamaan, kemudian mengevaluasi dengan menggunakan Gauss-Seidel Metode Iteratif dan menampilkan hasil di C ++ Programming # include <iostream.h> # include <stdlib.h> # include <stdio.h> # include <conio.h> # include <math.h> constint max_size=5; int n=0; int iterations=0;
  • 72. char Diagonally_dominant=NULL; longdouble input[max_size][max_size]={0}; longdouble previous_output[max_size]={0}; longdouble output[max_size]={0}; void show_screen( ); void clear_screen( ); void get_size_of_linear_equations( ); void show_input(constint,constint); void get_input_linear_equation( ); void sort_system_of_linear_equations( ); void apply_guass_seidel_iterative_method( ); void show_result( ); int main( ) { clrscr( ); textmode(C4350); show_screen( ); get_size_of_linear_equations( ); get_input_linear_equation( ); if(Diagonally_dominant=='Y' || Diagonally_dominant=='y') sort_system_of_linear_equations( ); apply_guass_seidel_iterative_method( ); show_result( ); getch( ); return 0; } /*************************************************************************// /-------------------------- show_screen( ) --------------------------- ///************************************************************************* /void show_screen( ) { cprintf("n****************************************************************** **************"); cprintf("**********************- -**********************"); cprintf("*---------------------- "); textbackground(1); cprintf(" Guass-Seidel's Itrative Method "); textbackground(8);
  • 73. cprintf(" ----------------------*"); cprintf("**********************- -**********************"); cprintf("******************************************************************** ************"); for(int count=0;count<42;count++) cprintf("* *"); gotoxy(1,46); cprintf("******************************************************************** ************"); cprintf("*------------------------------------------------------------------------------*"); cprintf("******************************************************************** ************"); gotoxy(1,2); } /*************************************************************************// /------------------------- clear_screen( ) --------------------------- ///************************************************************************* /void clear_screen( ) { textbackground(8); for(int count=0;count<37;count++) { gotoxy(3,8+count); cout<<" "; } gotoxy(1,2); } /*************************************************************************// /------------------ get_size_of_linear_equations( ) ------------------ ///************************************************************************* /void get_size_of_linear_equations( ) { do { clear_screen( ); gotoxy(38,11);
  • 74. cout<<"[ Maximum size = 4 ]"; gotoxy(4,9); cout<<"Enter the size of the System of Linear Equations = n = "; cin>>n; if(n<=0 || n>max_size) { gotoxy(12,25); cout<<"Error : Wrong Input. Press <Esc> to exit or any other key"; gotoxy(12,26); cout<<" to try again."; n=int(getche( )); if(n==27) exit(0); } } while(n<=0 || n>max_size); gotoxy(4,15); cout<<"Enter the number of iterations that you want to perform = "; cin>>iterations; gotoxy(4,18); cout<<"Do you want to make the System Diagonally Dominant : (Y/N) ? : "; cin>>Diagonally_dominant; gotoxy(1,2); } /*************************************************************************// /------------------ show_input(const int,const int) ------------------ ///************************************************************************* /void show_input(constint x,constint y) { int counter=0; int number_of_inputs=((x*n)+y+x); for(int count_1=0;count_1<n;count_1++) { gotoxy(4,(17+(count_1*2))); cout<<" ";
  • 75. gotoxy(4,(17+(count_1*2))); for(int count_2=0;count_2<=n;count_2++) { if(counter==(number_of_inputs+1)) textbackground(12); else textbackground(8); if(count_2==n) gotoxy((wherex( )+5),(17+(count_1*2))); gotoxy(wherex( ),(17+(count_1*2))); cprintf(" "); if(count_2==n) gotoxy((wherex( )-5),(17+(count_1*2))); gotoxy((wherex( )-6),(17+(count_1*2))); if(counter<=number_of_inputs && counter!=-1) { if(count_2==n) { cout<<" = "; cout<<input[count_1][count_2]; } elseif(count_2==0) cout<<input[count_1][count_2]<<" x"<<(count_2+1); else cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1); } elseif(count_2==n) cout<<" = b"<<(count_1+1); else cout<<"a"<<(count_1+1)<<(count_2+1)<<" x"<<(count_2+1); if(count_2<(n-1) && input[count_1][(count_2+1)]>=0) cout<<" + "; elseif(count_2<(n-1)) cout<<" - "; counter++; }
  • 76. } } /*************************************************************************// /-------------------- get_input_linear_equation( ) ------------------- ///************************************************************************* /void get_input_linear_equation( ) { clear_screen( ); gotoxy(4,9); cout<<"Size of the System of Linear Equations = n = "<<n; gotoxy(4,13); cout<<"System of Linear Equations :"; gotoxy(4,14); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; show_input(0,-1); for(int count_1=0;count_1<n;count_1++) { for(int count_2=0;count_2<=n;count_2++) { gotoxy(4,35); cout<<" "; gotoxy(4,35); if(count_2<n) cout<<"Enter the value of a"<<(count_1+1)<<(count_2+1)<<" = "; else cout<<"Enter the value of b"<<(count_1+1)<<" = "; cin>>input[count_1][count_2]; show_input(count_1,count_2); } } } /*************************************************************************// /----------------- sort_system_of_linear_equations( ) ---------------- ///************************************************************************* /void sort_system_of_linear_equations( ) {
  • 77. for(int count_1=0;count_1<(n-1);count_1++) { for(int count_2=count_1;count_2<(n-1);count_2++) { for(int count_3=count_1;count_3<(n-1);count_3++) { longdouble temp[max_size]={0}; if(fabs(input[count_3][count_1])< fabs(input[(count_3+1)][count_1])) { for(int count_4=0;count_4<=n;count_4++) temp[count_4]=input[count_3][count_4]; for(int count_5=0;count_5<=n;count_5++) input[count_3][count_5]= input[(count_3+1)][count_5]; for(int count_6=0;count_6<=n;count_6++) input[(count_3+1)][count_6]=temp[count_6]; } } } } } /*************************************************************************// /--------------- apply_guass_seidel_iterative_method( ) -------------- ///************************************************************************* /void apply_guass_seidel_iterative_method( ) { clear_screen( ); gotoxy(4,10); cout<<"Solution :"; gotoxy(4,11); cout<<"ÍÍÍÍÍÍÍÍÍÍ"; if(n==1) { gotoxy(4,13); cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(4,14); cout<<"³ n ³ X1 ³"; gotoxy(4,15); cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";
  • 78. gotoxy(4,16); cout<<"³ ³ ³"; } elseif(n==2) { gotoxy(4,13); cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(4,14); cout<<"³ n ³ X1 ³ X2 ³"; gotoxy(4,15); cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; gotoxy(4,16); cout<<"³ ³ ³ ³"; } elseif(n==3) { gotoxy(4,13); cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄ¿"; gotoxy(4,14); cout<<"³ n ³ X1 ³ X2 ³ X3 ³"; gotoxy(4,15); cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄ´"; gotoxy(4,16); cout<<"³ ³ ³ ³ ³"; } elseif(n==4) { gotoxy(4,13); cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(4,14); cout<<"³ n ³ X1 ³ X2 ³ X3 ³ X4 ³"; gotoxy(4,15);
  • 79. cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; gotoxy(4,16); cout<<"³ ³ ³ ³ ³ ³"; } for(int count_1=0;count_1<n;count_1++) { longdouble temp[max_size]={0}; for(int count_2=0;count_2<=n;count_2++) { if(count_2!=count_1) temp[count_2]=(input[count_1][count_2]/input[count_1][count_1]); } for(int count_3=0;count_3<=n;count_3++) input[count_1][count_3]=temp[count_3]; } for(int count_4=0;count_4<n;count_4++) { for(int count_5=0;count_5<n;count_5++) input[count_4][count_5]*=-1; } int x_cord=4; int y_cord=17; for(int count_6=0;count_6<=iterations;count_6++) { if(n==1) { gotoxy(x_cord,y_cord); cout<<"³ ³ ³"; gotoxy(x_cord,(y_cord+1)); cout<<"³ ³ ³"; } elseif(n==2) { gotoxy(x_cord,y_cord); cout<<"³ ³ ³ ³"; gotoxy(x_cord,(y_cord+1)); cout<<"³ ³ ³ ³"; }
  • 80. elseif(n==3) { gotoxy(x_cord,y_cord); cout<<"³ ³ ³ ³ ³"; gotoxy(x_cord,(y_cord+1)); cout<<"³ ³ ³ ³ ³"; } elseif(n==4) { gotoxy(x_cord,y_cord); cout<<"³ ³ ³ ³ ³ ³"; gotoxy(x_cord,(y_cord+1)); cout<<"³ ³ ³ ³ ³ ³"; } gotoxy((x_cord+3),y_cord); cout<<count_6; gotoxy((x_cord+10),y_cord); cout<<output[0]; if(n>=2) { gotoxy((x_cord+26),y_cord); cout<<output[1]; } if(n>=3) { gotoxy((x_cord+42),y_cord); cout<<output[2]; } if(n>=4) { gotoxy((x_cord+58),y_cord); cout<<output[3]; } for(int count_7=0;count_7<n;count_7++) { output[count_7]=0; for(int count_8=0;count_8<n;count_8++) { if(count_8!=count_7)
  • 81. output[count_7]+=(input[count_7][count_8]*previous_output[count_8]); } output[count_7]+=input[count_7][count_8]; previous_output[count_7]=output[count_7]; } y_cord+=2; if((count_6%12)==0 && count_6<iterations && count_6>0) { y_cord=17; gotoxy(30,44); cout<<"Press any key to continue..."; getch( ); for(int count_9=1;count_9<25;count_9++) { gotoxy(3,(16+count_9)); cout<<" "; } } } if(n==1) { gotoxy(x_cord,y_cord); cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; } elseif(n==2) { gotoxy(x_cord,y_cord); cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; } elseif(n==3) { gotoxy(x_cord,y_cord); cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄÙ"; } elseif(n==4) { gotoxy(x_cord,y_cord);
  • 82. cout<<"ÀÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; } gotoxy(30,44); cout<<"Press any key to continue..."; getch( ); } /*************************************************************************// /----------------------------- show_result( ) ------------------------ ///************************************************************************* /void show_result( ) { clear_screen( ); gotoxy(4,9); cout<<"Gauss-Seidel System of Linear Equations :"; gotoxy(4,10); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; for(int count_1=0;count_1<n;count_1++) { gotoxy(4,(13+(count_1*3))); cout<<"x"<<(count_1+1); gotoxy((wherex( )-1),(wherey( )-1)); cout<<"(k+1)"; gotoxy((wherex( )-3),(wherey( )+1)); cout<<" = "; for(int count_2=0;count_2<=n;count_2++) { gotoxy(wherex( ),(13+(count_1*3))); if(count_2!=count_1 && count_2<n) { if(count_2!=0) cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1); else cout<<input[count_1][count_2]<<" x"<<(count_2+1); gotoxy((wherex( )-1),(wherey( )-1)); if(count_2<count_1)
  • 83. cout<<"(k+1)"; else cout<<"(k)"; gotoxy((wherex( )-1),(wherey( )+1)); if(input[count_1][(count_2+1)]>=0) cout<<" + "; else cout<<" - "; } elseif(count_2!=count_1) cout<<fabs(input[count_1][count_2]); } } if(Diagonally_dominant=='Y' || Diagonally_dominant=='y') { gotoxy(4,26); cout<<"Note: The given System of Linear Equations is solved by making it"; gotoxy(10,28); cout<<"Diagonally Dominant."; } gotoxy(4,31); cout<<"Result of Given System of Linear Equations :"; gotoxy(4,32); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; for(int count_3=0;count_3<n;count_3++) { gotoxy(8,(34+count_3+count_3)); cout<<"x"<<(count_3+1)<<" = "<<output[count_3]; } gotoxy(4,(36+count_3+count_3)); cout<<"* Number of Iterations for the above result = "<<iterations<<"."; gotoxy(1,2); } 7. Program untuk membaca Sistem Linear dari Persamaan, kemudian mengevaluasi dengan menggunakan Metode Itrative Jacobi dan menampilkan hasil di C ++ Programming # include <iostream.h>
  • 84. # include <stdlib.h> # include <stdio.h> # include <conio.h> # include <math.h> constint max_size=5; int n=0; int iterations=0; char Diagonally_dominant=NULL; longdouble input[max_size][max_size]={0}; longdouble previous_output[max_size]={0}; longdouble output[max_size]={0}; void show_screen( ); void clear_screen( ); void get_size_of_linear_equations( ); void show_input(constint,constint); void get_input_linear_equation( ); void sort_system_of_linear_equations( ); void apply_jacobi_iterative_method( ); void show_result( ); int main( ) { clrscr( ); textmode(C4350); show_screen( ); get_size_of_linear_equations( ); get_input_linear_equation( ); if(Diagonally_dominant=='Y' || Diagonally_dominant=='y') sort_system_of_linear_equations( ); apply_jacobi_iterative_method( ); show_result( ); getch( ); return 0; } /*************************************************************************// /-------------------------- show_screen( ) --------------------------- ///************************************************************************* /void show_screen( ) {
  • 85. cprintf("n****************************************************************** **************"); cprintf("*************************- - *************************"); cprintf("*------------------------- "); textbackground(1); cprintf(" Jacobi's Itrative Method "); textbackground(8); cprintf(" -------------------------*"); cprintf("************************- - *************************"); cprintf("******************************************************************** ************"); for(int count=0;count<42;count++) cprintf("* *"); gotoxy(1,46); cprintf("******************************************************************** ************"); cprintf("*------------------------------------------------------------------------------*"); cprintf("******************************************************************** ************"); gotoxy(1,2); } /*************************************************************************// /------------------------- clear_screen( ) --------------------------- ///************************************************************************* /void clear_screen( ) { textbackground(8); for(int count=0;count<37;count++) { gotoxy(3,8+count); cout<<" "; } gotoxy(1,2); }
  • 86. /*************************************************************************// /------------------ get_size_of_linear_equations( ) ------------------ ///************************************************************************* /void get_size_of_linear_equations( ) { do { clear_screen( ); gotoxy(38,11); cout<<"[ Maximum size = 4 ]"; gotoxy(4,9); cout<<"Enter the size of the System of Linear Equations = n = "; cin>>n; if(n<=0 || n>max_size) { gotoxy(12,25); cout<<"Error : Wrong Input. Press <Esc> to exit or any other key"; gotoxy(12,26); cout<<" to try again."; n=int(getche( )); if(n==27) exit(0); } } while(n<=0 || n>max_size); gotoxy(4,15); cout<<"Enter the number of iterations that you want to perform = "; cin>>iterations; gotoxy(4,18); cout<<"Do you want to make the System Diagonally Dominant : (Y/N) ? : "; cin>>Diagonally_dominant; gotoxy(1,2); } /*************************************************************************// /----------------- show_input(const int,const int) -------------------
  • 87. ///************************************************************************* /void show_input(constint x,constint y) { int counter=0; int number_of_inputs=((x*n)+y+x); for(int count_1=0;count_1<n;count_1++) { gotoxy(4,(17+(count_1*2))); cout<<" "; gotoxy(4,(17+(count_1*2))); for(int count_2=0;count_2<=n;count_2++) { if(counter==(number_of_inputs+1)) textbackground(12); else textbackground(8); if(count_2==n) gotoxy((wherex( )+5),(17+(count_1*2))); gotoxy(wherex( ),(17+(count_1*2))); cprintf(" "); if(count_2==n) gotoxy((wherex( )-5),(17+(count_1*2))); gotoxy((wherex( )-6),(17+(count_1*2))); if(counter<=number_of_inputs && counter!=-1) { if(count_2==n) { cout<<" = "; cout<<input[count_1][count_2]; } elseif(count_2==0) cout<<input[count_1][count_2]<<" x"<<(count_2+1); else cout<<fabs(input[count_1][count_2])<<" x"<<(count_2+1); } elseif(count_2==n) cout<<" = b"<<(count_1+1);
  • 88. else cout<<"a"<<(count_1+1)<<(count_2+1)<<" x"<<(count_2+1); if(count_2<(n-1) && input[count_1][(count_2+1)]>=0) cout<<" + "; elseif(count_2<(n-1)) cout<<" - "; counter++; } } } /*************************************************************************// /-------------------- get_input_linear_equation( ) ------------------- ///************************************************************************* /void get_input_linear_equation( ) { clear_screen( ); gotoxy(4,9); cout<<"Size of the System of Linear Equations = n = "<<n; gotoxy(4,13); cout<<"System of Linear Equations :"; gotoxy(4,14); cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"; show_input(0,-1); for(int count_1=0;count_1<n;count_1++) { for(int count_2=0;count_2<=n;count_2++) { gotoxy(4,35); cout<<" "; gotoxy(4,35); if(count_2<n) cout<<"Enter the value of a"<<(count_1+1)<<(count_2+1)<<" = "; else cout<<"Enter the value of b"<<(count_1+1)<<" = "; cin>>input[count_1][count_2];
  • 89. show_input(count_1,count_2); } } } /*************************************************************************// /----------------- sort_system_of_linear_equations( ) ---------------- ///************************************************************************* /void sort_system_of_linear_equations( ) { for(int count_1=0;count_1<(n-1);count_1++) { for(int count_2=count_1;count_2<(n-1);count_2++) { for(int count_3=count_1;count_3<(n-1);count_3++) { longdouble temp[max_size]={0}; if(fabs(input[count_3][count_1])< fabs(input[(count_3+1)][count_1])) { for(int count_4=0;count_4<=n;count_4++) temp[count_4]=input[count_3][count_4]; for(int count_5=0;count_5<=n;count_5++) input[count_3][count_5]= input[(count_3+1)][count_5]; for(int count_6=0;count_6<=n;count_6++) input[(count_3+1)][count_6]=temp[count_6]; } } } } } /*************************************************************************// /------------------ apply_jacobi_iterative_method( ) ----------------- ///************************************************************************* /void apply_jacobi_iterative_method( ) { clear_screen( ); gotoxy(4,10); cout<<"Solution :"; gotoxy(4,11); cout<<"ÍÍÍÍÍÍÍÍÍÍ";
  • 90. if(n==1) { gotoxy(4,13); cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(4,14); cout<<"³ n ³ X1 ³"; gotoxy(4,15); cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; gotoxy(4,16); cout<<"³ ³ ³"; } elseif(n==2) { gotoxy(4,13); cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(4,14); cout<<"³ n ³ X1 ³ X2 ³"; gotoxy(4,15); cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; gotoxy(4,16); cout<<"³ ³ ³ ³"; } elseif(n==3) { gotoxy(4,13); cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄ¿"; gotoxy(4,14); cout<<"³ n ³ X1 ³ X2 ³ X3 ³"; gotoxy(4,15); cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄ´"; gotoxy(4,16); cout<<"³ ³ ³ ³ ³"; }
  • 91. elseif(n==4) { gotoxy(4,13); cout<<"ÚÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(4,14); cout<<"³ n ³ X1 ³ X2 ³ X3 ³ X4 ³"; gotoxy(4,15); cout<<"ÃÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´"; gotoxy(4,16); cout<<"³ ³ ³ ³ ³ ³"; } for(int count_1=0;count_1<n;count_1++) { longdouble temp[max_size]={0}; for(int count_2=0;count_2<=n;count_2++) { if(count_2!=count_1) temp[count_2]=(input[count_1][count_2]/input[count_1][count_1]); } for(int count_3=0;count_3<=n;count_3++) input[count_1][count_3]=temp[count_3]; } for(int count_4=0;count_4<n;count_4++) { for(int count_5=0;count_5<n;count_5++) input[count_4][count_5]*=-1; } int x_cord=4; int y_cord=17; for(int count_6=0;count_6<=iterations;count_6++) { if(n==1) { gotoxy(x_cord,y_cord); cout<<"³ ³ ³"; gotoxy(x_cord,(y_cord+1));
  • 92. cout<<"³ ³ ³"; } elseif(n==2) { gotoxy(x_cord,y_cord); cout<<"³ ³ ³ ³"; gotoxy(x_cord,(y_cord+1)); cout<<"³ ³ ³ ³"; } elseif(n==3) { gotoxy(x_cord,y_cord); cout<<"³ ³ ³ ³ ³"; gotoxy(x_cord,(y_cord+1)); cout<<"³ ³ ³ ³ ³"; } elseif(n==4) { gotoxy(x_cord,y_cord); cout<<"³ ³ ³ ³ ³ ³"; gotoxy(x_cord,(y_cord+1)); cout<<"³ ³ ³ ³ ³ ³"; } gotoxy((x_cord+3),y_cord); cout<<count_6; gotoxy((x_cord+10),y_cord); cout<<output[0]; if(n>=2) { gotoxy((x_cord+26),y_cord); cout<<output[1]; } if(n>=3) { gotoxy((x_cord+42),y_cord); cout<<output[2]; } if(n>=4) {