Hello everyone,

I am trying to get a head start on this week's program, as it seems to be a long, albeit relatively easy, program. I wanted to double check with the experts here to make sure the flow of control for this program is correct. The assignment is as follows:


The ability to write functions is one of the more powerful capabilities in C++. Functions allow code to be reused, and provides a mechanism for introducing the concept of modularity to the programming process. In addition to the concept of functions, C++ allows complex programs to be organized using header files (.h files) that contain the prototypes of functions and implementation files that (.cpp files) that contain the implementation (definition) of the functions.

In this programming assignment you will be asked to implement a number of useful functions using a header file and an implementation file. You will place the prototypes for your function in a .h file, and implement these functions in a .cpp file. You will need to write a driver (a program designed to test programs) to test your functions before you upload them.

Deliverables:

* MyFunctions.h

* MyFunctions.cpp

* Driver.cpp

Function:

1. maximum

1. Precondition

i. two integer values exist

2. Postcondition

i. The value of the largest integer is returned.

ii. The original integers are unchanged

iii. If the integers have the same value then the value of either integer is returned.

3. Return

i. integer

4. Description

i. Function returns the value of the larger of two integers.

5. Prototype: int maximum ( int, int );

2. maximum

1. Precondition

i. two double values exist

2. Postcondition

i. The value of the larger double is returned.

ii. The original doubles are unchanged

iii. If the doubles have the same value then the value of either double is returned.

3. Return

i. double

4. Description

i. Function returns the value of the larger of two double.

5. Prototype: double maximum (double, double);


2. minimum

1. Precondition

i. two integer values exist

2. Postcondition

i. The value of the smallest integer is returned.

ii. The original integers are unchanged

iii. If the integers have the same value then the value of either integer is returned.

3. Return

i. integer

4. Description

i. Function returns the value of the smaller of two integers.

5. Prototype: int minimum ( int, int );

4. minimum

1. Precondition

i. two double values exist

2. Postcondition

i. The value of the smaller double is returned.

ii. The original doubles are unchanged

iii. If the doubles have the same value then the value of either double is returned.

3. Return

i. double

4. Description

i. Function returns the value of the smaller of two double.

5. Prototype: double minimum (double, double);

5. absolute

1. Precondition

i. Some integer value exists

2. Postcondition

i. Integer value is unchanged

3. Return

i. Integer

4. Description

i. This function returns the absolute value of an integer

5. Prototype: int absolute( int );

6. absolute

1. Precondition

i. Some double value exists

2. Postcondition

i. Double value is unchanged

3. Return

i. Double

4. Description

i. This function returns the absolute value of a double

5. Prototype: double absolute( double );


6. power

1. Precondition

i. Double value X and integer value Y exist

2. Postcondition

i. The value of X and Y are unchanged

3. Return

i. The double X raised to the power of Y

4. Description

i. This function will calculate XY

5. Prototype: double power ( double, int );

8. squareRoot

1. Precondition

i. Some integer radicand X exists

2. Postcondition

i. The value of the radicand X is unchanged

3. Return

i. The square root of X

ii. 0 if an error is encountered

4. Description

i. The function calculates the square root of a number using Newton’s method with 25 iterations.

5. Prototype: double squareRoot( double );

9. hypotenuse

1. Precondition

i. For some right triangle ABC where side a = side AB and b = side BC, a and b are positive doubleing point values.

2. Postcondition

i. The values of a and b are unchanged

3. Return

i. The length the hypotenuse of triangle ABC

4. Description

i. This function calculates the length of a hypotenuse given the length of the two sides of a right triangle.

5. Prototype: double hypotenuse ( double, double );

Newton's method for calculating the square root of N starts by making a (positive number) guess at the square root. It then uses the original guess to calculate a new guess, according to the following formula:

guess = (( N / guess) + guess) / 2 ;

No matter how wild the original guess is, if we repeat this calculation the algorithm will eventually find the square root of N.

So I know this is quite the mouthful, but this is what I am planning to do. Please correct me if I am wrong:

1. I need to create 2 source files and 1 header file. I'll name those MyFunctions.cpp, driver.cpp, and MyFunctions.h, respectively.

2. Define the prototypes of the functions in the header file, with each respective function calling the correct data type, and then verify function overloading.

3. In the driver I am going to test to see if the files open properly and then execute the intended operation using the relative values.

4. And then of course define the actual functions in the main source file, but this to me seems to be the toughest part. I am not sure where I should be using void function calls and where I should be just calling the function.

This is what I have so far (I am still in the experimental stages so I have some of these commented off):

MyFunctions.h

maximum(); 
maximum();
minimum();
minimum();
absolute();
absolute();
power();
squareRoot();
hypotenuse();

#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H



/*// prototpes go here

#ifndef FUNCTIONS_H // header guard (FILENAME_H)
#define FUNCTIONS_H // declare if undeclared, but will skip if already defined

#include <iostream>
using namespace std;

void printMenu(ostream &out);
int factorial(int);
bool isValid(double);  // function overloading
bool isValid(int);  // function overloading




#endif */

driver.cpp

#include "MyFunctions.h"
int main()
{

/*#include "functions.h"

int main()
{
	printMenu();

	if (factorial(5) == 120)
	{
		cout << "Great!" << endl;
	}
	else 
	{
		cout << "Failed" <<endl;
	}

	if(isValid(2))
	{
		cout << "great"<<endl;
	}
	else 
	{
		cout << "Failed" <<endl;
	}
}*/

etc. etc.

MyFunctions.cpp

#include "MyFunctions.h"

/*#include "functions.h" // the brackets tell the preprocessor to look in a specific location where " " describe locally

void printMenu();
{		// stubbed out
	out << "this is the menu" << endl;
	out << "1. Nothing" << endl;
	out << "2. Nothing" << endl;
	out << "3. Nothing" << endl;
	out << "4. Nothing" << endl;
}		// stubbed out
int factorial(int);
{
	int result = 1;

	while (num > 0)
	{
		sesult *= num;
		num--;
	}

	return result;

}
bool isValid(double);  // function overloading
{
	return static_cast<int>(val)%2 == 0
}
bool isValid(int);  // function overloading
{
	return val%2 == 0
}

*/

I know this was a whole lot to read, and I thank whoever is doing so because I want to make sure I have a complete understanding of what is going on. Thanks everyone.

I did not go through the entire post, but I notice your myfunction.h has something wrong. The first few lines of declaration did not state the return type and the required parameters. Also, the inclusion detection should be at the very beginning of the file.

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.