I was given this code by my professor


#include "home.12.h"

int main()

	cout << mystery(30) << endl;

	return SUCCESS;

} // end of main



#include "home.12.h"

int search(int first, int last, int n){

	int returnValue;
	cout << "Enter: first = " << first << " last = " << last << endl << endl;

	int mid = (first + last) / 2;

	if( ( mid * mid <= n ) && ( n < ( mid + 1 ) * ( mid + 1 ) ) ){

		returnValue = mid;


	else if( mid * mid > n )
		returnValue = search( first, mid-1, n );

		returnValue = search( mid+1, last, n );

	cout << "Leave: first = " << first << " last = " << last << endl << endl;

	return returnValue;

}   // end search


int mystery( int n ){

return search( 1, n, n );

}   // end mystery

I have attached an example of the output that is produced with this code. The question the teacher asked is 'What is the output?' I believe she wants the output explain out in english and not just the command prompt window. I do not know how to explain this output out, so that is my question what is this output showing?

8 Years
Discussion Span
Last Post by buddha527

I think you need to attach "home12.h" for us to be able to run it and help. Also, do you need to #include the "functions.cpp" file in main.cpp in order to call the "mystery" function from main?


#include <iostream>

using std::cout;
using std::cin;
using std::endl;

const int SUCCESS = 0; 

int search(int first, int last, int n);

int mystery(int n);

There is the header file. No you do not need to #include "functions.cpp" in the main.cpp, as far as I know. I just have both the main.cpp and function.cpp as source files and the home.12.h as a header file and it compiles 100%.


Looks to me like it is calculating a square root through an iterative process. It is probably implementing a mathematical algorithm to solve for square roots. Not sure which algorithm.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.