On this occasion I would like to clarify, the algorithm for computing the rank with the recursive method. as we know, if recursive function is not use looping in the flowchart, but the way it works is by calling the function itself. For more clearly, let's look at the example below!
This is flowchart for calculate the rank value : To make us use a recursive function.
in this flowchart, in addition there is the main function, there must be other functions.
in the example below I give the name of the function is a function "pangkat".
picture above is the main function.. then we must make "pangkat" function, this is the flowchart :
please try!
if you completed make your flowchart,, then we make program!
for this example I use dev C++
#include <iostream>
#include <conio.h>
using namespace std;
class rekrusif{
friend istream& operator>>(istream&,rekrusif&);
friend ostream& operator<<(ostream&,rekrusif&);
private :
int x,y,z;
public:
long pangkat(int x,int y);
int output();
};
istream& operator>>(istream& insert, rekrusif& e){
cout<<"menghitung nilai pangkat secara rekursi:"<<endl;
cout<<"masukkan nilai x : ";
insert>>e.x;
cout<<"masukan nilai y : ";
insert>>e.y;
return insert;
}
long rekrusif::pangkat(int x,int y)
{
if (y==1)
return(x);
else if
(y==0)
return (1);
else
return(x * pangkat(x,y-1));
}
int rekrusif::output(){
z=pangkat(x,y);
cout<<x<<" ^ "<<y<<" = "<<z<<endl<<endl;
return z;
}
int main(){
rekrusif exp;
cin>>exp;
exp.output();
system ("PAUSE");
return 0;
}
This is flowchart for calculate the rank value : To make us use a recursive function.
in this flowchart, in addition there is the main function, there must be other functions.
in the example below I give the name of the function is a function "pangkat".
picture above is the main function.. then we must make "pangkat" function, this is the flowchart :
please try!
if you completed make your flowchart,, then we make program!
for this example I use dev C++
#include <iostream>
#include <conio.h>
using namespace std;
class rekrusif{
friend istream& operator>>(istream&,rekrusif&);
friend ostream& operator<<(ostream&,rekrusif&);
private :
int x,y,z;
public:
long pangkat(int x,int y);
int output();
};
istream& operator>>(istream& insert, rekrusif& e){
cout<<"menghitung nilai pangkat secara rekursi:"<<endl;
cout<<"masukkan nilai x : ";
insert>>e.x;
cout<<"masukan nilai y : ";
insert>>e.y;
return insert;
}
long rekrusif::pangkat(int x,int y)
{
if (y==1)
return(x);
else if
(y==0)
return (1);
else
return(x * pangkat(x,y-1));
}
int rekrusif::output(){
z=pangkat(x,y);
cout<<x<<" ^ "<<y<<" = "<<z<<endl<<endl;
return z;
}
int main(){
rekrusif exp;
cin>>exp;
exp.output();
system ("PAUSE");
return 0;
}
Tidak ada komentar:
Posting Komentar