Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

basic C

Name: Anonymous 2010-11-24 12:17

can you help create a program that will accept an amount and output its denomination.

example

amount: 5670.45

1000: 5
500: 6
200: 0
100: 6
50: 1
20: 1
10: 0
.25: 1
.05: 3

Name: Anonymous 2010-11-24 12:18

>>1
Read SICP.

Name: Anonymous 2010-11-24 12:19

sorry im just new to C and programming.

help plox

Name: Anonymous 2010-11-24 12:20

bump

Name: Anonymous 2010-11-24 12:26

>>2 and go back to /b/.

Name: Anonymous 2010-11-24 12:29

ive already done my code
i just want to know how you will do this because i did mine with 55 lines

Name: Anonymous 2010-11-24 12:31

scanf("%f", &x);
if (x>=1000) q=x/1000;
else q=0;
x=x-q*1000;
printf("1000: %d\n" , q);

// and so on..

other sugestions?

Name: Anonymous 2010-11-24 12:38

still here..

any other sugestions?

Name: Anonymous 2010-11-24 12:45

Why are there six 5000s? And the denominations look pretty arbitrary.

Name: Anonymous 2010-11-24 12:57

>>5
Fuck off, faggot.

Name: Anonymous 2010-11-24 13:36

Learn English, then learn C.

Name: Anonymous 2010-11-24 15:34

#include <stdio.h>

int main()
{
    double denom[9] = { 1000, 500, 200, 100, 50, 20, 10, .25, .05 };
    double amount;
    scanf("%f", &amount);

    int i, n;
    for (i = 0; i < 9; i++) {
        n = 0;
        while (amount > denom[i]) {
            amount -= denom[i];
            ++n;
        }
        printf("%8.2f: %d\n", denom[i], n);
    }
    return 0;
}

Name: Anonymous 2010-11-24 17:54

>>2
What kind of moron uses "double" to represent currency?  And then subtracts values in a loop?  Did your brain fry from writing too much ECMAScript?


$ cat test.c
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
    double x = strtod(argv[1], 0);
    while (x >= 0.05) x -= 0.05;
    printf("x = %f\n", x);
    return 0;
}
$ gcc test.c ; ./a.out 0.15
x = 0.050000


Note how "x = 0.05000", and yet "x >= 0.05" is false?  Need I connect the dots, from A to B to C as for a little child?

Name: Pwerlwrl 6 2010-11-24 19:11

Not really related, but what the hell ....


use v6;
my $amount = 5670.45;
my %change;
for <1000 500 100 50 10 5 1 0.50 0.25 0.01> -> $d {
  while $amount >= $d {
    %change{$d}++;
    $amount -= $d
  }
};
%change.perl.say;


Output:

{"1000" => 5, "500" => 1, "100" => 1, "50" => 1, "10" => 2, "0.25" => 1, "0.01" => 19}

Name: Anonymous 2010-11-24 19:28


($amt, @dens).reduce({
    my $x = 0;
    if $^b <= $^a {
        $x = ($a/$b).floor;
        say "$b:\t$x";
    }
    $a - $b * $x;
});

Name: Anonymous 2010-11-24 19:35

>>14
Tip: don't use auto quoting for arrays of numbers. You will end up with strings that numify at runtime, often to floats (with current Rakudo at least.) If you use actual numbers, you'll consistently get Rats which are precise in cases like this.

You can also do the following:

for <1000 500 ...>».Rat { ... }

Name: Anonymous 2010-11-24 20:25

Nice shiny nickel for explanation why this one prints out 4 nickels for 5670.45 whereas OP's program prints out 3 nickels.

import Data.Fixed
import System.Environment
import Numeric
denom = [1000, 500, 200, 100, 50, 20, 10, 0.25, 0.05] :: [Fixed E2]
makeChange (d:ds) x = let (n, y) = x `divMod'` d in (n, d) : makeChange ds y
makeChange _ _ = []
showPair (n, c) = showFixed True c ++ ": " ++ show n
main = do ((x,_):_) <- getLine >>= return . readFloat
          mapM_ (putStrLn . showPair) $ makeChange denom x

Name: Anonymous 2010-11-24 21:13

Nice shiny nickel for explanation why this one prints out 4 nickels for 5670.45 whereas OP's program prints out 3 nickels.
That's been covered by a number of posts already.

Name: Anonymous 2010-11-24 21:25

>>15
I like this. Care to explain how it works?

Name: Anonymous 2010-11-24 21:59

>>19
Sure.

.reduce is a list method which takes a coderef. Each pass it eats &code.arity (here 2) elements from a copy of the list, and puts the return value back onto the head of the copy. It does this until there's only one element left in the list copy, which is the reduced answer. So @list.reduce({$^a+$^b}); is the same as a simple summation reduction, [+] @list; ($^a etc simply introduce $a as the a-th argument and can be used to calculate arity.)

The logic in the closure is to propagate the first element as the amount (less the value of denominations printed out) down the list until it runs out of denominations. So, in reduce, the head is always the current $amt, and the tail is the list of remaining denominations to be evaluated. (If a denomination is greater than the amount, $x remains at zero, so $a (the current amount) is propagated unaltered.)

In this code "return" can't be used because the closure will return from the enclosing context so implicit returns (which are different) are used instead. A sub could have been used instead, and returns would have been okay. (IIRC there's also a keyword which will do the same as the implicit return in this case.)

Name: Anonymous 2010-11-24 22:02

thanks for the reply..
i guess i need to study more and LURk more..

cheers /prog/

Name: Anonymous 2010-11-24 22:18

>>21
If you're interested in learning Perl 6 and all it's magic, you might want to pop in at #perl6 on irc.freenode.net. They're

Name: >>22 2010-11-24 22:19

Sorry,
... they're helpful and usually very welcoming there.

Name: >>19 2010-11-24 22:37

>>22-23
I'm not >>21, but sure, if >>1,21 is interested he is welcome to go there as well.

Thank you so much for your explanation, >>20. Would you happen to have some links, or at least search terms, for Perl coderefs and the related syntax $^a?

Name: Anonymous 2010-11-24 23:24

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  unsigned long d[] = {10000, 5000, 2000, 1000, 500, 100,
                       25, 10, 5, 1, 0};
  unsigned long x, *p, n;
  char *s;

  if (argc < 2)
    x = 0;
  else {
    x = 100 * strtoul(argv[1], &s, 10);
    if (*s == '.')
      x += strtoul(s+1, NULL, 10);
  }

  for (p = d; *p; p++)
    if (n = x / *p) {
      printf("%3lu.%02lu: %lu\n", *p / 100, *p % 100, n);
      x -= n * *p;
    }

  return 0;
}

Name: Anonymous 2010-11-24 23:39

Oh, gotcha.

If you know Perl 5, check out http://perlgeek.de/en/article/5-to-6 ($^a is covered under "twigils"). Rakudo Star includes a book, I can't remember where to find the book outside of that distribution.

http://feather.perl6.nl/syn/S04.html#The_Relationship_of_Blocks_and_Declarations covers 3 kinds of coderefs, and you can see the kind of evolution that lets you use the placeholder ($^a etc) variables.

http://perl6.org/ is a good place to start. http://perlcabal.org/ has links to the official documentation, but it's littered with historical things, and sometimes it can be confusing since Rakudo doesn't implement everything yet (Perl 6 is fairly large, the existing core is quite usable.)

http://try.rakudo.org/ is a great place to test out stuff if you don't want to install anything, it has a multiline REPL.

>>1
Sorry for shitting up your thread with all the Perl 6 promotions. C is a fine language.

Name: Anonymous 2010-11-25 0:34

>>15,20,22-23,26
I have no idea why a gentleman like you would hang out in /prog/, for you are a much better person than most of us put together. I tip my hat to you, sir. Or perhaps it's just that this board's been shit lately, which of course does not remove any of your merit, actually quite the contrary).

Name: Anonymous 2010-11-25 5:18

>>27
I think it's metatroll. He's trolling us by giving candies to children, and helping students in need.

Name: Anonymous 2010-11-25 5:19

>>28
Also look at his good manners: he did not forget to put sage into email field. Not like some failures on this board.

Name: Anonymous 2010-11-25 6:41

>>20
Why did Larry name the function reduce instead of fold?

Name: Anonymous 2010-11-25 7:36

>>30
Because ``fold'' is the name used by functional hipsters.

Name: Anonymous 2010-11-25 7:42

>>31
Oh no, not the ``hipster'' ``faggot''.

Name: Anonymous 2010-11-25 11:38

you can fold a sheet, but you can't reduce it

Name: Anonymous 2010-11-25 12:03

>>33
fire.

Name: Anonymous 2010-11-25 12:04

>>32
The only people to condemn ``hipster ``faggots'''' are other ``hipster ``faggots''''.

Name: Anonymous 2010-11-25 12:31

>>35
Decloseting by self-reference, eh?

Name: Anonymous 2010-11-25 14:16

>>36
Who says he is condemning hipster faggots. Maybe he likes hipster faggots and is just making a statement about them.

Name: Anonymous 2010-11-25 14:27

>>25
aids@fail:~$ ./test 10.5
 10.00: 1
  0.05: 1
aids@fail:~$ ./test 10.50
 10.00: 1
  0.25: 2

Name: Anonymous 2010-11-25 14:54

fixed.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    unsigned long d[] = {10000, 5000, 2000, 1000,
                        500, 100, 25, 10, 5, 1, 0};
    unsigned long *c, n;
    float x;

    if (argc != 2)
      x = 0;
    else
      x = (unsigned long)(100 * atof(argv[1]));
   
    for (c=d; *c; ++c)
      if (n = x / *c) {
          printf("%3lu.%02lu: %lu\n", *c / 100, *c % 100, n);
          x -= n * *c;
      }

   return 0;
}

I like how the for loop terminates.

Name: Anonymous 2010-11-25 14:57

1000*5 + 500*6 + 200*0 + 100*6 + 50*1 + 20*1 + 10*0  + .25*1 + .05*3 = 8670.40

Name: Anonymous 2010-11-25 15:06

>>39
floating point error. Add +0.005 before cast, but that doesn't work for negative values...

Name: Anonymous 2010-11-25 16:16

>>41
Add [arbitrary amount] before cast
Or use nextafter or (*((int*)&number))++

Name: Anonymous 2010-11-25 16:21

>>42
wouldn't that round up?

Name: Anonymous 2010-11-25 16:33

>>43
That wasn't the issue I was addressing

Name: Anonymous 2010-11-25 17:29

>>27
I'm usually as bad as all of us, I just... er... I'm just wearing my Sussman hat?

>>30
Just guessing, but: partly for map/reduce cred. Partly because 5 already has some things named reduce and partly because there is no foldr (so the folding concept isn't so strong.) You can @list.reverse.reduce({...}); instead, and swap $^a and $^b in the closure.

A cute thing about Perl 6 is that map was made a little better, but I use it less often. In many cases where map is appropriate, factoring the problem a little differently without map is just as good or better.

Name: Anonymous 2010-11-26 12:21

>>15
($amt, @dens).reduce: {
    my $x = 0;
    if $^b <= $^a {
        $x = ($a/$b).floor;
        say "$b:\t$x";
    }
    $a - $b * $x;
};


The colon form helps take the pain out of supplying closures to methods.

Name: >>12 2010-11-26 14:23

>>13
I don't really know C. "x = 0.05000; x >= 0.05" being false is bullshit, though. Subtracting in a loop might've made more sense to >>1 and I don't know if C has a divmod function.

Name: Anonymous 2010-11-26 14:49

>>47
Not knowing C isn't your problem. Your problem is not knowing IHBT 754.

Name: Anonymous 2010-11-26 15:20

>>47
Looks like somebody hasn't read their 754.

Name: Anonymous 2011-02-04 19:46

<

Don't change these.
Name: Email:
Entire Thread Thread List