Problems with binary search function - c++

Having trouble with the binary_search function listed at the top. not sure where to go with it. I'm not very familiar with binary searching.
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
void get_input(ifstream& fin, int a[], int size, int & array_size);
void binary_search (int a[], int & array_size)
{
cout << "Please enter the element you would like to search for \n";
int element;
cin >> element;
int lastindex=array_size-1, startindex=0;
while (startindex <= lastindex)
{
int midindex=(array_size/2);
if(element > a[midindex])
{
startindex=midindex;
}
else if (element < a[midindex])
{
lastindex=midindex-1;
}
}
}
int main()
{
int array_size=-1;
int a[100];
ifstream fin;
get_input (fin, a, 100, array_size);
binary_search (a, array_size);
return 0;
}
void get_input (ifstream& fin, int a[], int size, int & array_size)
{
fin.open("numbers.txt");
if (fin.fail())
{
cout << "File failed to open";
exit(1);
}
for(int i = 0; i < size; i++)
{
a[i] = 0;
}
cout << "The numbers in the array are: \n\n";
for (int i = 0; i < size; i++)
{
if (!fin.eof())
{
fin >> a[i];
array_size ++;
}
}
for (int i = 0; i < array_size; i++)
{
cout << a[i] << " ";
}
cout << "\n\n\n";
cout << "The numbers in the array sorted are: \n\n";
for(int i = 0; i < array_size; ++i )
{
int temp2 = a[i];
for (int j = i+1; j < array_size; ++j )
{
if( a[j] < temp2)
{
temp2 = a[j];
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < array_size; i++)
{
cout << a[i] << " ";
}
cout << "\n\n\n";
fin.close();
}
when done the program is suppose to take an input from a file assign it to an array then sort the array. After this i need to use a binary search to find a number given by the user and display its place in the array to the user.
update: getting wrong output for the index found.... should i just add one to midindex?
void binary_search (int a[], int & array_size)
{
cout << "Please enter the element you would like to search for \n";
int element;
cin >> element;
int lastindex=array_size-1, startindex=0;
while (startindex <= lastindex)
{
int midindex= startindex + (lastindex - startindex) / 2;
if(element > a[midindex])
{
startindex=midindex+1;
}
else if (element < a[midindex])
{
lastindex=midindex-1;
}
else if (element == a[midindex])
{
cout<<"Element "<<element<<" found at index "<<midindex<<endl;
return;
}
}
}

Try changing
startindex=midindex;
to:
startindex=midindex + 1;
and
int midindex=(array_size/2);
to
int midindex= startindex + (lastindex - startindex) / 2
and most importantly you are doing nothing when you find the element !!
if(element == a[midindex]) {
cout<<"Element "<<element<<" found at index "<<midindex<<endl;
return;
}

My first reaction is to change the line
int midindex=(array_size/2);
to
int midindex = startindex + (lastindex - startindex) / 2;
Also, don't you want to report if the sought element was found or not? To detect the case when the element is found, another if branch like the following
if( element == a[midindex] )
can be inserted. That can have a return element; or return midindex inside it coupled with a return failure; outside the loop.
EDIT: I made a casual attempt to write a version of binary search. I don't claim it to be correct, as binary search is (in)famous for getting incorrect. Some code with test cases and output is uploaded at codepad.
Snippet:
int *
mybsearch( int const * const a, size_t const n, int const key ) {
int * lo = const_cast< int * >( a );
int * hi = lo + n;
while( lo <= hi ) {
int * const mid = lo + (hi - lo) / 2;
int const midelem = *mid;
if( key == midelem ) {
return mid;
}
else if( key < midelem ) {
hi = mid - 1;
}
else {
lo = mid + 1;
}
}
return NULL;
}
The main and test code:
int main() {
int const arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
size_t const num = sizeof( arr ) / sizeof( int );
int * pos20 = mybsearch( arr, num, 20 );
assert( pos20 && (*pos20 == 20) );
int * pos25 = mybsearch( arr, num, 25 );
assert( !pos25 );
int * pos5 = mybsearch( arr, num, 5 );
assert( !pos5 );
int * pos105 = mybsearch( arr, num, 105 );
assert( !pos105 );
}

Binary search works nicely as a recursive algorithm. Pass in the array and length, check the middle value, and recurse on the upper / lower half of the array, as appropriate.

Consider carefully what is not right about int midindex=(array_size/2); when array_size = 1. Then generalize to array_size = 3. Then to any odd number. This will require small run simulations in your head or on paper.

You're close. You want to do something like this:
int binary_search ...
so you can return the index of the element
while (startindex < lastindex)
{
int midindex=(startindex+endindex)/2;
if(element = a[midindex]) {
return midindex;
}
else if(element > a[midindex])
{
startindex=midindex+1;

Related

Potential Overflow Comparing Sorts and Comparisons of Various Sorting Algorithms

Im currently working with a simple program which compares the number of comparisons and swaps for a number of common sorting methods and testing it on data sets of size 1000, 2000, 5000, 10000, 50000 and 100000. The program appears to be working perfectly up until 50000 values however for example, testing the insertion method with random values at 50000 values yielded a comparison count of 626936785 however at 100000 values the output was -1788822792. I have included the full code below, is there potentially some sort of overflow occuring as the number of swaps and comparisons grows too large to properly keep track of?
#include <cstdlib>
#include <getopt.h>
#include <iostream>
#include <string>
using namespace std;
long long comparisons;
long long swaps;
bool comp_less(int a, int b){
++comparisons;
return a < b;
}
void swap(int& a, int& b)
{
++swaps;
int t = a;
a = b;
b = t;
}
void selectionSort(int *first, int *last)
{
for(int *i = first; i < (last - 1); ++i){
int *min = i;
for(int *j = i + 1; j < last; ++j){
if(comp_less(*j, *min)){
min = j;
}
}
swap(*i, *min);
}
}
void insertionSort(int* first, int* last)
{
for (int *i = first + 1; i < last; ++i)
{
int temp = *i;
int *j;
for (j = i; j > first && comp_less(temp, *(j - 1)); --j)
{
swap(*j, *(j - 1));
}
*j = temp;
}
}
int *partition(int *first, int *last)
{
int *pivot = last - 1;
int *i = first;
int *j = last - 1;
for (;;)
{
while (comp_less(*i, *pivot) && i < last)
{
++i;
}
while (*j >= *pivot && j > first)
{
--j;
}
if (i >= j)
break;
swap(*i, *j);
}
swap(*(last - 1), *i);
return i;
}
void quickSort(int* first, int* last) {
{
if ((first - last) <= 1)
return;
int *pivot = partition(first, last);
quickSort(first, pivot);
quickSort(pivot + 1, last);
}
}
int main(int argc, char** argv)
{
string algorithm = "selection";
string dataset = "random";
for (int c; (c = getopt(argc, argv, "ravqsin")) != -1;) {
switch (c) {
case 'r':
dataset = "random";
break;
case 'a':
dataset = "sorted";
break;
case 'v':
dataset = "reverse";
break;
case 'q':
algorithm = "quicksort";
break;
case 's':
algorithm = "selection";
break;
case 'i':
algorithm = "insertion";
break;
case 'n':
algorithm = "none";
break;
}
}
argc -= optind;
argv += optind;
const int size = argc > 0 ? atoi(argv[0]) : 10000;
int* data = new int[size];
if (dataset == "sorted") {
for (int i = 0; i < size; ++i) {
data[i] = i;
}
}
else if (dataset == "reverse") {
for (int i = 0; i < size; ++i) {
data[i] = size - i - 1;
}
}
else if (dataset == "random") {
for (int i = 0; i < size; ++i) {
data[i] = rand() % size;
}
}
if (algorithm == "quicksort") {
quickSort(data, data + size);
}
else if (algorithm == "selection") {
selectionSort(data, data + size);
}
else if (algorithm == "insertion") {
insertionSort(data, data + size);
}
else if (algorithm=="none"){
cout<< "Oops!" <<'\n';
exit(1);
}
cout << "OK" << '\n';
cout << "Algorithm: " << algorithm << '\n';
cout << "Data set: " << dataset << '\n';
cout << "Size: " << size << '\n';
cout << "Comparisons: " << comparisons << '\n';
cout << "Swaps: " << swaps << '\n';
return 0;
}
is there potentially some sort of overflow occuring as the number of swaps and comparisons grows too large to properly keep track of?
The output seems to indicate that overflow is happening.
You can add a test to be sure.
void swap(int& a, int& b)
{
++swaps;
if ( swaps == std::numeric_limits<decltype(swaps)>::max() )
{
std::cout << "Number of swaps reached max value. Resetting it 0.\n";
swaps = 0;
}
int t = a;
a = b;
b = t;
}
You can change the type of swaps to unsigned long long to delay the arrival of the maximum value. It's a good practice to stll have that test though.
unsigned long long swaps = 0;

Having Difficulty With This Q: Allow User To Input Values of Array and Do So With For, While loops, also output the largest number entered

I have to create a function which uses a for loop or while loop and an array, to display the largest number out of a group of numbers, and I'm having trouble with the question in the title.
When I run this code, it's not allowing the user to input however many elements in the array he wants, and it does not stop when the user wants to stop by entering a letter like g for ex. It also doesn't output the largest number at the end of what the user wants entered in the array.
What exactly is wrong with my code?
#include <iostream>
using namespace std;
void printarray(int array[], int size)
{
for (int i = 0; i < size; i++)
{
cout << array[i] << endl;
}
return;
}
int main()
{
const int SIZE = 2000;
int count = 0;
int userinput[SIZE];
int largest = 0;
for (int i = 0; i < SIZE; i++)
{
if (cin >> userinput[i])
{
count++;
}
else
{
break;
}
while (count < userinput[i])
{
if (largest < userinput[i])
{
largest = userinput[i];
}
count++;
}}
printarray(userinput, count);
cin.clear();
cin.ignore();
return 0;
}
The problem of the code is its bad indentation.
for (int i = 0; i < SIZE; i++)
{
if (cin >> userinput[i])
{
count++;
}
else
{
break;
}
while (count < userinput[i])
{
if (largest < userinput[i])
{
largest = userinput[i];
}
count++;
}}
The while loop is inside the for loop. And this condition of the while loop
count < userinput[i]
does not make sense.
Separate the loops.
for ( size_t i = 0; i < SIZE; i++ )
{
if (cin >> userinput[i])
{
count++;
}
else
{
break;
}
}
int largest = count == 0 ? 0 : userinput[0];
for ( size_t i = 1; i < count; i++ )
{
if ( largest < userinput[i] )
{
largest = userinput[i];
}
}
Take into account that you could use the standard algorithm std::max_element declared in the header <algorithm>
For example
auto it = std::max_element( userinput, userinput + count );
if ( it != userinput + count ) largest = *it;
The function printarray can be declared and defined like
std::ostream & printarray( const int array[], size_t size, std::ostream &os = std::cout )
{
for ( size_t i = 0; i < size; i++ )
{
os << array[i] << ' ';
}
return os;
}
Pay attention to that nowhere in your program the largest element is outputted.

Counter in arrays c++

i have a really simple code right there that counts how much values you need in arrays.
for (int i = 0; i < dm; i++)
{
if (arr[i] == c)
{
counter++;
}
};
But i need to make it a little bit tricky.I need to count number of same values. Imagine i have an array {4,4,4,3,3,2,2,1,1,0,0,0} and i need to find how much "twins" there. So 3,2,1 are twins because they have only 1 exact friend.
I tried something like 2 fors and 2 counters but still have troubles. Thanks. Hope you understand what i mean by "twin". x and x are twins and y,y,y are not ( just in case)
I'd make a map that counts - for each individual number in the array - their occurrences. The code could look as follows:
#include <iostream>
#include <map>
int main()
{
const int numberOfElements = 12;
int array[numberOfElements] = { 4,4,4,3,3,2,2,1,1,0,0,0 };
std::map<int,int> counts;
for (int i=0; i < numberOfElements; i++) {
counts[array[i]]++;
}
for (auto x : counts) {
if (x.second == 2) {
cout << "pair: " << x.first << endl;
}
}
return 0;
}
If - for some reason - the range of the elements is limited, you could also use a "plain" array for counting the occurrences. If, for example, the elements are in the range of 0..4, you could use the following fragment:
const int numberOfElements = 12;
const int elementMax = 4;
int array[numberOfElements] = { 4,4,4,3,3,2,2,1,1,0,0,0 };
int counts[elementMax+1] = {};
for (int i=0; i<numberOfElements; i++) {
counts[array[i]]++;
}
for (int i=0; i <= elementMax; i++) {
if (counts[i] == 2) {
cout << "pair: " << i << endl;
}
}
And if your array is sorted, than a solution without a counter-array could look as follows:
const int numberOfElements = 12;
int array[numberOfElements] = { 4,4,4,3,3,2,2,1,1,0,0,0 };
int prev = -1;
int count = 0;
for (int i=0; i<numberOfElements; i++) {
if (array[i] == prev) {
count++;
}
else {
if (count == 2) {
cout << "pair: " << prev << endl;
}
count=1;
prev = array[i];
}
}
if (prev >= 0 && count==2) {
cout << "pair: " << prev << endl;
}
You can do that in one pass and use binary search for efficiency:
int arr[] = { 4,4,4,3,3,2,2,1,1,0,0,0 };
int twins = 0;
for( auto i = std::begin( arr ); i != std::end( arr ); ) {
auto next = std::upper_bound( i, std::end( arr ), *i, std::greater<int>() );
if( std::distance( i, next ) == 2 ) ++twins;
i = next;
}
Live example
In case there are not too many duplicates in the array std::upper_bound could be not efficient and can be easily replaced:
auto next = std::find_if( std::next( i ), std::end( arr ), [i]( int n ) { return *i != n; } );
Solution without using additional array:
int twins_counter = 0;
for (int i = 0; i < dm; i++)
{
int counter = 0; // counter for elements
for (int j = 0; j < dm; j++)
{
if (arr[i] == arr[j])
{
if( j < i )
{
break; // we have searched twin for this element already
}
counter++;
if( counter > 2 )
{
break; // we meet element third time, it isn't twin
}
}
}
if( counter == 2 )
{
twins_counter++;
}
};
For sorted (upwards or downwards) arrays one cycle is enough:
int twins_counter = 0;
int counter = 1;
for (int i = 1; i < dm; i++)
{
if( arr[i] == arr[i-1] )
{
counter++;
}
else
{
if( counter == 2 )
{
twins_counter++;
counter = 1;
}
}
}
// check last value
if( counter == 2 )
{
twins_counter++;
}

Finding the mode of a sorted array using bin search

Hi there i've been tasked with Writing a simple program that is given an array of integers and determines the mode, which is the number that appears most frequently in the array.
The approach i'm trying to adopt is using a bubble sort with a bin search algorithm my level of knowledge is at a beginner stage can someone help point me?
Where i'm going wrong i believe it to be passing the correct search value to find it in the array! But i maybe wrong but some help would be very much appreciated, thanks in advance for those to take time to try help me.
#include <iostream>
using namespace std;
const int arrayLength = 10;
int searchVal;
int numGroup[arrayLength];
bool isSorted(int [], int arrayLength);
int binSearch(int [],int arrayLegth,int searchVal);
int main()
{
// Take in num
for (int index = 0; index < arrayLength; index++)
{
cout << "Enter Number: ";
cin >> numGroup[index];
}
// Sort numbers
//var to hold the val being swaped
int swapHolder = 0;
//bubble sort
for (int iSort = 0; iSort < arrayLength; iSort++)
{
for (int jSort = (iSort + 1); jSort <= arrayLength - 1; jSort++)
{
if (numGroup[iSort] > numGroup[jSort])
{
swapHolder = numGroup[iSort];
numGroup[iSort] = numGroup[jSort];
numGroup[jSort] = swapHolder;
}
}
}
//passes the sorted array and the length to the isSorted
isSorted(numGroup, arrayLength);
return 0;
}
bool isSorted(int numGroup[], int arrayLength){
cout << "Final result" << endl;
for (int index = 0; index < arrayLength - 1 ; index++)
{
if (numGroup[index] > numGroup[index + 1])
{
cout << "it's false";
system("pause");
return false;
}
cout << numGroup[index] << endl;
//cout << arrayLength << endl;
}
cout << numGroup[arrayLength - 1] << endl;
//trying to make searchVal
for (int i = 0; i < numGroup[arrayLength - 1]; i++)
{
if (numGroup[i] == numGroup[i])
{
int searchVal = numGroup[i];
}
}
binSearch(numGroup, arrayLength, searchVal);
cout << "It's true ";
system("pause");
return true;
}
int binSearch(int numGroup[], int arrayLength,int searchVal){
int low = 0;
int high = arrayLength - 1;
int mid;
while (low <= high)
{
mid = (low + high) / 2;
//search through the array
if (searchVal == numGroup[mid])
{
return mid;
}
else if (searchVal > numGroup[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
cout << "In bin search " << mid;
return mid;
}
You don't need to sort the array. You can have another array (freq) which will count the numbers appearances. So, a mini code for that:
int myArray[10];
int freq[1000]; //we assume that the numbers are smaller than 1000
void count()
{
for(int i = 0; i < 10; ++i)
{
++freq[v[i]];
}
}
int ReturnModeElement()
{
int maxFreq = -1;
int element = -1;
for(int i = 0 ; i < 10; ++i)
{
if(freq[v[i]] > maxFreq)
{
maxFreq = freq[v[i]];
element = v[i];
}
}
return element;
}
I hope you got the idea :)

LRU c++ program

I've been working on a program in one of my college classes. I have been having trouble with the implementation of my LRU code as it is not displaying any errors or anything, but compiles. There are two parts. The main that we input the values into, which we then specify which algorithm we want to use to find page faults. I know the main works, along with the FIFO algorithm, but I'm not getting anything with my LRU code (It compiles and "runs" but displays nothing as if I did not click to use the algorithm). Can anyone help me figure out what is wrong?
main.cpp
#include <iostream>
#include <string>
//#include "fifo.cpp"
#include "lru.cpp"
//#include "optimal.cpp"
using namespace std;
int main() {
// List of different variables
string pagestring;
int fs,pn[50], n;
// Prompt for page references
cout<<"Virtual Memory Simulation\nBy blah\n----------\nEnter the number of pages : " << endl;
cin >> n;
cout<<"\n-------------\nPlease enter a list of page numbers separated by commas.\n"<< endl;
cin>>pagestring;
// algorithm to use
char algo;
while (true) {
// Prompt algorithm to use
cout<<"----------\nPlease select an algorithm to use.\n\n1: First-In-First-Out (FIFO)\n2: Least-Recently-Used (LRU)\n3: Optimal\n0: Quit\n"<<endl;
cin>>algo;
if (algo == '1') {
//fifo(pagestring);
}
else if (algo == '2'){
LRU_Execute(pagestring, n);
}
else if (algo == '3'){
cout<<"Optimal Not yet coded"<<endl;
}
else if (algo == '0'){
break;
}
else {
cout<<"Invalid choice. Please try again."<<endl;
}
}
cout<<"Goodbye!!"<<endl;
};
LRU.cpp
#include <iostream>
#include <string>
using namespace std;
class pra
{
int fs,z;
int frame[50], frame1[50][2], pn[50], n, cnt, p, x;
public:
pra();
void init(string pagestring);
void getdata(string pagestring, int n);
void lru(int* pn, int n, string pagestring);
};
pra::pra()
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::init(string pagestring)
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::getdata(string pagestring, int n)
{
fs=3;
// index to loop through input string
int i = 0;
// current input string character
char z = pagestring[i];
int x = 0;
//cout << "\nEnter the page numbers : ";
while (z != '\0'){
// skip over commas and spaces
if (!(z == ',')) {
pn[x] = z;
x++;
// cout<<pn[x]<<"-This is pn[x]\n";
}
z = pagestring[++i];
}
//cout<<pn[x]<<"-This is pn[x] AGAIN\n";
this->lru(pn, n, pagestring);
}
void pra::lru(int* pn, int n, string pagestring)
{
init(pagestring);
int ind = 0, fault = 0, pi = 0, j, fn;
char i, z;
p = 0;
cnt = 0;
int min;
cout<<n<<"---"<<i<<" - "<<j<<" - "<<" - "<<fn<<" - "<<z;
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
pi = 0;
for (i = 0; i < n; i++)
{
j = 0;
if (ind > fs - 1)
ind = 0;
fault = 1;
min = 999;
while (j < fs)
{
if (frame1[j][0] = pn[pi])
{
fault = 0;
p++;
frame1[j][1] = p;
goto l2;
}
if (frame1[j][1] < min)
{
min = frame1[j][1];
fn = j;
}
j++;
}
j = 0;
while (j < fs)
{
if (frame1[j][0] = -1)
{
fault = 1;
fn = j;
goto l2;
}
j++;
}
ind++;
l2:
if (fault == 1)
{
p++;
frame1[fn][0] = pn[pi];
frame1[fn][1] = p;
cnt++;
}
cout << "\nElement: " << pn[pi];
pi++;
for (z = 0; z < fs; z++)
{
cout << "\t" << frame1[z][0];
}
if (fault == 1)
cout << "\t**Page Fault**";
else
cout << "\t--No Page Fault--";
}
cout << "\nTotal number of page faults: " << cnt;
cout << "\n";
}
void LRU_Execute(string pagestring, int n)
{
pra p;
int j, fault = 0, i, pi, z, fn, ind = 0, ans, ch;
p.getdata(pagestring, n);
//p.lru();
while (ans == 1);
//return 1;
}