Giles Harney

Greenhorn

Posts: 19

posted 12 years ago

I am trying to understand how to create this array... Actually I am trying to understand why an array should be used?

Would my first array be something like;

int [ ] coins = { 1, 5, 10, 25 };

?

Thanks,

-Giles

----

�Use an array to store the four values corresponding to coin types. Also, use an array to store the number amount of each coin based on the user�s input.

�To calculate the number amount of each coin, use a for loop that divides the remaining cents by the coin value.

�Assume valid data is entered.

import javax.swing.JOptionPane;

import java.lang.Math;

class CountMoney {

public static void main (String[] args){

double amount, sum;

String inputAmount = JOptionPane.showInputDialog(

"Enter number of cents:");

amount=Double.parseDouble(inputAmount);

int remainingAmount = (int)amount;

// Find the number of quaters in the remaining amount

int numOfQuarters = remainingAmount/25;

remainingAmount = remainingAmount%25;

// Find the number of dimes in the remaining amount

int numOfDimes = remainingAmount/10;

remainingAmount = remainingAmount%10;

// Find the number of nickels in the remaining amount

int numOfNickels = remainingAmount/5;

remainingAmount = remainingAmount%5;

// Find the number of pennies in the remaining amount

int numOfPennies = remainingAmount;

// Display results

if (numOfQuarters == 1) {

System.out.println(numOfQuarters + "--Quarter");

}

else if (numOfQuarters > 1) {

System.out.println(numOfQuarters + "--Quarters");

}

if (numOfDimes == 1) {

System.out.println(numOfDimes + "--Dime");

}

else if (numOfDimes > 1) {

System.out.println(numOfDimes + "--Dimes");

}

if (numOfNickels == 1) {

System.out.println(numOfNickels + "--Nickel");

}

else if (numOfNickels > 1) {

System.out.println(numOfNickels + "--Nickels");

}

if (numOfPennies == 1) {

System.out.println(numOfPennies + "--penny");

}

else if (numOfPennies > 1) {

System.out.println(numOfPennies + "--pennies");

}

}

{

} // method main

}

Would my first array be something like;

int [ ] coins = { 1, 5, 10, 25 };

?

Thanks,

-Giles

----

�Use an array to store the four values corresponding to coin types. Also, use an array to store the number amount of each coin based on the user�s input.

�To calculate the number amount of each coin, use a for loop that divides the remaining cents by the coin value.

�Assume valid data is entered.

import javax.swing.JOptionPane;

import java.lang.Math;

class CountMoney {

public static void main (String[] args){

double amount, sum;

String inputAmount = JOptionPane.showInputDialog(

"Enter number of cents:");

amount=Double.parseDouble(inputAmount);

int remainingAmount = (int)amount;

// Find the number of quaters in the remaining amount

int numOfQuarters = remainingAmount/25;

remainingAmount = remainingAmount%25;

// Find the number of dimes in the remaining amount

int numOfDimes = remainingAmount/10;

remainingAmount = remainingAmount%10;

// Find the number of nickels in the remaining amount

int numOfNickels = remainingAmount/5;

remainingAmount = remainingAmount%5;

// Find the number of pennies in the remaining amount

int numOfPennies = remainingAmount;

// Display results

if (numOfQuarters == 1) {

System.out.println(numOfQuarters + "--Quarter");

}

else if (numOfQuarters > 1) {

System.out.println(numOfQuarters + "--Quarters");

}

if (numOfDimes == 1) {

System.out.println(numOfDimes + "--Dime");

}

else if (numOfDimes > 1) {

System.out.println(numOfDimes + "--Dimes");

}

if (numOfNickels == 1) {

System.out.println(numOfNickels + "--Nickel");

}

else if (numOfNickels > 1) {

System.out.println(numOfNickels + "--Nickels");

}

if (numOfPennies == 1) {

System.out.println(numOfPennies + "--penny");

}

else if (numOfPennies > 1) {

System.out.println(numOfPennies + "--pennies");

}

}

{

} // method main

}

posted 12 years ago

There are a couple of reasons why you might want to use an array. first, can then use a loop with a counter to access each member. you don't have to keep re-writing code like you did for your 4 coin types, you can just do (pseudo code):

this will then put the number of coins needed into an array as well. (note that you may want to re-think the order you put the values in your coinValues array, or how you loop through it...)

Why would you do this? well, with a loop like this, you could EASILY add 50-cent peices, or silver dollars, or (if you live in Canada) $2 coins... and you will automatically extract those values into your coinsNeeded array.

you still then have to write the output case, but this can save you writing a lot of code...

this will then put the number of coins needed into an array as well. (note that you may want to re-think the order you put the values in your coinValues array, or how you loop through it...)

Why would you do this? well, with a loop like this, you could EASILY add 50-cent peices, or silver dollars, or (if you live in Canada) $2 coins... and you will automatically extract those values into your coinsNeeded array.

you still then have to write the output case, but this can save you writing a lot of code...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Giles Harney

Greenhorn

Posts: 19

posted 12 years ago

thank you Fred for your reply...

lets see if i am on the right track;

for (int i = 0; i<=3; i++)

{

coinsNeeded[i] = remaining amount / coinsValues[i];

remaining amount = remaining amount % coinValues[i];

}

'i' would be the counter, correct?

so, if on the first pass would this be correct;

coinsNeeded[0]= remaining amount / coinsValues[0];

which would mean, set the first element in the coinsNeeded array equal to the result of the remaining amount being divided by the first element in the coinsValues array (25?) ?

thanks!

-Giles

lets see if i am on the right track;

while (i < size of array)

coinsNeeded[i] = remaining amount / coinValues[i];

remaining amount = remaining amount % coinValues[i];

i++;

for (int i = 0; i<=3; i++)

{

coinsNeeded[i] = remaining amount / coinsValues[i];

remaining amount = remaining amount % coinValues[i];

}

'i' would be the counter, correct?

so, if on the first pass would this be correct;

coinsNeeded[0]= remaining amount / coinsValues[0];

which would mean, set the first element in the coinsNeeded array equal to the result of the remaining amount being divided by the first element in the coinsValues array (25?) ?

thanks!

-Giles

posted 12 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

This looks pretty good to me. of course it still needs a little fleshing out, but you're on the right track.

and instead of i<=3, you can use i<coins.getLength()

this way, if the size of your array changes, you don't have two worry about this loop.

some other things to be cautious of...

make sure your coinsNeeded array is big enough - you could probably use the same getLength() method above. which would again be good if you decided to add 50-cent coins.

you should probably put the data in the array in the opposite order than you have listed (which i think you figured out), or you could start at the last element of the array and work backwards in your loop...

and instead of i<=3, you can use i<coins.getLength()

this way, if the size of your array changes, you don't have two worry about this loop.

some other things to be cautious of...

make sure your coinsNeeded array is big enough - you could probably use the same getLength() method above. which would again be good if you decided to add 50-cent coins.

you should probably put the data in the array in the opposite order than you have listed (which i think you figured out), or you could start at the last element of the array and work backwards in your loop...