I have some code here that I need detailed comments on just about every line so
ID: 3640722 • Letter: I
Question
I have some code here that I need detailed comments on just about every line so I can use it to study off of.The best way is to assume that I don't know anything. Even variable declaration and just add comments to all line. It doesn't have to be long comments just as long as I or anyone can see what each line is doing.
Those that help completely I will give full helpful review
---------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
unsigned int binarySearch(double v[], unsigned int n, double value)
{
unsigned int low = 0, high = n;
while (low < high) {
unsigned int mid = (low + high) / 2;
if (v[mid] < value)
low = mid + 1;
else
high = mid;
}
return low;
}
main()
{
string file = "binarySearch.input";
ifstream in(file.c_str());
double numbers[100];
int n = 0; // n is number of numbers
double item; // search item
while (in >> numbers[n])
n++;
// Here in.eof() and/or in.fail() returned true
sort(numbers, numbers+n); // array must be sorted
cout << "Enter a number to search for: ";
cin >> item;
if (cin.fail()) {
cout << "Enter a number. ";
exit(1);
}
cout << binarySearch(numbers, n, item) << endl;
}
----------------------------------------------------------------------------
// code 2 recursion
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
unsigned int binSearch(double v[], int low, int high, double value)
{
if (low >= high)
return low;
unsigned int mid = (low + high)/2;
if (value == v[mid])
return mid;
if (value < v[mid])
return binSearch(v, low, mid, value);
else
return binSearch(v, mid+1, high, value);
}
unsigned int binarySearch(double v[], unsigned int n, double value)
{
return binSearch(v, 0, n, value);
}
main()
{
string file = "binarySearch.input";
ifstream in(file.c_str());
double numbers[100];
int n = 0; // n is number of numbers
double item; // search item
while (in >> numbers[n])
n++;
// Here in.eof() and/or in.fail() returned true
sort(numbers, numbers+n); // array must be sorted
cout << "Enter a number to search for: ";
cin >> item;
if (cin.fail()) {
cout << "Enter a number. ";
exit(1);
}
cout << binarySearch(numbers, n, item) << endl;
}