subroutine and array in c++

Hi everyone, i hope you’ll help me with this code. So basically after storing an array “arr”, its size “r” and an integer “k”, I gotta write a subroutines that replaces the first number of the array greater than “k” with the sum of k and r and return its index. how can I write it??
ilovelaptop wrote:
how can I write it??


Open an editor.

Start typing.
replaces the first number of the array greater than “k” with

What if ...
1
2
arr[] {3, 7, 4};
int k {42};

Which number is greater than k in that arr?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int findn(int arr[], size_t r, int k)
{
	for (size_t i = 0; i < r; ++i)
		if (arr[i] > k) {
			arr[i] = k + r;
			return i;
		}

	return -1;
}

int main()
{
	int arr[] {3, 7, 4};

	std::cout << findn(arr, sizeof(arr) / sizeof(arr[0]), 4);
}


Returns -1 if condition not satisfied.
Topic archived. No new replies allowed.