Converting Coins


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting Coins
# 8  
Old 01-13-2009
You are correct, the amount of coins....37 = 1 quarter, 1 dime and 2 pennies
# 9  
Old 01-13-2009
The first one is correct
# 10  
Old 01-13-2009
.. well, that's a heck of an assignment to get you to "get used to scripting". In any case..

I don't know what's in this file "mynum" that you are grepping from, but, for this particular situation I see 2 approaches here:
1.- The newbie Smilie
2.- The mathematician. Smilie

The newbie is simple: 100 cents is a small finite number, and having 4 types of coins to work with (pennies, nickels, dimes, quaters) further reduces the permutations. So, you can basically create a file with 100 lines that describe each possible combination of coins depending on the input:
Code:
# cat myfile
1 one penny
2 two pennies
...
13 one dime 3 pennies
...
37 one quarter one dime 2 pennies
...

Here what you would do, when the user inputs the amount, you'd grep myfile for that number and match it to a line in myfile that starts with that number, then output the rest of the line.

The mathematician, well, that one can get complicated. For that, you basically need to declare variables for each type of coin, then match the variables to the user input. There are probably 1000 ways to parse this: one approach would be to match the highest variable to the user input, once user input is less than variable, you'd add it to a numeric variable, then you go to the next highest variable, and so on. Something like:

Code:
Qcount=0
Qtotal=0
Dcount=0
Dtotal=0
Ncount=0
Ntotal=0
Pcount=0
Ptotal=0
TOTAL=$USERINPUT

# Find number of quarters
while  (( ${TOTAL} >= 25 })); do
               let Qcount=${Qcount}+1
               let Qtotal=${Qtotal}+25
               let TOTAL=${TOTAL}-25
done

Once TOTAL is less than Qtotal, Qcount will tell you how many quarters are in the original TOTAL, and TOTAL what is left, which should be less than 25 cents.
Then you'd use the same loop to figure out the dimes, then the nickels, then the pennies.
When you are finished, you can echo Qcount, Dcount, etc...

The loop will act something like this:
Code:
while 87 is bigger than or equal to 25, do
          let Qcount=1
          let Qtotal=25
          let TOTAL=62
   do it again
while 62 is bigger than or equal to 25, do
            let Qcount=2
            let Qtotal=50
            let TOTAL=37
   do it again
while 37 is bigger than or equal to 25, do

... and so on.

Get the idea?

Like I said, there are probably 1000 ways to work this, and I haven't tested the loops, but that should give you a start.

Last edited by System Shock; 01-13-2009 at 05:38 PM..
# 11  
Old 01-13-2009
Thanks for the reply "system", I will give it a shot.
# 12  
Old 01-13-2009
Quote:
Originally Posted by tzeman
I have never written a script so I guess you could say I am very yellow.Smilie However, I could use some help.... I need to write a Korn Shell Script that converts coins.

If you are just learning shell scripting, learn to write scripts that will run in any POSIX shell (which includes Korn shell).
Quote:
Meaning if I print or echo "Please enter amount:" and the user puts in 3.87, I need to know the least amount of coins that would equal the .87 cents.

Am I making any sense? I hope someone can give me a push in the right direction. Thanks in advance!!!

This is a relatively trivial exercise; it took me a couple of minutes and about a dozen lines of code.

A more interesting exercise would be to do it without a single-unit coin (but with other odd-denomination coins).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in converting

I have Excel file with the below three columns, i need your expertise in converting this to .csv file delimiter "|" Excel - Serial Number Serial Name Serial Brand 111 test sample 123 test2 sample1 134 ... (9 Replies)
Discussion started by: kiran_hp
9 Replies

2. Shell Programming and Scripting

converting empty value into NA

I have a set of values separated by a tab ch:12 1 3 4 ch:13 3 3 4 ch:25 1 8 ch:23 2 8 1 There is a missing value in the third column and i would like replace it with NA such that the final output would look like ch:12 1 3 ... (7 Replies)
Discussion started by: johnkim0806
7 Replies

3. Solaris

Help converting my Script

So i have been asked to write a script for a friend who has a server running solaris 11. My problem is the correct syntax i can't seem to get it work with the follow code, i have read through the date man page and found no work around Below is the code for part of the rsync script. The code i... (2 Replies)
Discussion started by: paperghost
2 Replies

4. UNIX for Advanced & Expert Users

Need help converting ctlU (^U) to a \12...

I have a file that ends with a ctlU: > cat -v test.file blah,blah,blah,GEAEA*1*xx0000111xxx^UIEA*xxx0^U would like to replace the ctlU (^U) with a "\12"...due to printers or something. I believe I might be able to utilize the tr command, if I could only identify the correct... (4 Replies)
Discussion started by: mr_manny
4 Replies

5. SCO

library converting

Hi everybody Is there any sco unix command to convert .so library to .a (under sco unix openserver.5.0.6) tnx (2 Replies)
Discussion started by: javad1_maroofi
2 Replies

6. Shell Programming and Scripting

Converting \0 to a \n

Hi - I have seen some similar posts but I am a bit stumped here below is the first line of a 'od -c filename' command. I want to change the \0 to \n 0000000 l s \0 c d - \0 c d . . \0 l s I have tried a sed construct in a script......... sed... (2 Replies)
Discussion started by: ajcannon
2 Replies

7. SCO

Converting

I use Sco_Sv 3.2v5.0.5 with parellel conection using dump terminals and i want to convert them to desktop pc. Anybody knows what hardware and other thing that would be involved? (3 Replies)
Discussion started by: seeyou
3 Replies

8. Shell Programming and Scripting

Converting to Uppercase

I want to convert string into uppercase string. How can i do that ? Ex: Enter the user name: read name show=upper(name) echo $show --- This output should be the uppercase output. Thanks (3 Replies)
Discussion started by: dreams5617
3 Replies

9. UNIX for Dummies Questions & Answers

converting .txt

Hello, I transferred some .txt files from windows to Unix. When i used the editor in Unix to open up the file, all the <cr> show up. How to I get rid of all of them? (4 Replies)
Discussion started by: laila63
4 Replies

10. UNIX for Dummies Questions & Answers

converting kb to mb

When I create filesystems in AIX i often get confused(using smit) When you specify size in aix, it is asked like this SIZE of file system (in 512-byte blocks) I never seem to grasp this, what is the equation to get say 500mb? Or is there a program anyone knows of that does this, like a... (1 Reply)
Discussion started by: csaunders
1 Replies
Login or Register to Ask a Question