returning index position HELP

my program takes the values from one array and searches for their index position in another array(linear search algorithm).
this is an example of the issue im having
a[]={1,2,3,4,5,6}
Arr[]={1,2,2,3,4,5}
if it finds 1 in arr, it returns 0, which is fine but if it finds 2 in arr it return 1 and 1 instead of 1 and 2. any thoughts on how to fix this.


1
2
3
4
5
6
7
8
9
10
int secfunc(int arr[],int SIZE, int values) // this is the 2nd function which 
{                                           // searches for  them.
 for (int a=0;a<SIZE;a++)
 {
  if(arr[a]==values)
  {
   return a;
  }
 }
}
closed account (SECMoG1T)
Well if you need all the indices at which a value occurs your will need to do more than that, probably store the already found indices somwhere ... that's just an idea.
1) Function can return only one value. So it returns first found in your case.
2) Your function has a serious problem: if nothing is found it won't return anything at all which causes undefined behavior and might make your program to stop working properly.

You can use resumable function approach by using pointers instead of array and size or make function return a container of found indices:

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
31
32
#include <vector>
#include <iostream>


std::vector<int> secfunc(int* arr, std::size_t SIZE, int values)
{
    std::vector<int> indices;
    for (int a = 0; a < SIZE; ++a)
        if(arr[a] == values)
            indices.push_back(a);
    return indices;
}


template<typename T, std::size_t N>
std::size_t array_size(T (&array)[N])
{
    return N;
}

int main()
{
    int numbers[] = {2, 1, 1, 4, 5, 2, 1, 3};
    auto ind = secfunc(numbers, array_size(numbers), 1);
    if(ind.size() == 0)
        std::cout << "No elements equals 1\n";
    else {
        std::cout << "Indices of elements which equals 1: ";
        for(auto i : ind)
            std::cout << i << ' ';
    }
}
Indices of elements which equals 1: 1 2 6
that looks advanced from what im learning, is there another method of achieving this ?
What is advanced? vectors> It is basically C++ replacement for C arrays.
You can ignore safe array size template and just write size manually.

Another way is to use something like:
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
#include <iostream>

//inferior copy of
//http://en.cppreference.com/w/cpp/algorithm/find
int* secfunc(int* ibegin, int* iend, int values)
{
    for ( ; ibegin < iend; ++ibegin)
        if(*ibegin == values)
            break; //stop iterating as soon as we find number
     //return pointer to found element of to past-the-end iterator if nothing found
    return ibegin;
}


int main()
{
    int numbers[8] = {2, 1, 1, 4, 5, 2, 1, 3};
    int* ibegin = numbers;
    int* iend = ibegin + 8; //Past-the-end iterator
    std::cout << "Indices of elements which equals 1: ";
    while(ibegin != iend) {
        if((ibegin = secfunc(ibegin, iend, 1)) == iend)
            break; //Stop iterating if there isn't anything found anymore
        std::cout << ibegin - numbers << ' ';
    /*calculating distance ↑↑↑ from array beginning (index)*/
        ++ibegin; //Start searching from one element pas last found
    }
}
im sorta confused by your code, this is mine. it loops the values from one array into the second function and compares it with the other array
how do i integrate the premise of yours into mine?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for (int q=0; q=size2;q++)
{
  int rs=secfunc(array1;size1;array2[q])
  if(rs>=0)
  {
    cout<<rs << "\n";
  }
}

return 0;

int secfunc(int arr[],int SIZE, int values)
for (int a=0;a<SIZE;a++)
 {
  if(arr[a]==values)
  {
   return a;
  }
 }
return -1;
}
Last edited on
Your function returns as soon as it finds a match. So in your first part of code you need to add another loop which will search again starting with next index (and changing size accordingly) until search function returns -1;
but if its already looping and returning each index if its found, or am i misunderstanding you?
It will still print only one index even if here several copies of the same value.
Topic archived. No new replies allowed.