اريد احد يشرح لى هذا الاكود وهذا السوال المطلوب من السوال
الكود الاول :
#include<iostream>
using namespace std;
int length(int);
int count=0;
int main()
{
int n;
cout<<" \tEnter a number ";//prompt message to enter an integer suppose the user enter 12345
cin>>n;
cout<<" \tThe length of "<< n <<" is "<<length(n)<<endl;
return 0;
} // End of main
int length(int num )
{
if (num==0)
return 0;
else
{
num/=10;
return 1+ length(num);
}
}
الكود الثانى :
#include <iostream>
using namespace std;
int main ()
{
int first_value = 5, second_value = 10;
int * p1, * p2;
p1 = &first_value;
p2 = &second_value;
*p1 = 10;
*p2 = *p1;
p1 = p2;
*p1 = 25;
cout << "first value is " << first_value << endl;
cout << "second value is " << second_value << endl;
cout << "value of p1 is " << *p1 << endl;
cout << "value of p2 is " << *p2<< endl;
return 0;
}
والسوال اللذى اوريد شرحه هو :
Write a C++ program that ask user to enter the number in decimal then convert it to the binary and vise versa .
Be sure that your program have the following :
1- Recursive function that convert from binary to decimal.
2- Validate function that check if the binary number entered by the user contain only 0s and 1s .
How Convert from binary to decimal :
To convert binary into decimal is very simple and can be done as shown below:
Say we want to convert the 4 bit value 1001 into a decimal value, we can use a formula like that below:
128 64 32 16 8 4 2 1
1 0 0 1
As you can see, we have placed the numbers 1, 2, 4, 8, 16, 32, 64, 128 (powers of two) in reverse numerical order, and then written the binary value below.
To convert, you simply take a value from the top row wherever there is a 1 below, and then add the values together.
For instance, in our example we would have 8 + 1 = 9 .
Because we know binary is base 2 then the above could be written as:
1*23 + 0*22 + 0*21 + 1*20 = 9 .