I need some help for school

Write your question here.
I need help for one of my classes. I am supposed to write a binary search function. My professor gave me a file to start with.
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
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

const int ARRAY_SIZE = 100;

//x is the search value. If x is in the array, return the index of x in the array; otherwise return -1. 
int binarySearch(int array[], int first, int last, int x)
{
}

int main()
{ 
	int A[ARRAY_SIZE], B[ARRAY_SIZE], C[ARRAY_SIZE];// array C is used to hold the search results temporarily
	int n = 0;// the number of elements stored in A
	int m = 0;// the number of elements stored in B
	string dataFile = "hw1_Q6_data.txt"; // the name of the data file 
	string searchFile = "hw1_Q6_search.txt"; // the name of the search file 
	string outputFile = "hw1_Q6_output.txt"; // the name of the output file 
//these files are several random numbers.
	input(A, n, dataFile); 
	input(B, m, searchFile); 

	//for (int i = 0; i< n; i++)
	//	cout << A[i] << " "; 
	//cout << endl;


	//print the search results on the screen
	for (int i = 0; i < m; i++)
	{
		C[i] = binarySearch(A, 0, n-1, B[i]); 
		cout << "The position of " << B[i] << " is " << C[i] << endl; 
	}

	//output the search results to the output file
	output(C, B, m, outputFile);

	cout << endl; 
}


He wants me to write the BinarySearch function. I have no problem with that but I am not sure how I am supposed to pass the array using the int array[] notation he has set up.

this is my search code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

	int Half = (first+last)/2;
	do{
		if (array[Half] == x)
			return Half;					//if x = A[Half] return the index
		else if (array[Half] > x){			//if x < Half 
			first = Half;					//set first to Half
			Half = (first + last)/2;		//find the new Half
		}
		else{
			last = Half;
			Half = (first + last)/2;
		}
	}while(Half != first && Half != last);
	if (array[first] == x)
		return first;
	else if(array[last] == x)
		return last;
	else return -1;
I have no problem with that but I am not sure how I am supposed to pass the array using the int array[] notation he has set up.

See line 33 in your main above.
yeah I know how to call the function but when I do it doesn't pass any values into the search. I don't know why.
Topic archived. No new replies allowed.