We are about to switch to a new forum software. Until then we have removed the registration on this forum.
How do i make this code print the ID number of the person who lost the most weight?
int size = 10;
int[] id = new int[size];
int[] first = new int[size];
int[] second = new int[size];
int[] diff = new int[size];
int average = 0;
for(int n = 0; n < size; n++)
{
id[n] = (int) random(100); //chooses 10 people at random out of 100
first[n] = (int) random(100)+100; //beginning weight
second[n] = (int)random(100)+90; //new weight
diff[n] = second[n]-first[n]; //weight difference
average += diff[n];
println("Person#:"+id[n] + ", Beginning Weight:" + first[n] + ", New Weight:" + second[n] + ", Weight Lost:" + diff[n]); //prints all of the above 10 times- one for each person
}
int l = max(diff); //max weight difference
int g = min(diff); //min weight difference
println("Average Weight Loss: " + average/size+1); //print average weight loss
// this is what im stuck on, i can't get it to choose the random id number of the person that lost/gained the most
println("Most Weight Lost: " + "Person: " + id + " Lost: " + g);
println("Most Weight Gained: " + "Person: " + id + " Gained: " + l);
Answers
[deleted]
I would think you have to find the person who lost the most weight, and then print that person's id. What does the code do now?
max or min are useful, but since you want where the max or min is found, you have to do it yourself, using a loop. Then, when you find a value greater than the current max or lower than the current min, you keep its index (you need, of course, two indexes).
At the end of the loop, you have indexes giving the information about the corresponding persons.
Indeed.
Here's an example of what PhiLho is talking about: