In the name of GOD
Deitel Exercise : Chapter 2
2.27. Modify the following code to produce the output shown. Use porper indetation techniques. You musn not make any changes other than inserting braces. The compiler ignores indentation in a C++ program. We eliminated the indentation from the following code to make the problem more challenging. [Note: It's possible that no modification is necessary.]
if ( y == 8 )
if ( x == 5 )
cout << "@@@@@" << endl;
else
cout << "#####" << endl;
cout << "$$$$$" << endl;
cout << "&&&&&" << endl;
a) Assuming x=5 and y=8, the following output is produced:
@@@@@
$$$$$
&&&&&
b) Assuming x=5 and y=8, the following output is produced:
@@@@@
c) Assuming x=5 and y=8, the following output is produced:
@@@@@
&&&&&
d) Assuming x=5 and y=7, the following output is produced. Note : The last three output statements after the ELSE are all part of a block.
#####
$$$$$
&&&&&
2.30. Input an integre containing only 0s and 1s (i.e. a "binary" integer) and print its decimal equivalent. Use the modulus and division operators to pick off the "binary" number's digits one at a time from right to left. Much like in the decimal number system, where the right most digit has a positional value of 1, the next digit left has a positional value of 10, then 100, then 1000, and so on, in the binary number system, the right most digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, and so on. Thus the decimal number 234 can be interpreted as 4*1 + 3*10 + 2*100. the decimal equivalent of binary 1101 is 1*1 + 0*2 + 1*4 + 1*8 or 1+0+4+8, or 13. [Note: The reader not familiar with binary numbers might wish to refer to Appendix C.] (In yeki ro sharmande!!! :D )
2.37. A company wants to trasmit data over the telephone, but is concerned that its phone could be trapped. All of the data are transmited as four-digit integers. The company has asked to write a progra mthat encryots the data so that it can be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by (the sum of that digit plus 7) modulus 10. Then, swap the first digit with the third, swap the second digit with the forth and print the encrypted integer.
Write a seperate program that inputs an encrypted four-digit integer and decrypts it form the original number.
2.54. Calculate the value of Pi from the infinite series
Pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...
Print a table that shows the value of Pi approximated by one term of this series, by two terms, by three terms, etc. How many terms of this series do you have to use before you first get :
3.14 ?
3.141?
3.1415?
3.14159?
2.55. (Pythagorean Triples) A right triangle can have sides that all are three integers. A set of three integer valuse for the side o fa triangle is called a pythagorean triple. These three sides must satisfy the relationship that the sum of the squars of two of th sides is equal to the squar of the hypotenuse. Find al pythagorean triples for side1, side2 and hypotenuse all no longer than 500. Use a triple-nested for-loop that tries all possibilities. This is an example of brute force computing. You will learn in more advanced computer-science courses that there are many interesting problems which there is no knonw algorithmic approach other than sheer brute force.
2.56. A company pays its employees as managers (who recieve a fixed weekly salary), hourly workers (who recieve a fxed hourly wages for up to the first 40 hours they work and "time-ane-a-half" - 1.5times their hourly wages - fo overtime hours worked), cimmission workers (who recieve 250$ plus 5.7% of their gross weekly sales), or pieceworkers (who recieve a fixed amount of money per item for each of he items they produce - each pieceworker in this company on only one type of item). Write a program to compute the weekly pay for each employee. You do not know the number of employees in advance. Each type of employee has its own pay code: managers have pay code 1, hourly workers have code 2, commission workers have code 3 and pieceworkers have code 4. Use a switch to compute each employee's paycode. Within the switch, prompt the user (i.e., the payroll clerk) to enter the appropriate facts your program needs to calculate each employee's ay according to that employee's paycode.