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?
6 replies to this topic
#1
Posted 13 September 2010 - 07:25 PM
|
|
|
#2
Posted 13 September 2010 - 10:44 PM
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
Posted 14 September 2010 - 06:38 PM
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:

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
Posted 14 September 2010 - 10:20 PM
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
Posted 15 September 2010 - 12:10 AM
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
Posted 15 September 2010 - 12:57 AM
My biggest mistake was
if ((amount / values[i]) > 1)which should be
if ((amount / values[i]) >= 1)
#7
Posted 15 September 2010 - 03:43 AM
thanks guyz,
i fixed it up an hour after i posted it,
if((amount/values[i])>=1) like what oxano said..
:D
:thumbup:
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


Sign In
Create Account


Back to top









