Binary Search

Hi,
I tried to write a binary search algorithm, but for some reason, I have an 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
// Search.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

void generate_sorted_array(int n, int* &arr)
{
	arr=new int[n];
		
	for (int i=0; i<n; ++i)
	{
		arr[i]=i;
	}
}

int search_x(int x, int* arr, int n)
{
	int start=0;
	int size=n;

	while(size!=0)
	{
		int mid=start+size/2;
		if (x>arr[mid])
		{
			start=mid+1;
			size=size/2;
		}

		else if (x<arr[mid])
		{
			size=size/2;
		}

		else 
			return mid;
	}
	return -1;
}

void print_array(int n, int* arr)
{
	for (int i=0; i<n; ++i);
	{
		cout << arr[i] << ",";
	}
}

void main()
{
	int* arr;
	generate_sorted_array(100, arr);
	print_array(100,arr);

	int found_number=search_x(0,arr,100);
	cout << found_number << endl;
}



But I have an error at the:
1
2
3
4
for (int i=0; i<n; ++i);
	{
		cout << arr[i] << ",";
	}


The error is that i isn't defined apparently. But I don't understand why.

Thanks a lot for the help. :)
Remove the semicolon at the end of line 46.
I did that, and I still have the same error.
Removing ; should work.Check properly or see the revised code
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
#include <iostream>
#include <stdlib.h>
using namespace std;

void generate_sorted_array(int n, int* &arr)
{
	arr=new int[n];
		
	for (int i=0; i<n; ++i)
	{
		arr[i]=i;
	}
}

int search_x(int x, int* arr, int n)
{
	int start=0;
	int size=n;

	while(size!=0)
	{
		int mid=start+size/2;
		if (x>arr[mid])
		{
			start=mid+1;
			size=size/2;
		}

		else if (x<arr[mid])
		{
			size=size/2;
		}

		else 
			return mid;
	}
	return -1;
}

void print_array(int n, int* arr)
{
	for (int i=0; i<n; ++i)
	{
		cout << arr[i] << ",";
	}
}

void main()
{
	int* arr;
	generate_sorted_array(100, arr);
	print_array(100,arr);

	int found_number=search_x(5,arr,100);
	cout << endl << found_number << endl;
	cin.get();
}
You should not be using void main(). Main should always ALWAYS ALWAYS return an int.
I am using g++ 4.7.1 with c++11 enabled and I have trouble with this:

for (int i=0; i<n; ++i)

doing this fixes it:

1
2
int i;
for (i=0; i<n; ++i)


Maybe this is your problem too?

I am not sure why I have this problem, I thought the first syntax had been around for a while.

HTH
I'm not having this problem. When I wrote int i, there was an error that i was defined before being initialized. I actually tried to fix the code with the comments written here, and I still had the same error. Apparently, there's a system error, not a code error. I sent the code to a friend, who ran the code on his computer, and he didn't have any problems, despite the fact that he didn't change the code at all.
Make sure that the code that you see is the code that you compile and run. Not some copy of it.
When I wrote int i, there was an error that i was defined before being initialized.


Maybe this following code is a bit pedantic, but it should always work. I would be upset if it doesn't because that is C style that has been around forever.

1
2
int i = 0;  //declare and initialise
for (i=0; i<n; ++i)  //notice dont specify int again 


Maybe you should post the exact compiler output?

I sent the code to a friend, who ran the code on his computer, and he didn't have any problems, despite the fact that he didn't change the code at all.


That implies different compiler settings - a system error as you say. We would need to know lots about your system to fix that. The compiler & version, the compiler flags, maybe the compile command being used. Some of this we could see from the compiler output.

Topic archived. No new replies allowed.