QA 3
Q1: For 123, "three" gets printed, how do I print the rest after figuring the counter for the number of loops ? Q2: How do you reverse the order so it reads the number from left to right? ------------------------------------------------------------------------------- Q1: For 123, "three" gets printed, how do I print the rest after figuring the counter for the number of loops ? A1: Since you have the last digit 3 in the loop, keep going in that loop INCLUDING the switch/if else to print the digit "three". Group the switch/if else in the loop that extracts each digit. Now the next loop through will give you 2 -- print "two". ... ------------------------------------------------------------------------------- Q2: How do you reverse the order so it reads the number from left to right? A2: This has something to do with "count", the number of iterations for the loop to divide 123 by ten. For 123, count is 3. How do you get the "1" ? Oh divide by 100 (10*10 --> 10 to the 2 power) How do you get the "2" ? Oh divide by 10 (10 --> 10 to the 1 power) How do you get the "3" ? Oh divide by 1 (1 --> 10 to the 0 power) Notice the pattern, how many times you must divide by 10. For 1234, count is 4. How do you get the "1" ? Oh divide by 1000 (10*10*10 --> 10 to the 3 power) How do you get the "2" ? Oh divide by 100 (10*10 --> 10 to the 2 power) How do you get the "3" ? Oh divide by 10 (10 --> 10 to the 1 power) How do you get the "4" ? Oh divide by 1 (1 --> 10 to the 0 power) Think on this, hint another loop. ------------------------------------------------------------------------------- Q3: I have 500+ lines of uncommented code, how could I shorten it? A3: Your method is "brute force" AND unacceptable for programming, its like longhand addition of 10 + 10 + 10 + 10 +10... You need to step back and think this over in patterns. Use loops to repeat. Eg. 123 123 divide by 100 (10 * 10 OR 10 to 2 power) ---> 1 23 divide by 10 ( 10 OR 10 to 1 power) ---> 2 3 divide by 1 ( 1 OR 10 to 0 power) ---> 3 Eg. 1234 1234 divide by 1000 (10 * 10 * 10 OR 10 to 3 power) ---> 1 234 divide by 100 ( 10 * 10 OR 10 to 2 power) ---> 2 34 divide by 10 ( 10 OR 10 to 1 power) ---> 3 4 divide by 1 ( 1 OR 10 to 0 power) ---> 4 123 has 3 digits AND divided by ten 2 times --- > 1 23 has 2 digits AND divided by ten 1 time --- > 2 3 has 1 digits AND divided by ten 0 time --- > 3 Notice this is the same as the power and the repetition amount is one less than the count of digits. If you get the count of digits, we did this in class using a counter in the while loop like PA#2. Then with that count repeat dividing by 10. (another loop) -------------------------------------------------------------------------------