Array comparison

Hello,
I have the following code, you have to get a canvas with two lines of numbers from the two arrays. The top row must contain the values of the top_array array and the bottom row must contain the values of the bottom_array. Other than that, I want to compare the positions of the two arrays to paint, for each position, the largest value in green and the smallest value in red. If both are the same, you will need to paint them white. The problem is, I don’t know how to do it.

Could someone help me? Thanks

int dimension_arrays = 10;
int[] top_array = new int[dimension_arrays];
int[] bottom_array = new int[dimension_arrays];

int distance = 0;

PFont f;

void setup() {
  size(600, 300);
  distance = width/dimension_arrays;
  for (int i = 0; i < dimension_arrays; i++) {
    top_array[i] = int(random(0, 100));
    bottom_array[i] = int(random(0, 100));
  }
}

void draw() {
  background(0, 100, 130);
   f = createFont("Arial", 28, true);
  textFont(f, 28);
for (int i = 0; i < top_array.length; i++){
    text(top_array[i], width/2, height/3);
  for (int j= 0; j < bottom_array.length; j++){
    text(bottom_array[j], width/2, height-height/3);
   if (top_array[i] > bottom_array[j]) {
     fill(0,255,0);
  }
  else if (bottom_array[j]>top_array[i]){
    fill(0,255,0);
  }
  else if(top_array[i]<bottom_array[j]){
    fill(255,0,0);
  }
  else if (bottom_array[j]<top_array[i]){
    fill(255,0,0);
  }
  else if(top_array[i]==bottom_array[j]){
    fill(0);
  }
 }
}

This method returns the index of the lowest element of your array:

int indexLowest(float[] array) {
  int ind=0;
  float min=999999;
  for (int i=0; i<array.length; i++) if (array[i]<min) {
    ind=i;
    min=array[i];
  }
  return ind;
}

A method for the max would work simular.

A slight variation on the above method but it makes no assumptions about the values in the array and performs one less comparison.

1 Like

I see an opportunity!

Is this what you are trying to achieve?

image

References:

Hints:

  • Use a similar loop
  • compare the two values for each index 0 to 9
  • assign a color based on comparison one for each row
  • apply color to text showing the number

This is achievable for beginner.

Is this a homework assignment?

Please format your code as per instructions here:
https://discourse.processing.org/faq#format-your-code

:)

Hi,
I´m trying to do exactly what you did at the sketch, but when i try to assign colors isn´t working properly.

Could you help me?

Thanks in advance.
image