## PIC 10A

(Sec. 3a-3d)

C++ Programming and Problem Solving

## Instructor: Ivo Dinov, Asst. Prof.Neurology, Mathematics, Statistics

 http://www.math.ucla.edu/~dinov/10a.3.01s/

Due Date:

# Wednesday, 8:00 PM, May 23, 2001

Please, submit your homework electronically using the "submit" system. Please try to follow the "good programming" conventions we discussed in class. Write modular code and include all of your functions in the same CPP file as your main.

• (HW_6_1.cpp/exe) A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following five-digit integers is a palindrome: {12321, 55555, 45554, 11611}. Write a program that reads in a five-digit integer and determines whether it is a palindrome. (Hint: Use the division and modulus operators to separate the number into its individual digits. E.g., the left-most 1 in 12321, could be extracted by: 12321/104 = 1, and the remaining 4 digits are obtained by: 12321%104 = 2321. This remainder is then processed in a similar fashion.)

Sample Run:
%> (Palindrome Test) Enter a five-digit integer:
%> 12321
%> This number is a Palindrome
%> Try another one (y/n)?
%> y
%> (Palindrome Test) Enter a five-digit integer:
%> 12341
%> This number is NOT a Palindrome
%> Try another one (y/n)?
%> n
%> Bye!

• (HW_6_2.cpp/exe) (Binary-to-decimal conversion) Collect from the user an integer containing only 0's and 1's (i.e., a binary integer) and print its decimal equivalent. (Hint: Use the modulus and division operators to pick off the binary number's digits one at a time from right to left. Just as in the decimal number system where the rightmost digit has a positional value of 1 and the next digit left has a positional value of 10, then 100, then 1000, etc., in the binary number system, the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, etc. (powers of 2, Why?) Thus, the decimal number 234 can be interpreted as 4*1 + 3*10 + 2*100. The decimal equivalent of the binary 1101 is: 1*1 + 0*2 + 1*4 + 1*8 = = 1 + 0 + 4 + 8 = 13. Note that you should be able to convert a binary number of any length less than 9 digits. Here is a bit more about interpreting binary numbers.)

Sample Run:
%> (Binary-to-decimal-Converter) Please enter a binary number (e.g., 10010):
%> 10010111
%> 10010111 (2) <--> 151 (10).
%> Would you like to convert another number from (2)-base --> (10)-base (y/n)?
%> y
%> 111 (2) <--> 7 (10).
%> Would you like to convert another number from (2)-base --> (10)-base (y/n)?
%> n
%> Goodbye!

\Ivo D. Dinov, Ph.D., Department of Neurology and Program in Computing, UCLA School of Medicine/