| | |
writing functions.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
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
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
Have you tried to compile?
Your header file needs the statement "#endif" at the end. The inclusion detection system works in this way:
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.
Your header file needs the statement "#endif" at the end. The inclusion detection system works in this way:
C++ Syntax (Toggle Plain Text)
#ifndef FUNCTION_H #define FUNCTION_H // Put all your declaration here #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.
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
okay i fixed it now thank you.... although I am still very much confused on my driver function
•
•
•
•
/**************************************
**********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.
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.
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
/********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
Last edited by jazzyangelz; Oct 20th, 2008 at 10:22 pm.
The parentheses are wrong. Look closely:
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.
C++ Syntax (Toggle Plain Text)
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.
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
Your right this is the wrong formula. Is this correct?
hypotenuse=
c=((a*a)+(b*b))
c2=c*c
hypotenuse=
c=((a*a)+(b*b))
c2=c*c
•
•
•
•
/********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
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.
For example:
c = sqrt(x);
You will need to include the header file <math.h> to use this function though.
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
![]() |
Similar Threads
- Writing data into a file (C)
- Need help writing a program (C++)
- writing a class without the class declaration? (C++)
- Help Writing Fuction to Read Fractions From User (C++)
- Automatically writing webpages by submitting a form... (PHP)
- How to write FNVAL functions (Java)
- writing a script (Shell Scripting)
- access Digital Camera Functions (C++)
Other Threads in the C++ Forum
- Previous Thread: working with stringstream
- Next Thread: Linked List of Pointers
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





