I NEED HELp!!! PASSING ARRAY TO FUNCITON

I have no idea how to pass array to function... I thought i just need to use & ....
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
int user(int x);
int computer(int x);
int binarySearch( int &search, int x, int i, int The_Number);

int computer(int x)
{
	int The_Number = x;
	int fool = 0; 
	int Address;
	const int SIZE(100); 
	int search[SIZE]; 
	int lastnumber = 100, Guess, firstnumber = 0;
	for (int i = 0; i < 100; i++)
	{
		Address = i + 1;
		search[i] = Address;

	}
	while (x != Guess)
	{
		binarySearch(search, firstnumber, lastnumber, The_Number);
	}	
	cout << "Computer Guessed the Right Number!" << endl; 
	return fool;
}
// x fist i last number
int binarySearch(const int &search, int x, int i, int The_Number)
{
	bool found = false;
	int first = x, last = i; 
	int mid;
	while (first <= last && !found)
	{
		mid = (first + last) / 2;
		if (The_Number < search[mid])

	}

}
Last edited on
int binarySearch(const int &search, int x, int i, int The_Number)
should be int binarySearch(const int search[], int x, int i, int The_Number)
Be sure to make this change in your function declaration at the top of the program, I notice that const is not in your declaration but is in the definition down below.
You don't even need & in this case as you can't have an array of references.
Last edited on
Thank you but i still have problem on mid saying arry or pointer type?
Can you post the line that's throwing the error and the full error message?
intelliSense: expression must have pointer- to-object type
error C2109: subscript requires array or pointer type
Okay what about the line in your program? As in what line does IntelliSense underline? I'm asking because mid appears in several places.

Also, what other changes did you make? With a few changes, I got your code to compile, but I never encountered this error.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstdlib>  
#include <ctime>    
#include <algorithm>


using namespace std;
// It int x does not have to match with main function but int function have to match 
// Function for user or computer 
int user(int z);
int computer(int z);
int binarySearch(const int& search, int& x, int& i, int The_Number);
int main()
{
	srand(unsigned(time(NULL)));
	bool quit = true; 
	int Player, Computer, Guess, choice; 
	const int x = 100; 
	do{
		// Make Number I want to Guess
		Guess = rand() % x + 1; 
		cout << Guess << endl; 
		cout << "Enter 1 to Play as Player or 2 to see Computer Play or 3 to quit" << endl; 
		cin >> choice; 
		// Make Sure User press one or two
		 if (choice == 1)
		{ 
			Player = user(Guess);
		}
		else if (choice == 2)
		{
			Computer = computer(Guess);
		}
	
	} while (quit);


	return 0; 
}
int user(int z)
{//function for Player!!!! 
	int number = 0, i = 0; 
	while (z != number)
	{	
		cout << "Enter int number (guess number)!" << endl; 
		cin >> number;
		// Enters if Number is Higher than Guess Number!
		if (z < number)
		{
			cout << "Your Number is Higher!" << endl; 
		}
		//Enters if Number is Lower than Guess Number!
		else if (z > number)
		{
			cout << "Your Number is Lower!" << endl; 
		}
	}
	cout << "Congraduation You Got the Guess the Right Number!"<< endl; 
	return i;
}
int computer(int z)
{
	int The_Number = z;
	int fool = 0; 
	int Address;
	const int SIZE = 100; 
	int search[SIZE]; 
	int lastnumber = 100, Guess, firstnumber = 0;
	for (int i = 0; i < 100; i++)
	{
		Address = i + 1;
		search[i] = Address;

	}
	while (z != Guess)
	{
		binarySearch(search[SIZE], firstnumber, lastnumber, The_Number);
	}	
	cout << "Computer Guessed the Right Number!" << endl; 
	return fool;
}
// x fist i last number
int binarySearch(const int& search, int& x, int& i, int The_Number)
{
	bool found = false;
	int mid = 0;
	while (x <= i && !found)
	{
		mid = (x + i) / 2;
		if (The_Number < search[mid])
		{
		}
	}

}


this is what i have right now have underline on search[mid]
@wh1t3crayon but you can have a reference to array (this'd work only for static arrays), if I remember correctly. Still here it's better to use a pointer to array.
Oh, you didn't make the change which I provided for you. Line 83, change int& search to int search[]. You do want search to be an array, right?

Now that leaves line 77, where you are passing an int equal to the size of search[], not the search array itself. I believe you just want to pass the array? If so, just remove [SIZE] from line 77.

Side note, be sure to initialize Guess in line 68, and make sure that binarySearch() returns an int value.

@TheHardew right but in the parameters passing a reference of an int array (like the OP was trying to do anyways) would not compile.
Last edited on
Thank you i am an idot
Last edited on
Thank you i am a idot
No no, you're just on a learning curve. My first post ever was asking how to call a function from a class object (I am not joking, that took me days to understand).
@wh1t3crayon If he wanted to make reference to a static array he could have used some parentheses magic:
1
2
template <unsigned S>
int binarySearch(int (&search)[S], int x, int i, int The_Number);

And this way you don't have to ask for array size.
Topic archived. No new replies allowed.