943,796 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 950
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 20th, 2008
0

writing functions.

Expand Post »
I need help writing functions. In this program I need to be able to implement a number of useful functions using a header file and an implementation file. I also need to place the prototypes for my function in a .h file, and implement these functions in a .cpp file. I also need to write a driver to test your functions before you upload them.

Deliverables:
MyFunctions.h
MyFunctions.cpp
Driver.cpp

Function:

1. maximum
a. Precondition
i. two integer values exist
b. 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.
c. Return
i. integer
d. Description
i. Function returns the value of the larger of two integers.
e. Prototype: int maximum ( int, int );



This is my headerfile so far.


/**************************************
headerfile
***************************************/

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <iostream>

using namespace std;

int maximum (int, int);
double maximum (double, double);
int minimum (int, int);
double minimum (double, double);
int absolute (int);
double absolute (double);
double power (double, int);
double squareRoot (double);
double hypotenuse(double, double);


and my driver file
/**************************************
driver.cpp
***************************************/


#include "function.h"
int main ()

{
if(intmaximum>int)
{
cout<<intmax;
}
else if(intmax==int)
{
cout<<intmax<<int;
}
else
cout<<"ERROR!"<<endl;




}

i would really appreciate some help. Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jazzyangelz is offline Offline
9 posts
since Oct 2008
Oct 20th, 2008
0

Re: writing functions.

Have you tried to compile?

Your header file needs the statement "#endif" at the end. The inclusion detection system works in this way:
C++ Syntax (Toggle Plain Text)
  1. #ifndef FUNCTION_H
  2. #define FUNCTION_H
  3.  
  4. // Put all your declaration here
  5.  
  6. #endif

The reason for doing this is to prevent the compiler from parsing the same header file more than once (thus preventing duplicated declaration) when compiling a source file. The #endif statement tells the preprocessor the end of the scope for this inclusion detection (similar to the closing brace } ).

Your driver function simply doesn't work.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 20th, 2008
0

Re: writing functions.

okay i fixed it now thank you.... although I am still very much confused on my driver function

Quote ...
/**************************************
**********header*****************************/

#ifndef FUNCTION_H
#define FUNCTION_H

#include <iostream>

using namespace std;

function prototypes
int maximum (int, int);
double maximum (double, double);
int minimum (int, int);
double minimum (double, double);
int absolute (int);
double absolute (double);
double power (double, int);
double squareRoot (double);
double hypotenuse(double, double);


#endif
Last edited by jazzyangelz; Oct 20th, 2008 at 9:54 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jazzyangelz is offline Offline
9 posts
since Oct 2008
Oct 20th, 2008
0

Re: writing functions.

Try writing out the function and post it here. The driver file is supposed to call the function, pass parameters to it, and gets return value (if any) back. It is used for the purpose of testing whether the functions work correctly.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 20th, 2008
0

Re: writing functions.

Quote ...
/********myfunctions.cpp**********/
#include "functions.h"


double hypotenuse(double a, double b)//function definition
{

double c;

c=(((a/c)+c)/2)+((b/c)+c)/2)));

return c;

}


/**************************************
driver.cpp
***************************************/


#include "functions.h"
int main ()
{
double a, b, c;
a=2.5;
b=3.5;
c=hypotenuse( a, b);//function call
cout<<hypotenuse<<endl;
return 0;

}

/**************************************
**********header*****************************/

#ifndef FUNCTION_H
#define FUNCTION_H

#include <iostream>

using namespace std;

//declarations
double hypotenuse(double, double);


#endif



okay so this is what I have so far. I removed the rest of the function prototypes to see if this worked. But I still get compile errors. Something is wrong with the parentheses. I am trying to get the absolute value of c the hypotenuse.
Last edited by jazzyangelz; Oct 20th, 2008 at 10:22 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jazzyangelz is offline Offline
9 posts
since Oct 2008
Oct 20th, 2008
0

Re: writing functions.

The parentheses are wrong. Look closely:

C++ Syntax (Toggle Plain Text)
  1. c=(((a/c)+c)/2)+((b/c)+c)/2)));

Also, are you sure that's the correct formula for calculating the hypotenuse? Why is variable "c" used in the formula for computing "c" ? Even if the formula is right, the variable "c" isn't initialized at all. And I am quite sure the formula itself is wrong.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 20th, 2008
0

Re: writing functions.

Your right this is the wrong formula. Is this correct?

hypotenuse=

c=((a*a)+(b*b))
c2=c*c

Quote ...
/********myfunctions.cpp**********/
#include "functions.h"


double hypotenuse(double a, double b)//function definition
{

double c, c2;

c=((a*a)+(b*b))
c2=c*c

return c2;

}


/**************************************
driver.cpp
***************************************/


#include "functions.h"
int main ()
{
double a, b, c, c2;
a=2.5;
b=3.5;
c2=hypotenuse( a, b);//function call
cout<<hypotenuse<<endl;
return 0;

}

/**************************************
**********header*****************************/

#ifndef FUNCTION_H
#define FUNCTION_H

#include <iostream>

using namespace std;

//declarations
double hypotenuse(double, double);


#endif
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jazzyangelz is offline Offline
9 posts
since Oct 2008
Oct 20th, 2008
0

Re: writing functions.

You should use the function sqrt() to find the square root of a variable.

For example:
c = sqrt(x);

You will need to include the header file <math.h> to use this function though.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 20th, 2008
0

Re: writing functions.

I wish we were allowed to use functions from the cmath library but were not. We have to come up with out own equations.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jazzyangelz is offline Offline
9 posts
since Oct 2008
Oct 21st, 2008
0

Re: writing functions.

I wish we were allowed to use functions from the cmath library but were not. We have to come up with out own equations.
maybe this would work..

double hypotenuse(double a, double b)
{
double R;
R = squareRoot(power(a,2) + power(b,2));
return R;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Trader09 is offline Offline
7 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: working with stringstream
Next Thread in C++ Forum Timeline: Linked List of Pointers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC