Better way to write this?

Hi, working from the textbook and doing this example that basically searches for the next value in an array instead of just finding the first one and exiting. Should I be doing something to increment the pos variable or is it okay the way I have it where it's just updated by the index?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
   Finds the next occurrence of an element in an array.
   @param values an array of values
   @param searched_value the value to search for
   @param start the position at which to start the search
   @return the position of the first match at position >= start,
   or -1 if the element was not found
*/
int find_next(int values[], int size, int searched_value, int start)
{
   int pos = 0;
   
   for(int i = start; i < size; i++)
   {
      
      if(values[i] == searched_value)
      {
         pos = i;
      }
   }
   
   if(pos != 0)
   {
      return pos;
   }
   else
   {
      return -1;
   }
}
Ignore, doesn't work for one of the requirements. Ooops!

IF you can use <algorithm>:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <algorithm>

int find_next(int values[], int size, int searched_value, int start)
{
   auto found = std::find(values + start, values + size, searched_value);

   if (found != values + size) { return *found; }
   else                        { return -1; }
}

int main()
{
   const int SIZE { 5 };

   int arr[SIZE] { 1, 3, 5, 7, 9 };

   int find1 { 3 };

   int found1 { find_next(arr, SIZE, find1, 0) };
   std::cout << found1 << '\n';

   int find2 { 12 };
   int found2 { find_next(arr, SIZE, find2, 0) };
   std::cout << found2 << '\n';

   find2 = 3;
   found2 = find_next(arr, SIZE, find2, 3);
   std::cout << found2 << '\n';
}
Last edited on
dakotad8218, your code finds the last matching value, not the first. To find the first (untested):
1
2
3
4
5
6
7
8
9
int find_next(int values[], int size, int searched_value, int start)
{
   for(int i = start; i < size; i++) {
      if(values[i] == searched_value) {
         return i;
      }
   }
   return -1;
}



@dhayden, thank you, honestly, for making me reexamine my attempt and noticing how I was trying to punch above my weight. Crow is a tasty little dish.
@Furry Guy:
You almost had it. If only you could convert pointer to index ... (rather than dereference it).
"Almost had it" is an assignment fail, at least in my book. :)

It was still a learning experience for me, so not all wrong.
to find the second value. I think thats what hes trying to do .

1
2
3
4
5
6
7
8
9
10
11
12
int find_next(int values[], int size, int searched_value, int start)
{
	int pos = 0;
	for (int i = start; i < size; i++) {
		
		if (values[i] == searched_value && !pos) {
			pos = 1;
		}
		else if (pos && values[i] == searched_value) { return i;}
	}
	return -1;
}


edit: idk what he wrote is different than what is described in the quotes. I'll leave this here for lols. with this method you can find next without having the start value as a parameter
Last edited on
Recursive solution...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int find_next(int values[], int size, int searched_value, int start)
{
    if(start + 1 < size) {
   return values[start + 1] ==  searched_value ? ++start : find_next(values, size, searched_value, ++start);
    }
  return -1;
}


int main()
{
 
 int myArray[] = { 3, 4, 5, 2, 3, 1, 5, 6, 3, 4 };
 
 cout << find_next(myArray, 10, 3, 2);
 
 
 return 0;
}
Topic archived. No new replies allowed.