Binary Search

closed account (4jzvC542)
I have been given following example on Binary Search
it is not working and showing one error and i cant understand what is
wrong with the 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
  #include <iostream>

const int N = 10;

void main()
{
	int A[N];
	int i, intial, final, mid, data;

	std::cout << "Enter ten(10) values in ascending order : \n";

	for(i = 0; i < N; i++)
		std::cin >> A[i];

	std::cout << "Enter the data to be serched : \n";

	std::cin >> data;

	intial 0;
	final = N - 1;
	mid = (intial + final)/2;

	while((intial <= final) && (A[mid] != data))
	{
		if(A[mid] > data)
			final = mid - 1;
		else
			intial = mid + 1;
	}
	if (A[mid] == data)
		std::cout << "Data Present \n";
	else
		std::cout << "Data Absent \n";
}


it Shows following error :

--------------------Configuration: binary - Win32 Debug--------------------
Compiling...
binary.cpp
F:\00 C++Study 13\03 Home-Work Exercise\chapter 11 Array\Examples\03 binary serch\binary.cpp(19) : error C2143: syntax error : missing ';' before 'constant'
Error executing cl.exe.

binary.exe - 1 error(s), 0 warning(s)


please help me where i am wrong
parjanya
the error is telling you to look at line 19,

although the error message isn't very helpful, you should still be able to see that it should read "initial = 0;"

closed account (4jzvC542)
@Jaybob66

Thanks problem solved....
it was just initial = 0;

however there is another problem
i entered 10 ascending number : 1 2 3 4 5 6 7 8 9 10

now if i enter number to be searched = 5
then it will show present status

however if i will search 12(which is obviously not there)
it(Console) just keeps on blinking and i have to terminate it with Ctr + c
and win task manager shows CPU USAGE = 100%
as soon as i terminate it gets down too
whats the problem
Last edited on
closed account (4jzvC542)
Now working code is :
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
#include <iostream>

const int N = 10;

void main()
{
	int A[N];
	int i, intial, final, mid, data;

	std::cout << "Enter ten(10) values in ascending order : \n";

	for(i = 0; i < N; i++)
		std::cin >> A[i];

	std::cout << "Enter the data to be serched : \n";

	std::cin >> data;

	intial = 0;
	final = N - 1;
	mid = (intial + final)/2;

	while((intial <= final) && (A[mid] != data))
	{
		if(A[mid] > data)
			final = mid - 1;
		else
			intial = mid + 1;
	}
	if (A[mid] == data)
		std::cout << "Data Present \n";
	else
		std::cout << "Data Absent \n";
}

Topic archived. No new replies allowed.