Program doesn't cout anything!

Hi everyone!
I've written the code bellow and there are no errors.But when I run it it doesn't give me anything...It doesn't even cout messages. I don't know what the problem is! I'd appreciate your help.

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>

using namespace std;

int A[5];
//Binary Search
int bin_search(int A[], int n, int key)
{
	int low, high, middle;
	low=0;
	high=n-1;
	while(low<high)
	{
		middle=(low+high)/2;
		if(A[middle]==key)
		{
			return middle;
		}
		else if(A[middle]<key)
		{
			low=middle+1;
		}
		else high=middle-1;
		Sleep(20);
	}
	return -1;
}


//Quick Sort
void quick_sort(int A[], int left, int right)
{
	int pivot, i, j, temp;
	i=left;
	j=right+1;
	pivot = A[(left + right / 2)];
	
	if(left< right)
	{
		do
		{
			do
			{
				i++;
			}
			while(A[i]<pivot);
			do
			{
				j--;
			}
			while(A[j]>pivot);
			if(i<j)
			{
				temp=A[i];
				A[i]=A[j];
				A[j]=temp;
			}
		} 
		while (i<j);
		temp=A[j];
		A[j]=A[left];
		A[left]=temp;
		Sleep(1);
		quick_sort(A,left, j-1);
		quick_sort(A,j+1, right);
	}
}

//Bubble Sort
void bubble_sort(int A[], int n)
{
	int i, pass, flag, temp;
	flag=0;
	pass=1;
	while (flag==1 || pass <=n)
	{
		flag=0;
		for(i=0;i<=n-pass; i++)
		{
			if (A[i] > A[i+1])
			{
				temp=A[i];
				A[i]=A[i+1];
				A[i+1]=temp;
				flag=1;
			}
		}
		pass++;
	}
}

//Linear Search
int lin_search(int array[], int size,int val)
{
	for(int i=0; i<size;i++)
	{
		if(val==array[i])
		{
        return i;
		}
	}
	return -1;

}
//Hanoi Tower

int disk;
void Hanoi(int n, char A, char B, char C)
{
	if(n==1)
	{
		cout<<"move disk from "<< 'A' << "to" << 'C' << endl;
	}
	else 
	{
		Hanoi(n-1,A,C,B);
    cout<<"move disk from " << 'A' << "to" << 'C' << endl;
    Hanoi(n-1,B,A,C);
	}
}

//Calling and timing the functions
int main()
{
	int j;
	int t;
	const int size1 = 5000;
	int arr1[size1];
	for (int count = 0; count < size1; count++)
	{
		arr1[count] = rand() % 1000;
	}

	const int size2 = 10000;
	int arr2[size2];
	for (int count = 0; count < size1; count++)
	{
		arr2[count] = rand() % 1000;
	}

	const int size3 = 25000;
	int arr3[size3];
	for (int count = 0; count < size1; count++)
	{
		arr3[count] = rand() % 1000;
	}

	const int size4 = 50000;
	int arr4[size4];
	for (int count = 0; count < size1; count++)
	{
		arr4[count] = rand() % 1000;
	}

	const int size5 = 130000;
	int arr5[size5];
	for (int count = 0; count < size1; count++)
	{
		arr5[count] = rand() % 1000;
	}


	//Binary Search Start
	const clock_t Starttime1 = clock();
	for (j = 0; j <= 10000; j++)
		bin_search(arr1, size1, -1);
	t = clock() - Starttime1;
	cout << "Binary Search Function Time (O(log2n))" << endl;
	cout << "Time 1: " << int(t) / CLOCKS_PER_SEC << endl;


	const clock_t Starttime2 = clock();
	for (j = 0; j <= 10000; j++)
		bin_search(arr2, size2, -1);
	t = clock() - Starttime2;
	cout << "Time 2: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime3 = clock();
	for (j = 0; j <= 10000; j++)
		bin_search(arr3, size3, -1);
	t = clock() - Starttime3;
	cout << "Time 3: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime4 = clock();
	for (j = 0; j <= 10000; j++)
		bin_search(arr4, size4, -1);
	t = clock() - Starttime4;
	cout << "Time 4: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime5 = clock();
	for (j = 0; j <= 10000; j++)
		bin_search(arr5, size5, -1);
	t = clock() - Starttime5;
	cout << "Time 5: " << int(t) / CLOCKS_PER_SEC << endl;
	cout << " " << endl;
	// Binary Search End



	//Quick Sort Start
	const clock_t Starttime6 = clock();
	for (j = 0; j <= 100000000; j++)
		quick_sort(arr1, size1, -1);
	t = clock() - Starttime6;
	cout << "Quick Sort Function Time (O(nlogn))" << endl;
	cout << "Time 1: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime7 = clock();
	for (j = 0; j <= 1000000; j++)
		quick_sort(arr2, size2, -1);
	t = clock() - Starttime7;
	cout << "Time 2: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime8 = clock();
	for (j = 0; j <= 1000000; j++)
		quick_sort(arr3, size3, -1);
	t = clock() - Starttime8;
	cout << "Time 3: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime9 = clock();
	for (j = 0; j <= 1000000; j++)
		quick_sort(arr4, size4, -1);
	t = clock() - Starttime9;
	cout << "Time 4: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime10 = clock();
	for (j = 0; j <= 1000000; j++)
		quick_sort(arr5, size5, -1);
	t = clock() - Starttime10;
	cout << "Time 5: " << int(t) / CLOCKS_PER_SEC << endl;

	cout << " " << endl;
	// Quick Sort End

	//Bubble Sort Start
	const clock_t Starttime11 = clock();
	for (j = 0; j <= 100000000; j++)
		bubble_sort(arr1,j);
	t = clock() - Starttime11;
	cout << "Quick Sort Function Time (O(nlogn))" << endl;
	cout << "Time 1: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime12 = clock();
	for (j = 0; j <= 1000000; j++)
		bubble_sort(arr2,j);
	t = clock() - Starttime12;
	cout << "Time 2: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime13 = clock();
	for (j = 0; j <= 1000000; j++)
		bubble_sort(arr3,j);
	t = clock() - Starttime13;
	cout << "Time 3: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime14 = clock();
	for (j = 0; j <= 1000000; j++)
		bubble_sort(arr4,j);
	t = clock() - Starttime14;
	cout << "Time 4: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime15 = clock();
	for (j = 0; j <= 1000000; j++)
		bubble_sort(arr5,j);
	t = clock() - Starttime10;
	cout << "Time 5: " << int(t) / CLOCKS_PER_SEC << endl;

	cout << " " << endl;
	//Bubble Sort End


//Linear Search Start
const clock_t Starttime16 = clock();
	for (j = 0; j <= 10000; j++)
		lin_search(arr1, size1, -1);
	t = clock() - Starttime16;
	cout << "Binary Search Function Time (O(log2n))" << endl;
	cout << "Time 1: " << int(t) / CLOCKS_PER_SEC << endl;


	const clock_t Starttime17 = clock();
	for (j = 0; j <= 10000; j++)
		lin_search(arr2, size2, -1);
	t = clock() - Starttime17;
	cout << "Time 2: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime18 = clock();
	for (j = 0; j <= 10000; j++)
		lin_search(arr3, size3, -1);
	t = clock() - Starttime18;
	cout << "Time 3: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime19 = clock();
	for (j = 0; j <= 10000; j++)
		lin_search(arr4, size4, -1);
	t = clock() - Starttime19;
	cout << "Time 4: " << int(t) / CLOCKS_PER_SEC << endl;

	const clock_t Starttime20 = clock();
	for (j = 0; j <= 10000; j++)
		lin_search(arr5, size5, -1);
	t = clock() - Starttime20;
	cout << "Time 5: " << int(t) / CLOCKS_PER_SEC << endl;
	cout << " " << endl;


//Hanoi start
	cout<<"number of disks: "<<endl;
  cin>>disk;


  const clock_t begin_time = clock();
  Hanoi(disk,'A','B','C');
  cout<<"Hanoi : "<<int(clock() - begin_time)/CLOCKS_PER_SEC<<endl;
}

I'd recommend stepping through it with a debugger, so that you can see exactly how the code is running, line by line.
Your bin_search() hangs in an endless loop. Condition must be while(low <= high)

BTW. Testing is much easier when to test a function first separately with small numbers - like so:
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
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

#ifdef _DEBUG
  #define TRACE(...) { printf(__VA_ARGS__); }
#else
  #define TRACE(...)
#endif

int bin_search (int A[], int n, int key)
{
  TRACE("\nEntering bin_search: n = %u, key = %d", n, key);
  int low, high, middle;
  low = 0;
  high = n - 1;

  TRACE("\nlow = %u - high = %u", low, high);

  while (low <= high)
  {
    middle = (low + high) / 2;
    TRACE("\nMiddle = %u\tA[%u] = %u", middle, middle, A[middle]);
    if (A[middle] == key)
    {
      TRACE("\nFound %u at %u\treturn %u", key, middle, middle); 
      return middle;
    }
    else if (A[middle] < key)
    {
      low = middle + 1;
      TRACE("\nA[middle] < key \tlow = %u", low);
    }
    else 
    {
      high = middle - 1;
      TRACE("\nA[middle] < key t\low = %u\thigh=%u", low, high);
    }
    TRACE("\nlow = %u \t high = %u", low, high);
  }
  return -1;
}

int main()
{
  int numbers[] = {1,2,3,4,5,6,7,8,9,10};

  cout << "Searching for value " << 10;
  int result = bin_search(numbers, 10, 10);
  cout << "\n\nResult: " << result << "\n\n";

  cout << "Searching for value " << 1;
  result = bin_search(numbers, 10, 1);
  cout << "\n\nResult: " << result << "\n\n";

  cout << "Searching for value " << 5;
  result = bin_search(numbers, 10, 5);
  cout << "\n\nResult: " << result << "\n\n";

  cout << "Searching for value " << -1;
  result = bin_search(numbers, 10, -1);
  cout << "\n\nResult: " << result << "\n\n";

  system("pause");
  return 0;
}

Even better would be to use unit tests, probably to difficult for beginners.
Topic archived. No new replies allowed.