linear search algorithm HELP

this is just a quick example getting to the point.
i need to search for the values from array2 in array1 and display the position.
i know how to find an individual value, but i dont know how to find multiple values in the array2 to the other function. any thoughts?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
 const int size1=10;
 int array1[size1];
 const int size2=5;
 int array2 [size2];
 
 int z = function2(array1,size1,?); // ? is the values that need to be found
}

int function2(int thearray[],int thesize, int thevalues)// this stays like this
{

}
Last edited on
Loop over left array and treat each element as an individual one. Using a for-loop may be a good choice. Use the following syntax to choose an arrays element:

array1[index]

where index is an unsigned integer value ranging from 0 to the arrays size - 1.
is it possible to loop values into another function?
Have a look at http://www.cplusplus.com/doc/tutorial/. There you'll find detailed description of all C/C++ Control Structures.

You may put loops into any function or block of statements. Blocks of statements are those encapsulated by "{" and "}". You may even put loops into the statement block of another loop.

Maybe I don't really understand your question. So try a look into the Tutorial. It has simple but meaningful examples.
Yes.
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main() {
  const int N = 5;
  int arr[N] = { 7, 42, -4, 3, 101 };
  for ( int index = 0; index < N; ++index ) {
    std::cout << arr[index] << '\n'; // "loop into function"
  }
  return 0;
}


The "thevalues" is a single number. The "function2" returns a single number. One position. How would you tell with one integer that the numbers that you are searching are at positions 1, 4, 7, and 13?

You can not.

You can get one position for one search value with one function call. Call the function multiple times with different parameters in order to get multiple positions, one at a time.
Topic archived. No new replies allowed.