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

Pages: 1-

A little help would be nice...

Name: Anonymous 2010-05-01 5:17

Hello /prog/rammers.

Can someone help me with a C dilemma I currently have?

We have to make a program that calculates how many ones, fives and tens you need given a whole number amount.

Hints, anyone?

Name: Anonymous 2010-05-01 5:20


while num>0
 if num>=10
  tens++
  num-=10
  next
 if num>=5
  fives++
  num-=5
  next
 if num>=1
  ones++
  num-=1
  next

Name: Anonymous 2010-05-01 5:24

I would do something like this:

if(num >= 10) {
   while(num >= 10) {
      num -= 10;
      countTens++;
   }
}
if(num >= 5) {
   while(num >= 5) {
      num -= 5;
      countFives++;
   }
}
if(num >= 1) {
   while(num >= 1) {
      num--;
      countOnes++;
   }
}

Name: 3 2010-05-01 5:25

>>2
Also, ignore me and do what he does

Name: Anonymous 2010-05-01 5:31

ones = num

Name: Anonymous 2010-05-01 6:20

>>1
Here's a CL solution that reuses a macro I wrote a long time ago, with a small twist:

(defmacro with-same-measurement-units (source unit-bindings &body body)
  (destructuring-bind (first second . rest) unit-bindings
    (let ((units (list (car second) (car first))))
      `(multiple-value-bind ,(reverse units) (truncate ,source ,(second first))
         ,@(if rest
               `((with-same-measurement-units
                     ,(first units) ,(cons second rest) ,@body))
               body)))))

(defun decode-money (coins)
  (with-same-measurement-units coins ((dimes 10) (nickels 5) (pennies))
    (values dimes nickels pennies)))

(decode-money 156) ;=>15,1,1

Name: Anonymous 2010-05-01 6:23

http://books.google.com/books?id=zW0EFRjsfdUC&lpg=PA54&ots=5bBTJCS-rt&dq=pick's%20formula%2C%20lattice%20triangulation&pg=PA42#v=onepage&q&f=false

Name: Anonymous 2010-05-01 6:27

ENTERPRISE SOLUTION

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
  char *n=argv[1];
  char *p = n + strlen(n) - 1;
  int f=0,o=0;
L:
  switch(*p) {
    case '0': *p = 0; break;
    case '5': *p = '0'; f=1; goto L;
    default: (*p)--; o++; goto L;
  }
  printf("tens: %s fives: %d ones: %d\n",n,f,o);
  return 0;
}

Name: OP 2010-05-01 6:42

I was thinking of something of a simpler scale though, as decoding them drives me insane.

Name: OP 2010-05-01 6:47

OP again. What I was looking for is a simple Input/Output program where you put in a whole number, then the program will compute for the least amount of ones, fives and tens.

Given example:
Input:24
Output:
 Tens:2
 Fives:0
 Ones:4

Hope this helps.

Name: Anonymous 2010-05-01 7:17

[code]
g=null;
t=(i-(r=i%10))/10;
f=(r-(s=r%5))/5;
o=s;

Should work for i >= 10, assuming i belongs to N and / is integer division with truncation.

Name: Anonymous 2010-05-01 7:19

>>9,10
9999999999 9/10 0000000000

Name: Anonymous 2010-05-01 7:33

may be borked; if it is, your new homework assignment is to fix it
#include <stdio.h>
#include <ctype.h>
char numberin[9];
int number;
int tens = 0;
int fives = 0;
int ones = 0;

int main(int argc, char *argv[])

{
   while(!isdigit(numberin)) {
      printf("\nPlease input a number: ");
      fgets(numberin, sizeof(numberin), stdin);
   }
   sscanf(numberin, "%d", &number);
   while(number > 0) {
      if(number > 10) {
         tens++;
         number -= 10;
      } else if(number > 5) {
         fives++;
         number -= 5;
      } else {
         ones++;
         number -= 1;
      }
   }
   printf("\nTens: %d", tens);
   printf("\nFives: %d", fives);
   printf("\nOnes: %d", ones);
  
   return(0);
}

Name: Anonymous 2010-05-01 8:09

>>11

#include<stdio.h>

int main(int argc, char *argv[])
{
   /* dear */
   auto long int s,i,r; /* please */
   auto long int* g = null;
   auto long int t = (i-(r=i%10))/10;
   auto long int f = (r-(s=r%5))/5;
   auto long int o = s;
   printf("| %s %l | %s %l | %s %l |\n","tens:",t,"fives:",f,"ones:",o);
   return 0;
}

Name: Anonymous 2010-05-01 8:40

>>14
You forgot to put the + in front of the numbers to make them be positive. E.g: (+10)

Name: Anonymous 2010-05-01 8:52

>>2-3
Prelude Control.Arrow> let countTensFivesOnes = second (flip divMod 5) . flip divMod 10
(0.00 secs, 524028 bytes)
Prelude Control.Arrow> countTensFivesOnes 28
(2,(1,3))
(0.00 secs, 0 bytes)
Prelude Control.Arrow> [code]

or in c:
[code]tens = num / 10
num %= 10;
fives = num / 5
ones %= 5;

Name: Anonymous 2010-05-01 8:53

>>2-3
Prelude Control.Arrow> let countTensFivesOnes = second (flip divMod 5) . flip divMod 10
(0.00 secs, 524028 bytes)
Prelude Control.Arrow> countTensFivesOnes 28
(2,(1,3))
(0.00 secs, 0 bytes)
Prelude Control.Arrow>


or in c:
tens = num / 10
num %= 10;
fives = num / 5
ones %= 5;

Name: Anonymous 2010-05-01 9:40

ones %= 5;
i think you meant ones = num % 5;...

typedef struct { int ones, fives, tens; } counts;

static inline counts count_ones_fives_tens(int n)
{ int t = n / 5;
  return (counts)
  { .ones = n % 5,
    .fives = t % 2,
    .tens = t / 2 }; }

Name: Anonymous 2010-05-01 10:55

I don't even understand what your assignment wants.  Maybe it could be worded a little better.

Name: Anonymous 2010-05-01 11:51



#include <stdio.h>

int crunch(input){
        int tens, fives, ones;
        printf("INPUT WAS: %i\n", &input);
        tens = input % 10;
        fives = fives % 5;
        ones = fives % 1;
        printf("tens:     %i        | fives:    %i       |    ones:  %i  \n", &tens, &fives, &ones);
        return(20);
}


int main(){
        int number_to_be_analyzed;
        printf("Give to me an integer: ");
        scanf("%i", &number_to_be_analyzed);
        crunch(number_to_be_analyzed);
        return(1);
}

Name: Anonymous 2010-05-01 13:06

>>20
Why do you return 1? And where is fives set?

Name: Anonymous 2010-05-01 13:40

>>20
return(1);
wat.

Name: Anonymous 2010-05-01 15:18

ones = fives % 1; should be ones = ones % 1;, maybe?

>return(20);

(  _-_)

Name: Anonymous 2010-05-01 15:21

>>23

Should be

        tens = input % 10;
        fives = tens % 5;
        ones = fives % 1;


But having it process -1080601408 probably doesn't help OP.

Name: Anonymous 2010-05-01 18:42

>>2,3,13
Do you also perform of a and b with while (a > 0) b++;?
I know a lot of you are trolling, but a problem this simple shouldn't go this long without a correct solution.

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

int main(int argc, char **argv)
{
    int num;

    if (argc < 2) {
        fprintf(stderr, "No number provided.\n");
        return 1;
    }

    num = atoi(argv[1]);
    printf("Tens:\t%d\nFives:\t%d\nOnes:\t%d\n",
           num / 10, num % 10 >= 5, num % 5);

    return 0;
}

Name: >>25 2010-05-01 18:44

Do you also perform of a and b with while (a > 0) b++;?
See? I'm drunk and even I write better C than any of you.

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