Skip to content

Instantly share code, notes, and snippets.

@jaredonline
Created September 21, 2011 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredonline/1231156 to your computer and use it in GitHub Desktop.
Save jaredonline/1231156 to your computer and use it in GitHub Desktop.
Homeworks
/* 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.
*/
@afelli
Copy link

afelli commented Sep 21, 2011

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)?

@jaredonline
Copy link
Author

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?

@afelli
Copy link

afelli commented Sep 21, 2011

We haven't learned arrays yet, but we definitely do know if the % operator. It stands for the remainder, right? Not quite sure though

@jaredonline
Copy link
Author

Right, so, along that line, consider the return value of

123 % 10

Have you learned about loops yet?

@afelli
Copy link

afelli commented Sep 21, 2011

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.

@jaredonline
Copy link
Author

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?

@afelli
Copy link

afelli commented Sep 21, 2011

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)?

@jaredonline
Copy link
Author

Also, don't confuse control structures and loops.

A loop is a type of control structure: http://en.wikipedia.org/wiki/Control_structure#Choice

@jaredonline
Copy link
Author

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?

@afelli
Copy link

afelli commented Sep 21, 2011

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.

@jaredonline
Copy link
Author

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?

@afelli
Copy link

afelli commented Sep 21, 2011

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.

@afelli
Copy link

afelli commented Sep 21, 2011

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)

@jaredonline
Copy link
Author

This is due at 10?

@afelli
Copy link

afelli commented Sep 21, 2011

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 :)

@jaredonline
Copy link
Author

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! ]
*/

@jaredonline
Copy link
Author

Big hint given so you can turn it in on time [=

@jaredonline
Copy link
Author

Although, there is a big flaw for this method of doing it, but we can work on that as a last step.

@afelli
Copy link

afelli commented Sep 21, 2011

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?

@jaredonline
Copy link
Author

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.

@afelli
Copy link

afelli commented Sep 22, 2011

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! :)

@jaredonline
Copy link
Author

Cool [= Glad to hear it. Let me know if you make any progress or get stuck anywhere or have any questions.

@afelli
Copy link

afelli commented Oct 3, 2011

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

@jaredonline
Copy link
Author

No worries. What was the solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment