|
| sl02ggp (27) | |||
| The assignment everyone has done before..lol...reversing digits. How do i make my code more simple (as my friend stated)? Trying to write a program that reverses the digits in an integer. Assuming that the max value of the integer is 99999. And using arrays to solve the problem. For example if integer: 0 was entered, output is 0 10 was entered, output is 1 12 was entered, output is 21 123 was entered, output is 321 7600 was entered, output is 67 8015 was entered, output is 5108 90000 was entered, output is 9
| |||
Last edited on | |||
| sl02ggp (27) | |
I found out that, I can't use printf but instead use couts. I'm in a beginner level's C++ class and my teacher also wants it more simpler..aha said i can't use scanf_s, pow, and strings!?!?! | |
| Disch (2152) | |
| Your teacher wants you to use cin and cout instead of printf and scanf. And they're right, you're making this way more complicated than it needs to be. Think of it this way... to move a digit to the left, you multiply by 10, right? and to move a digit to the right... you divide by 10. There's also the mod operator, which gives you the remainder of division. So basically x % 10; will give you the rightmost digit of x.You should only need 1 loop, 1 division, 1 mod, 1 multiply, and no other math. | |
| buffbill (197) | |
| As Disch says ........but also...... 1. Start with a 5 element array with all elements initializes to 0 (zero) 2. Using a for loop with condition set to i<5; enter up to 5 integers. (Pad with zeros to get to 5.) 3. Read them out backwards using i--;but if array[4]==0 don't print it. print "" 4. Test at each iteration as in 3. above. 5. However if array [i] >0 print array[i-1] even if == 0. If you need to make several manipulations put the whole lot in a while loop. I'm sure you will be able to improve on this... especially if I've missed something! | |
| Disch (2152) | |
| You don't need an array. Using a 5 element array limits you to 5 characters. It also makes you convert to a string (or a series of characters) which is wholly unnecessary. You can do this with 2 integers and a loop. We're talking 3 or 4 lines of code. | |
| buffbill (197) | ||
| @Ditch Yes the OP has stated MAX value to be handled is 99999 so 5 integers are involved.
No incorrect. I'd be interested to read your pseudo code as I can't quite visualize the process. | ||
| Disch (2152) | |||
| It's Disch with an 's'. Pronounced like "Dish". The extra C is for "cool". ;D I missed that about the max. And also I might have gotten this post mixed up with another one regarding unnecessary conversion to strings. My mistake! I can't really give pseudocode without giving it away completely. So I'll just give it away completely:
| |||
| buffbill (197) | |
| Sorry about my typo Disch Yes I can see why you include the 'c' The code is very cool ****************************** Enter a number using 5 digits max. 90720 rev:= 0 num:= 9072 rev:= 2 num:= 907 rev:= 27 num:= 90 rev:= 270 num:= 9 rev:= 2709 num:= 0 The reverse of this number is 2709 ****************************** I suppose its very obvious to you blokes but i really had to go through it with a pencil before i could get it. Many thanks | |
This topic is archived - New replies not allowed.
