wood burning stoves*
The moose likes Beginning Java and the fly likes Array problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array problem" Watch "Array problem" New topic

Array problem

Mike Smith

Joined: Jul 28, 2006
Posts: 9
I'm having one hell of a time with this problem using arrays. I realize this is long, but if anyone could help me with it I'd be so grateful. It's an assignment for class, and I've already turned it in as is below, but I spent two hours staring into the code and it just never came to me. I *have* to know how to fix my problem.

Create a list of 100 donations, choosing both a random charity (1-15) and a random donation ($1.00 - $100.00). You should print the the charity and donation, and also record both the charity and the donation in two arrays, one integer and one float.

When you are done creating the individual donations, go back through the data, and create a THIRD array, and total the donations for each charity. (This array is subscripted by the charity number (1 - 15) and contains floating point TOTALs. At the same time, create yet a FOURTH array that contains the count of the number of donations for each charity. This will be used below.

Print out the total for each charity and the average donation for each of the charities.

Finally, go back through the original data yet again, and for each donation, print out whether it was below, equal to, or above the average donation FOR THAT CHARITY, and how much it differed from the average.

It's all well and good up until the last part about stating whether or donation is equal, below, or above the average for that charity.
Below I post the code I have so far, and have bolded the problem area.

public class Main {

public static void main(String[] args) {
//Program that generates 15 random charities out of 100, and 15 random amounts for those 15 charities, $0.0 - $100.00

//Define arrays and variables
int charities[] = new int[101]; //Array for charities
double donations[] = new double[101]; //Array for donations
double donation_total = 0.0;
double donation_avg = 0.0;
int count[] = new int[16]; //Count for how many donations a charity received
double total_donation_per_charity[] = new double[16]; //Array for total donation for each charity

//Set all positions in the arrays to 0.
for (int x = 1; x <= 15; x++) { //begin for x

charities[x] = 0;
donations[x] = 0.0;
count[x] = 0;

} //end for x

//Distribute 15 random charities and 10000/100 amounts 100 times
for (int x = 1; x <= 100; x++) { //begin for x

int chosenchar = (int) (Math.random() * 15) + 1;
double chosenamount = (double) ((Math.random() * 10000) + 1) / 100;

//Assign the randomly chosen amounts to positions in the arrays
charities[x] = chosenchar;
donations[x] = chosenamount;

//Increase the chosen charity's count by one. To determine how many times a charity got chosen.

//Tell how many donations each charity (1-15) got.
System.out.println("Charity #" + charities[x] + " received $" + donations[x]);

//Create a total amount donated to each charity.
total_donation_per_charity[chosenchar] += chosenamount;

//Create a donation total for all 100 times, to later be used for the overall average.
donation_total += chosenamount;

} //end for x


for (int x = 1; x <= 15; x++) { //begin for x

System.out.println("Charity #" + x + " received " + count[x] + " donations, totaling to $" +
total_donation_per_charity[x] + ", and averaging at $" + total_donation_per_charity[x] / count[x] + " each.");

} //end for x


for (int x = 1; x <= 100; x++) { //begin for x

if (donations[x] < total_donation_per_charity[x] / count[x])

System.out.println("Charity #" + charities[x] + " received $" + donations[x] + ", which was BELOW the average " +
"for Charity #" + charities[x]);
else if (donations[x] > total_donation_per_charity[x] / count[x])
System.out.println("Charity #" + charities[x] + " received $" + donations[x] + ", which was ABOVE the average " +
"for Charity #" + charities[x]);
System.out.println("Charity #" + charities[x] + " received $" + donations[x] + ", which was EQUAL the average " +
"for Charity #" + charities[x]);

} //end for x


donation_avg = donation_total / 15;
System.out.println("The total amount donated to all charities was $" + donation_total);
System.out.println("The average donation to each charity was $" + donation_avg);

} //end main

} //end class main

The problem is that to run through the 100 donations again with the amounts already known, I need a new For loop. So I have another For loop of 100 and it prints out the donations and charities found in the original For loop of 100. The problem is that in the If statement, to compare the current donation to the average, I have to use total_donation_per_charity[x] / count[x], which are both only 16 slotted arrays. The result is that the For loop stops after 15 donations. If you copy/paste this into NetBeans or something, you'll see, and hopefully someone will be able to point out my problem.

Thank you so so much.

P.S. I feel so dumb being stumped so easily. This is my first problem for class that I've been unable to do since class started about four weeks ago. Should I just pack up and consider myself a failure at programming, or do these kinds of things plague people often? What's the best strategy for getting around a problem like this? arg...)
[ July 28, 2006: Message edited by: Mike ]
Stu Thompson
Ranch Hand

Joined: Jun 14, 2006
Posts: 136

The problems seems a bit odd to me, but have two points:

1) donation and charity arrays are created with 101 elements. this seems wrong to me. donations would be 100 and charities 15, as per instructions.
2) your if section would be inside nested loops. one for donations and one for charities.

hope that helps.


"This is not to say that design is unnecessary. But after a certain point, design is just speculation." --Philip Chu
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183


Welcome to JavaRanch!

We have a strict policy on display names, which must be a real first and last name with a space between. A single name is not enough.

Please go here and fix your display name up, pronto. Thanks, pardner!

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link: http://aspose.com/file-tools
subject: Array problem