-
-
Save jaredonline/1231156 to your computer and use it in GitHub Desktop.
/* Here's what you've got */ | |
/* | |
Problem 2: You are asked to implement an automated number-to-language translation system for a phone com- pany. Write a C program that receives as an input an integer number and converts each digit of the number to an English word. The integer number can be of any length. | |
Sample program execution: | |
Please enter your phone number: 2345769854 two three four five seven six nine eight five four | |
*/ | |
#include<stdio.h> | |
int main(void) | |
{ | |
int numbs; | |
char ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN; | |
printf("Give a number to be converted: "); | |
scanf("%3d", &numbs); | |
switch (numbs) { | |
case 0: | |
printf("ZERO\n"); | |
break; | |
case 1: | |
printf("ONE\n"); | |
break; | |
} | |
return (0); | |
} | |
/* consider your what you're doing... in plain english: | |
Your program: "Hey user, give me a number" | |
Your user: "Okay, here's '12345'" | |
Your switch statement then says, "Does the number equal 1? Nope. Does it equal 2? Nope" etc. | |
You need to take the input and split it up into single digits first, then do the comparison. | |
*/ |
You could do that. I'm not very familiar with C. Have you guys learned about arrays yet? There's plenty of solutions... Do you know about the %
operator?
We haven't learned arrays yet, but we definitely do know if the % operator. It stands for the remainder, right? Not quite sure though
Right, so, along that line, consider the return value of
123 % 10
Have you learned about loops yet?
12.3 ...? Not quite sure how it works out, we've used them before in class and i know if you declare the numbers as integers, it has a much different outcome than if you declare it as a float. And as for loops, we've learned if/else loops, while loops, do-while loops, and for loops.
As far as integers are concerned (which is what we're dealing with), the %
operator says: "Take the number on the left, and divide it by the number on the right EVENLY. Then return the remainder".
So, what's the remainder of 123 / 10
?
OH! The remainder would be 3. Therefore letting me single out my last digit and therefore print that. This code requires that it can be a number of any length though, so how would I do it to continue this process? while (remainder > 0)?
Also, don't confuse control structures
and loops
.
A loop
is a type of control structure
: http://en.wikipedia.org/wiki/Control_structure#Choice
Yes! Exactly! So, now we know how to single out a single digit. We need to now figure out how to print all the digits.
Now, let's look at some loops. You named three, do/while
, while
, for
. Do you know the difference between the three?
well, while and do while are very similar. with a while loop, you state the condition; aka while (a != 0 ), then tell the program what to do, and as long as that condition is met it will loop forever. a do-while loop states do: command, and at the end has the while function to repeat if the condition is met. For loops, however, i'm much more iffy on.
Maybe I should back up a bit. Do you know why I want to use a loop? At a bit of a high level, do you know what they're good for?
Well, in terms of this problem we want to have a loop so that we can single out each of our "remainders." Otherwise, i can only single out my last integer one time , which doesn't help if i have anything longer than two digits.
I can't figure out exactly how to edit the above file, but this is what i've tried doing. I set a while loop above the switch, so i have printf(blah) scanf(blah) and below that, rem=numbs%10; while (rem !< 1) { and inside here i have my switch. Then at the end of all my cases, i have rem=rem%10. This has just given me infinite loops though, so i think i need to figure out the condition that needs to be met so it knows when to stop? (within the next 30 minutes! hahaha)
This is due at 10?
It is, but don't worry about it! Either way I'm learning more now than I could have hoped for, so we're all good :)
I'll give you a hint.
/*
num = 123
rem = 123 % 3; rem = 3
num = num / 10; num now equals 12
do
put your case statement here
while [ put something here that indicates when it should stop; think about what the return value for 1 / 10 is! ]
*/
Big hint given so you can turn it in on time [=
Although, there is a big flaw for this method of doing it, but we can work on that as a last step.
Damn! I didn't end up getting it to work before i turned it in, but whatever. I am still getting an infinite loop though; this is what i have:
include<stdio.h>
int main(void)
{
int numbs,rem;
char ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN;
printf("Give a number to be converted: ");
scanf("%d", &numbs);
rem=numbs%10;
do
{
switch (rem) {
case 0:
printf("ZERO\n");
break;
ect....
;
}
numbs=numbs/10;
} while (rem > 0.1);
return (0);
}
any idea on what's going on?
Aha! Take out a piece of paper. Pretend I gave you the number "12345", and fill out the following table for each iteration through the loop
rem | numbs
----------------
5 12345
That's the setup before you enter the loop, you do the rest and see if you can pinpoint the mistake.
Just a heads up (so you don't think I just decided to bail after the homework deadline, because I am definitely not doing that!), I had a physics exam yesterday and a Diff Eq exam today, so either later tonight or tomorrow I'll get back to work on this! :)
Cool [= Glad to hear it. Let me know if you make any progress or get stuck anywhere or have any questions.
Oops! Totally forgot to check back on this with you; I ended up asking a bunch of questions during our 3 hour lab in this class (like three days after doing all this with you) and figured everything out completely. Thanks again though man! :D
No worries. What was the solution?
Ah! that makes a lot of sense. How would I go about doing that, though? Possibly some gnarly equation that will subtract whatever the following digits are and divide by 10^(however many digits there are)?