REALLY basic functions problem

Hi all,

So I’ve been given an assignment to produce a function that gets the lowest value of an array, then stores that value and uses it to color a circle, and write that same value onto that circle.

I cannot get my function to work. It seems to do nothing, commenting it out makes no impact whatsoever.

I am basically losing the will to live here as the person giving my class this assignment hasn’t given us any outlines of syntax and I am clearly ordering things or calling things wrong here.

If you can help, I would HUGELY appreciate it.

int a[]= new int [50];
int MIN= 0;

void setup()
   {
  size(450,450);
  
  for(int i=0; i < a.length; i++)
      {
  a[i] = (int) random (60,255);
print(a[i]+" , ");
println("MIN+",MIN);
      }
     }

  
 void draw(){
   fill (MIN);
   textSize(width/4);
   ellipse(width/2,height/2,width/2,height/2);
   
   fill (240);
   textSize(width/4);
   text(MIN, width*.32 ,height*.58);
   
   getMIN(8);
   
   }


void getMIN(int MIN){
 for(int i=0;i<a.length; i++)
   {
    if(a[i]< MIN){
      MIN=(a[i]);
      }
   }
}
1 Like

You are almost there

Try to call getMin without a parameter (so in the line void getMIN(int MIN){
delete int MIN) and call getMin at the end of setup ()

2 Likes

Also since you check if the values in the array are < MIN I advise to give MIN a very high value initially so the condition is met !

Welcome to the forum!

3 Likes

Oh yeah I’ve less of a problem just running it within the setup.

But this is the brief,

"• Write a program which has a global array of 50

integers with random values assigned to the array.

• The values should be displayed in the console window in a row

• Write a function ‘getArrayMin’ to return the minimum value in the array.

• Draw a circle in the centre of the window with the
greyscale colour returned by the getArrayMin function.

The circle should be 50% the size of the window

• The value returned should be displayed inside the
circle and be at least 50% the size of the circle

• The value should be visible whether the circle is black or white"

So I have to do it in a function. For example, this works and I think it’s what you’re describing…

int a[]= new int [50];
int MIN= 255;

void setup()
{
size(450,450);

for(int i=0; i < a.length; i++)
{
a[i] = (int) random (60,255);
print(a[i]+" , ");
println(“MIN+”,MIN);
if(a[i]< MIN){
MIN= a[i];

  }
  }
 }

void draw(){
fill (MIN);
textSize(width/4);
ellipse(width/2,height/2,width/2,height/2);

fill (240);
textSize(width/4);
text(MIN, width*.32 ,height*.58);

}

When I try to use “int” as the return type and say “return (MIN);” it says “This method must return a result of type int”

I really have no idea how to use these things.

2 Likes

Sorry, if I wasn’t clear

I didn’t say to delete the function.

Just delete the parameter

so in the line void getMIN(int MIN){ delete int MIN

Then call it with getMin();

Explanation

When you use void getMIN() { without the parameter, the variable MIN used in the function is just the global variable MIN that you already have. Good. It will impact what happens in setup and draw!

WITH the parameter the function uses the local parameter MIN and changes it without changing the global variable. Bad. We say the local variable / parameter is overshadowing the global variable MIN with the same name.

In both cases the function doesn’t return anything. Which is OK, when we change the global variable.

Alternatively

Alternatively (when the function should return anything) use an expression like MIN = getMin(); in setup().
Then the function looks slightly different:

  • at the beginning int getMIN() { (telling us what it is returning ) and
  • at its end return MIN; (returning MIN).
  • and also, it should declare a local variable MIN it returns. Would be best with another name than the global variable MIN, e.g. MIN_local or so.

I got both versions running now. Looks cool.

Remark

Actually what I would pass as a parameter is the array a.

E.g. void getMIN(int[] arrayLocal) {.................................

Chrisir

4 Likes

Chrisir,

You’re a superhero. Thanks so much. I did what you said and it works. Just on your alternative suggestion, that seems to be fitting the brief much better. That was what I wanted to use initially, but when I try to use the “int” as the returnType (and say “return (MIN)”) it tells me “This method must return a result of type int”.

Also, how would I call the variable MIN_local, if this was the way I was doing it? Am I close with this?
It’s saying “MIN_local cannot be reolved to a variable”.

int a[]= new int [50];
int MIN= 990;

void setup()
{
  size(450, 450);

  for (int i=0; i < a.length; i++)
  {
    a[i] = (int) random (90, 255);
    print(a[i]+" , ");
//    println("MIN+", MIN);
  }
  
MIN = getArrayMin();

if (MIN>40){
  fill(0);
}

}


void draw() {
  fill (MIN);
  textSize(width/4);
  ellipse(width/2, height/2, width/2, height/2);

  fill (240);
  textSize(width/4);
  text(MIN, width*.28, height*.58);

}

int getArrayMin() {
  for (int i=0; i<a.length; i++)
  {
    if (a[i]< MIN) {
      MIN_local=(a[i]);
      return (MIN_local);
    }
  }
}

declare the variable MIN_local locally at the start of the function

(your if-clause in the function read if (a[i]< MIN) { - can you spot the error)



int getMIN() {

  // declare the variable locally at the start of the function 
  int MIN_local=11000; 

  for (int i=0; i<a.length; i++) {
    if (a[i] < MIN_local) {
      MIN_local = a[i]; 
    }//if
  }//for
  return MIN_local;
}//function
1 Like

It’s a to do

Remark

Try the random between 110 and 222:

text will exceed the ellipse

1 Like

"Try the random between 110 and 222:

text will exceed the ellipse"

I was going to try get fancy with it so I can say:

  if (MIN>60){
  fill(0);
} else{
  fill(255);
}

But putting that in the draw function doesn’t seem to be working. Maybe I’m just framing it long.

I’m just trying to get a sense of how to express these sorts of terms even just reading your outline there gives me the right words to google and helps the syntax understanding a lot. Thanks again.

int a[]= new int [50];
int MIN= 255;

void setup()
{
  size(450, 450);

  for (int i=0; i < a.length; i++)
  {
    a[i] = (int) random (255);
    print(a[i]+" , ");
//    println("MIN+", MIN);
  }
  
//MIN = getArrayMin();

  if (MIN>60){
  fill(0);
} else{
  fill(255);
}


}


void draw() {
  fill (MIN);
  textSize(width/4);
  ellipse(width/2, height/2, width/2, height/2);
  

  fill (240);
  textSize(width/4);
  text(MIN, width*.28, height*.58);

}

int getArrayMin() {
    int MIN_local=11000; 
  for (int i=0; i<a.length; i++)
  {
    if (a[i]< MIN_local) {
      MIN_local= a[i] ;
    }
  }
  return MIN_local;
}

Remark 1

why did you comment this out:
//MIN = getArrayMin();

won’t work (I understand why, to test the if-clause)

Remark 2

That’s a duplicate: textSize(width/4);

Remark 3

This works:

 if (MIN>60) {
    fill(0);
  } else {
    fill(255);
  }

BUT it must be directly before the ellipse command because at the moment, there is another fill command after this if-clause which overwrites the last fill value…

  • Rule of thumb: Always do fill and stroke in draw() not in setup() and before using the ellipse or rect or text command that has to get this color.

Remark 4

The textSize for numbers MIN with 3 digits is another issue than the fill color.

Remark 5

Remember to hit ctrl-t in processing for auto-format of your code (indents…).

Chrisir

1 Like

" Remark 1

why did you comment this out:
//MIN = getArrayMin();"

Just to make the circle white®. I’ll uncomment it after I’m happy with my if/else statement but at the moment it’s not working for me.

"Remark 2

That’s a duplicate: textSize(width/4);"

So it is! thanks!

So what I have now is this…

int a[]= new int [50];
int MIN= 59;

void setup()
{
  size(450, 450);

  for (int i=0; i < a.length; i++)
  {
    a[i] = (int) random (255);
    print(a[i]+" , ");
//    println("MIN+", MIN);
  }
  
//MIN = getArrayMin();

}


void draw() {
  fill (MIN);
  ellipse(width/2, height/2, width/2, height/2);
  
  if (MIN<60){
  fill(255);
} else{
  fill(0);
}

  textSize(width/4);
  text(MIN, width*.28, height*.58);

}

int getArrayMin() {
    int MIN_local=11000; 
  for (int i=0; i<a.length; i++)
  {
    if (a[i]< MIN_local) {
      MIN_local= a[i] ;
    }
  }
  return MIN_local;
}

Seems to be working! Thanks again Chrisir you have honestly made such a big difference to my day. I couldn’t be more grateful.

1 Like

@Chrisir another soul saved :wink:

2 Likes

this

 if (MIN<60) {
    fill(255);
  } else {
    fill(0);
  }

is changing the color of the text (not of the ellipse) - which is ok

My version

  • intially MIN can be 0, it gets overwritten; MIN_local must have a big value initially though.

  • passing a as a parameter now. Therefore it can be moved into setup() and not be of global scope anymore.

  • using textAlign(CENTER, CENTER); and text(MIN, width/2, height/2);

Chrisir

int MIN = 0;

void setup() {
  size(450, 450);

  int[] a = new int [50];
  for (int i=0; i < a.length; i++) {
    a[i] = (int) random (255);
    print(a[i]+", ");
  }
  MIN = getArrayMin(a);
}//func

void draw() {
  // show ellipse
  fill (MIN);
  ellipse(width/2, height/2, width/2, height/2);

  // show text
  if (MIN<60) {
    fill(255);
  } else {
    fill(0);
  }
  textSize(width/4);
  textAlign(CENTER, CENTER); 
  text(MIN, width/2, height/2);
}//func

// ------------------------------------------------------

int getArrayMin(int[] a) {
  int MIN_local=11000;
  for (int i=0; i<a.length; i++) {
    if (a[i]< MIN_local) {
      MIN_local = a[i];
    }
  }
  return MIN_local;
}//func
1 Like