I was given this code by my professor

main.cpp

#include "home.12.h"

int main()
{

	cout << mystery(30) << endl;

	return SUCCESS;

} // end of main

//=========================================

functions.cpp

#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 );

	else
		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?

Recommended Answers

All 5 Replies

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?

hm, search in sorted array?

home.12.h

#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.

Okay thanks for your help really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.