c++ algorithm

I had a problem in understanding the algorithm of this program.Can you tell how this program is working ??

void fungsi(int a, int &b,int &c)
{
int y=1;
a=a*b++;
c=b+y;
cout<<a<<b<<c<<y;
}

main(){
int a=2,b=3,c=3,y=2;
fungsi(a+b,c,y);
cout<<a<<b<<c<<y;
fungsi(b,a,c);
cout<<a<<b<<c<<y;
}
boring.
Go through the program step by step with a debugger or with pen and paper.
you may want to change the names of the variables in `main()' to make it easier.
I assume you speak Indonesian.

That function doesn't look quite right.

It changes the value of b and c in the function fungsi(int, int &, int &).

Changes b to: b + 1

Changes c to: (b + 1) + 1

So, if you passed the values 5, 10, 15 to fungsi(int, int &, int &) then this would happen:

1
2
3
4
5
5 ==> 5

10 ==> 11

15 ==> 12
Last edited on
can you write in paper how the program works ??
Hello ryard,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

I may get some of this wrong, but I have the right idea.

Starting in main:
int a=2,b=3,c=3,y=2;
This line defines the variables and initializes the variables. Always a good idea to initialize your variables even if it is just zero otherwise they will contain whatever value that was at that memory location when the program runs and that will cause problems when you try to use them.

Now in the line:
fungsi(a+b,c,y);
the formula a + b can not be sent to the function, so b is added to a and the result is sent to the function. The result is actually stored in a temporary variable that the compiler creates. This is a no name variable that the compiler uses when needed e.g., here, in a cout or in a return. Nothing you need to worry about, but nice to know.

To the function:
void fungsi(int a, int &b,int &c)
void means that nothing is returned. int a is being passed and received by value which means that you are only working with a copy of what was sent from main. int &b, int &c mean that the variables are being passed and received by reference which means that 'b' and 'c' are different names for the variables that came from main. It also has the advantage that any changes made in the function will be reflected in the variables in main.

http://www.cplusplus.com/articles/z6vU7k9E/
An explanation of pass by value and pass by reference.

The line:
a=a*b++;
means, using the order of precedence 'a' is multiplied by 'b' and the result is stored in a. Then the 'b++' means 1 is added to 'b', the post fix of the '++' means it is done after.

http://en.cppreference.com/w/cpp/language/operator_precedence

c=b+y; is self-explanatory.

The last line:
cout<<a<<b<<c<<y;
will print the variables to the screen. Notice the values that are print here compared to what is printed in main. That is due to the variables in the function loosing scope when the function ends.

http://www.cplusplus.com/doc/tutorial/namespaces/

Returning to main:
cout<<a<<b<<c<<y;
this will print your variables again. Notice what has changed and what has not. When the function ends all the variables in the function loose scope, so the first parameter that was passed by value will not show any changes.

The last two lines of main are similar to the two lines above them. Although you are sending the variables in a different order, that part is OK, but the final output will be different.

Hope that helps,

Andy
Topic archived. No new replies allowed.