# Array comparison

**URL:** https://discourse.processing.org/t/array-comparison/37207
**Category:** Coding Questions
**Created:** [May 29, 2022, 2:37pm UTC](https://discourse.processing.org/t/array-comparison/37207 "2022-05-29T14:37:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mvb](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mvb](https://discourse.processing.org/u/mvb)
#### Post date: [May 29, 2022, 2:37pm UTC](https://discourse.processing.org/t/array-comparison/37207/1 "2022-05-29T14:37:55Z")

</div>

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

```auto
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);
  }
 }
}

```

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [May 29, 2022, 3:21pm UTC](https://discourse.processing.org/t/array-comparison/37207/2 "2022-05-29T15:21:58Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [May 29, 2022, 3:41pm UTC](https://discourse.processing.org/t/array-comparison/37207/3 "2022-05-29T15:41:17Z")

</div>

> [@NumericPrime](#):
>
> ```auto
> int indexLowest(float[] array) {
> int ind=0;
> float min=array[0];
> for (int i = 1; i < array.length; i++) 
> if (array[i] < min) {
> ind = i;
> min = array[i];
> }
> return ind;
> }
> 
> ```

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [May 29, 2022, 8:49pm UTC](https://discourse.processing.org/t/array-comparison/37207/4 "2022-05-29T20:49:02Z")

</div>

> [@mvb](#):
>
> The problem is, I don’t know how to do it.

I see an opportunity!

Is this what you are trying to achieve?

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/7/9786b543dc28f37392abff51cff58c1fb4a77368.png)

References:

- [text() / Reference / Processing.org](https://processing.org/reference/text_.html)
- [fill() / Reference / Processing.org](https://processing.org/reference/fill_.html)
- [https://processing.org/reference/if.htm](https://processing.org/reference/if.htm)
- [color / Reference / Processing.org](https://processing.org/reference/color_datatype.html)

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](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

### Author: ![oskikarb](https://avatars.discourse-cdn.com/v4/letter/o/ed655f/32.png) [@oskikarb](https://discourse.processing.org/u/oskikarb)
#### Post date: [January 6, 2023, 11:03am UTC](https://discourse.processing.org/t/array-comparison/37207/6 "2023-01-06T11:03:05Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/4/a458a6d638b2c8e9f11487854f4135107eb6ea1e.png)
