Saturday, December 17, 2011

How does this code work? - I'm new at C++?

//Includes the libary?


#include %26lt;iostream%26gt;





//Use standard function; so I wont have to write it all the time


using namespace::std;





//the "int a(int b)" how does that work?


int binary(int decimal)


{


//Make variable "reminder"


int remainder;





//And if the variable decimal is less or equal to 1, then run following function?


if(decimal %26lt;= 1)


{


//post decimal's value?


cout %26lt;%26lt; decimal;


//Return to the start and run it again?


return 0;


}





//I don't got this line at all


remainder = decimal%2;





//If decimal is greater then 1, then run this function?


binary(decimal %26gt;%26gt; 1);


//and write the remainder value


cout %26lt;%26lt; remainder;


}





//Start main process, isn't this suppost to be runned before anything els?


int main()


{


//Make variable "num"


int num;





//Ask the user for input


cout %26lt;%26lt; "Enter decimal: ";


//Set the input as "num"


cin %26gt;%26gt; num;


//Write "endl?", then "binary of", then the "num" you just inputted, and then "is"?


cout %26lt;%26lt; endl %26lt;%26lt; "Binary of " %26lt;%26lt; num %26lt;%26lt; " is ";


//then say the binary number?, but how was this found, and posted?


binary(num);


//Pause it


system("PAUSE");


}





Anyone able to help me understand the code?|||In addition to what the guys above have already said:





return 0; doesn't return to the start. It exits the function with the value of 0. Actually this return value is never used, so the declaration for your function should be "void binary(int decimal)" (ie returning nothing) and "return 0" should be just "return".





What the function binary() does is convert a decimal number (like 3) into its binary form (namely 101) and then prints it out.





How it works: first it checks whether the decimal number is %26lt;= 1 (ie if it's 0 or 1). If it is, then it's ALREADY binary and can be printed. Otherwise, the function prints the remainder of a division by 2 (the result is 1 for odd numbers, 0 for even numbers), then shifts the number right by one bit position (decimal %26gt;%26gt; 1) and calls binary() again on that shifted value.





Call stack of binary(3):





binary(3): is it %26lt;= 1? No. Print remainder: 3%2=1. Call binary(3 %26gt;%26gt; 1) = binary(2).


binary(2): is it %26lt;= 1? No. Print remainder: 2%2=0. Call binary(2 %26gt;%26gt; 1) = binary(1).


binary(1): is it %26lt;= 1? Yes. Print 1





Result printed: 101|||It's just a recursive function which displays an integer in binary format. You enter a decimal integer value in main and then it calls binary() recursively to print an appropriate string of 1s and 0s.|||The functions that interact with the hardware (and OS) directly aren't in the program, their in what's called a library. So firstly you need to tell the compiler (the program that makes your program) to


#include %26lt;iostream%26gt;





using namespace::std;


just means that you only have to write cout or cin, rather than std::cout or std::cin. don't worry about this for now.





int binary(int decimal) is the declaration of a function that will be used later in your program.


the first int means that the function will return an integer.


the word "binary" is just the name of the function.


in the brackets, you would put all the parameters your function would accept separated by a comma, in this case it accepts only one parameter, "decimal" of type int(eger).


the function is them between the {


and the }





I think you've got the rest of the function understood until here,


remainder = decimal%2;


That means calculate the modulus of decimal and 2 (e.g. the remainder of decimal when divided by 2 - see http://www.cprogramming.com/tutorial/mod鈥?/a> for more info).





I must be honest, I don't quite know what this does,


//If decimal is greater then 1, then run this function?


binary(decimal %26gt;%26gt; 1);


but if your sure that the code is correct, then s'ok.





endl is a new line character, and the


binary(num);


will print out the number in binary (techincally it should have returned the number as you defined it as int binary, but it might work this way).





if you need more help, well, dunno. email, google, pm, whatever.

No comments:

Post a Comment