#Array Logic
15 messages · Page 1 of 1 (latest)
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;
}
}
The problem is that they don't want us to use libraries in the exam
Built-in libraries aswell?
yup
🤔
I'll try to figure it out
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.
That's the same logic I used, the thing is that when I used this solution it multiplied all the numbers in the array
not the numbers between the max and min
I can say chatgpt is getting dumber overtime
I think you need to use < and > instead of <= and >=
Otherwise you keep min/max included
In this code arr is undefined. The array passed in is matrix.