Getting Error on max() function

edited July 2015 in Questions about Code

Hello,

I am new to processing and new to programming in general. Can someone please help me with the code below?

I believe the error is related to the line with the max() function, which I need for the map function to properly import my data.

The error I keep getting back is "The method max(float,float) in the type PApplet is not applicable for the arguments (float)"

I have no idea what I am doing wrong....Thanks in advance.

Point [] points;
Table table;

void setup () {
  size(1000,1000);
  smooth();
  loadData();
  }

void draw() {
  background(255);
  for (int i = 0; i < points.length; i++) {
    points[i].display();
  }
}

void loadData() {
  table = loadTable("Point_Class_Data.csv", "header");
  points = new Point[table.getRowCount()];

  for(int i = 0; i< table.getRowCount(); i++) {
    TableRow row = table.getRow(i);
    float xpos = row.getFloat("MIXTO");
    xpos = map(xpos, 0, max(xpos), 0, width);
    points [i] =  new Point(xpos, height/2, 10, #FFCC00, #FFCC00, 175);
    println(xpos);
  }
}

class Point {

  float xpos;
  float ypos;
  color fill;
  color stroke;
  float alpha;
  float size;

  Point (float xpos_, float ypos_, float size_, color fill_, color stroke_, float alpha_) {
    xpos = xpos_;
    ypos = ypos_;
    size = size_;
    fill = fill_;
    stroke = stroke_;
    alpha = alpha_;
  }

  void display() {
    fill(fill,alpha);
    stroke(stroke,alpha);
    ellipse(xpos, ypos, size, size);
  }
}

Answers

  • max() takes two numbers as parameters, and returns the one with the higher value.

    You are only providing it with one value, which is confusing.

    Are you trying to get the maximum value from the i'th row of your table? Or maybe you want the maximum MIXTO value found in the table? You will need to look at each value/row in turn and determine this beforehand.

  • Please read the sticky post about code formatting.

    You are calling Max () with only one argument. It takes two and returns the higher

  • I am trying to use the max function on an array.

    The array variable is xpos.

  • float xpos = row.getFloat("MIXTO");
    

    It is not an array.

  • I see.

    I assumed it was because println(xpos) gives me back the entire list of numbers in the console.

    How do I make it into an array that can utilize array functions like max()?

  • Fixed code example so that its easier to read.

  • Printing gives you a whole list of numbers because it's in a loop!

    You have to work out the Max element of the xpos values beforehand, in a separate loop.

  • Answer ✓
      float found_max;
      for (int i = 0; i< table.getRowCount (); i++) { 
        TableRow row = table.getRow(i); 
        float xpos = row.getFloat("MIXTO");
        if(i==0 || xpos > found_max ){
          found_max = xpos;
        }
      }
    
  • Awesome guys. Thank you both for your help! This makes it clearer. Will go and dissect this code to make sure I understand.

Sign In or Register to comment.