-1

```
int found=false;
int first=0;
int last= arraysize-1;
int count2=0;
while(first<=last && found==false)
{
int loc=(first+last)/2;
if(key<sorted_array[loc])
last=loc-1;
else
{
if(key>sorted_array[loc])
first=loc+1;
else
if(key==sorted_array[loc])
{
first=loc+1;
count2++;
}
else
found=true;
}
}
cout<<"the number of students with this grade are: "<<count2<<endl;
```

I have already sorted an array of size 100 so its in ascending order. I am using this binary search to find how many times a number, which i have called KEY is found in this sorted_array[100]. so far this part:

```
if(key==sorted_array[loc])
{
first=loc+1;
count2++;
}
else
found=true;
```

does count the ammount of numbers which match the key, BUT it only counts the ones above LOC, which is the middle. Basically its ignoring any numbers that might match the KEY that lie below LOC. Is there some way to get it to count these numbers below LOC that match the key?