Jump to content

Is there a short code to run a problem like this ?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
harddisk

harddisk

    Newbie

  • Members
  • PipPip
  • 12 posts
sample output:

an integer 445 is equal to:
1000 - 0
500 - 0
200 - 2
100 - 0
50 - 0
20 - 2
10 - 0
5 - 1
1 - 0

the integer will be based on the user input.
i created a code using "if and else" but it is actually containing several lines, so it would be quite bad if i post it here :) ...
anyone could show a code that minimizes the one i made?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
This seems short:

    int amount = 445;

    int[] values = {1000, 500, 200, 100, 50, 20, 10, 5, 1};

    int[] occurrencies = {0, 0, 0, 0, 0, 0, 0, 0, 0};

    String text = "";

 

    for(int i=0 ; i<values.Length ; i++){

      if((amount/values[i])>1){

        int x = amount/values[i];

        amount = amount - (values[i]*x);

        occurrencies[i] = x;

      }               

    }

 

    for(int i=0 ; i<occurrencies.Length ; i++){

      Console.WriteLine(values[i] + " - " + occurrencies[i]);

    }



#3
harddisk

harddisk

    Newbie

  • Members
  • PipPip
  • 12 posts

oxano said:

This seems short:

    int amount = 445;

    int[] values = {1000, 500, 200, 100, 50, 20, 10, 5, 1};

    int[] occurrencies = {0, 0, 0, 0, 0, 0, 0, 0, 0};

    String text = "";

 

    for(int i=0 ; i<values.Length ; i++){

      if((amount/values[i])>1){

        int x = amount/values[i];

        amount = amount - (values[i]*x);

        occurrencies[i] = x;

      }               

    }

 

    for(int i=0 ; i<occurrencies.Length ; i++){

      Console.WriteLine(values[i] + " - " + occurrencies[i]);

    }


i changed it to let it ask an input from a user:

harddisk:
Console.WriteLine("Enter Integer: ");
int amount = int.Parse(Console.ReadLine());
int[] values = { 1000, 500, 200, 100, 50, 20, 10, 5, 1 };
int[] occurrencies = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

for (int i = 0; i < values.Length; i++)
{
if ((amount / values[i]) > 1)
{
int x = amount / values[i];
amount = amount - (values[i] * x);
occurrencies[i] = x;
}
}

for (int i = 0; i < occurrencies.Length; i++)
{
Console.WriteLine(values[i] + " - " + occurrencies[i]);
}
goto harddisk;

this is the output:

Posted Image

as you can see, at integer 123, it suppose to be 100 - 1, 20-1, 1-3..
and at 523, it suppose to be 500 - 1, 20-1, 1-3..

can you fix it up please? :)

Edited by harddisk, 14 September 2010 - 06:42 PM.
image does not show


#4
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
The problem is in the checked condition inside the main loop:

for (int i = 0; i < values.Length; i++)

{

    [COLOR="red"]if ((amount / values[i]) > 1)[/COLOR]

    {

        int x = amount / values[i];

        amount = amount - (values[i] * x);

        occurrencies[i] = x;

    }

}
This is not correct. I would remove that "if" or change it with:

for (int i = 0; i < values.Length; i++)

{

    [COLOR="red"]if (amount == 0)

    {

        break;

    }[/COLOR]

    int x = amount / values[i];

    amount = amount - (values[i] * x);

    occurrencies[i] = x;

}
This should solve the problem.

#5
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
And I would change your example to be:
for (int i = 0; i < values.Length; i++) {

    if (amount == 0) {

        break;

    }

    occurrencies[i] = amount / values[i];

 [COLOR="Red"]   amount %= values[i];[/COLOR]

}


#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
My biggest mistake was
if ((amount / values[i]) > 1)
which should be
if ((amount / values[i]) >= 1)


#7
harddisk

harddisk

    Newbie

  • Members
  • PipPip
  • 12 posts
thanks guyz,

i fixed it up an hour after i posted it,

if((amount/values[i])>=1) like what oxano said..

:D

:thumbup:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users