Arrays, Binary Seaching and Selection sorting.

Hi i'm up at 4am trying to figure out why my code won't work right. It compiles with no errors, but i'm not getting what i want. It's just a program that reads from a file and stores the 40 values in an array, sorts them with selection, then uses a binary search to find a value. Here she 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
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
#include<iostream>
#include<string>
#include<fstream>
#include<cstring>
#include <cstdlib>
#include<iomanip>
using namespace std;

void selectionSort(int [], int);
int binarySearch(int [], int , int);
const int SIZE = 40;
 


int main()
{
	
	int array [SIZE];
	int count = 0; 

	
	ifstream inputFile;
	string filename;
	int value, result;
	int median;
	


	cout << "Enter the filename: ";
	cin >> filename;

	inputFile.open (filename.c_str());

	cout<<"-----------------------------------------"<<endl;
	 
	if (inputFile)
	{
		
		while(count < SIZE && inputFile >> array[count])
			count++;

		cout << "There are "<< count <<" many values: "<<endl;

			
		for(int i = 0; i < count; i++)
		{
			cout << array[i] << endl;
			
		}
	}
	
	


	else 
	{
		while(!(inputFile))
		{
			cout << "Error opening file " <<filename<<endl;
			cout << "Re-enter the filename: ";
			cin >> filename;

			inputFile.open (filename.c_str());
		}
	
		while(count < SIZE && inputFile >> array[count])
			count++;
			
		for(int i = 0; i < count; i++)
			cout << array[i] << endl;		
	

	}


		
	inputFile.close();

	cout<<" "<<endl;
	cout<<"There are "<< count<< " numbers in " << filename<<endl;
	cout<<"-----------------------------------------"<<endl;
	selectionSort(array, SIZE);
	
	for(int q = 0; q < count; q++)
	{
			cout << array[q] << endl;
			
	}




	median = SIZE/2;
	cout<<"-----------------------------------------"<<endl;
	cout<<"The Median of the data set is: "<< array[median] << endl;
	cout<<"-----------------------------------------"<<endl;
	cout<<"Enter the number you're looking for: ";
	cin >> value;
	result = binarySearch(array, SIZE, value);
	if(result == -1)
	{
		cout<<"That number doesn't exist in the array."<<endl;
		return 0;
	}
	else 
	{
		cout<<"That number is found at element " << result;
		cout<< " in the array."<<endl;
	}




	
	
	

return 0;

}



void selectionSort(int a[], int size)
{

      int i, j, minIndex;
      int tmp;    



      for (i = 0; i < size - 1; i++) 
      {
		minIndex = i;

        for (j = i + 1; j < size; j++)
           if (a[j] < a[minIndex])
                 minIndex = j;

            if (minIndex != i) 
            {
            	tmp = a[i];
				a[i] = a[minIndex];
				a[minIndex] = tmp;

            }

      }

}

int binarySearch(int a[],int size,int target)
{
	int first =0,
		last = size -1,
		middle,
		position = -1;
	bool found = false;

	while(!found && first<=last)
	{
		middle = (first <= last) /2;
		if(a[middle] == target)
		{
			found = true;
			position = middle;
		}
		else if(a[middle] > target)
			last = middle -1;
		else if(a[middle] < target)
			first = middle +1;
	}
	return position;
	}


please i want to go to bed..
"i'm not getting what I want" -> could you be more specific? Is it not sorting, or is the output bogus, or what?
That's the thing i can't show you the output because there isn't one. I'm sorry i should include that yes it does sort, and outputs the array, then the array in the order, the problem is somewhere in between 98-107. When i type in a number to search i press enter and nothing happens :3 did i do something wrong with cin>> or result?
Line 162: middle = (first <= last) /2;

Not sure what that's supposed to be. (first <= last) returns a boolean which, when divided by two, becomes 0.
Should probably be (first+last)/2.
Gaminic i don't know how i can repay you, thank you so much for being up this late/early you are my hero.
You're very welcome.
(Also, it's around noon here, so don't worry about it!)
Topic archived. No new replies allowed.