#Array Logic

15 messages · Page 1 of 1 (latest)

austere herald
#

You could first sort the array, so the min number would be at index 0, the max number at index length-1, then loop over those between.
BTW are you asking for a cpp solution? This server is about JS, but however - the logic might be the same, if cpp doesn't have any nice built-in for min/max in a list

fresh ore
#

You can use a min_element and max_element from the std library to get the min, max values. Then use vector<int>.begin() and vector<int>.end() as arguments for both methods. Then you can create a loop and check if minValue is less than n and n is less than the maxValue and multiply n 2 times.

#include <iostream>
#include <vector>

std::vector<int> Function(std::vector<int>& vec)
{
    int minValue = *std::min_element(vec.begin(), vec.end());
    int maxValue = *std::max_element(vec.begin(), vec.end());
    
    for (int& n : vec)
    {
        if (minValue <= n && n <= maxValue) n *= 2;
    }
} 
crude compass
fresh ore
#

Built-in libraries aswell?

crude compass
#

yup

fresh ore
#

🤔

crude compass
fresh ore
# crude compass I'll try to figure it out<:ReKill:830609251541057577> thanks mate

You can set both minValue and maxValue to the first element of the array. Create a for loop by setting the index to 1 in increase it's value until it reaches size. Inside the loop you check if the array of index i is less than minValue and set minValue to the array of index, same for maxValue, changing the sign.
Then create another loop, setting j to 0 and increment it until it reaches sizes again. Inside that loop check if minValue is less than n and n is less than maxValue, and multiply by 2.
Note that n = array of index

void Function(int matrix[], int size)
{
  int minValue = matrix[0];
  int maxValue = matrix[0];

  for (int i = 1; i < size; ++i)
  {
      if (arr[i] < minValue)
      {
        minValue = matrix[i];
      }
      if (arr[i] > maxValue)
      {
        maxValue = matrix[i];
      }
  }

  for (int j = 0; j < size; ++j)
  {
      if (minValue <= arr[j] && arr[j] <= maxValue) arr[j] *= 2;  
  }
}```
#

Note that

size = sizeof(inMatrix) / sizeof(inMatrix[0])

aswell.

crude compass
#

not the numbers between the max and min

fresh ore
#

I can say chatgpt is getting dumber overtime

austere herald
#

I think you need to use < and > instead of <= and >=

#

Otherwise you keep min/max included

wanton berry