Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Explanation / Answer

//include input/output library, as well as algorithm and fstream (read from data files) #include #include #include //shortcut to be able to use cout using namespace std; //method signature to do binary search iteratively: Binary search searches through an //ordered list and finds the position of a given value based on chopping the array in //half at each iteration unsigned int binarySearch(double v[], unsigned int n, double value) { //set pointers: low = beginning or arrya, n = ending unsigned int low = 0, high = n; //while pointers are still apart - low moves from left to right and high moves from //right to left towards each other while (low numbers[n]) //increase our index, so that the next value is placed in the next position n++; // Here in.eof() and/or in.fail() returned true sort(numbers, numbers+n); // array must be sorted //Messages to read in the number from the user to search for cout