Arrays

edited December 2013 in Using Processing

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

  • edited December 2013

    [deleted]

  • edited December 2013

    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:

    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;
    int big = 0;
    int small = 0;
    int smallId = 0;
    int bigId = 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
      //--------------------------------------------------
      //Here are the lines of code that compare the weights and take in ID info.
      average += diff[n];
      if(small > diff[n]) {
        small = diff[n];
        smallId = id[n];
      }
    
      if(big < diff[n]) {
        big = diff[n];
        bigId = id[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
    }
    println("Average Weight Loss: " + average/size+1); //print average weight loss
    
    println("Most Weight Lost: " + "Person: " + smallId + " Lost: " + small); 
    println("Most Weight Gained: " + "Person: " + bigId + " Gained: " + big);
    
Sign In or Register to comment.