-
writing functions
I have two small programs dealing with writing functions. I'm not not skilled at writing functions. for the second assignment, what type of recursive function can i use? Can someone give me an example of both types of functions that I have to write.
Exercise 1:
Write a function called celsToFahr that takes in as a parameter a Celsius temperature (type double) and returns the corresponding Fahrenheit temperature (also type double). Recall that the matehmatical formula for converting temperatures between these scales is:
F = 9 / 5 * C + 32
To test this function, write a main() routine that asks the user for a celsius temperature, and then prints out a chart that illustrates the celsius to fahrenheit conversions for 10 celsius temperatures starting at the entered value and incrementing the celsius temperature by 1 each time. See the sample output for clarification.
Sample run 1: (user input underlined)
Enter a celsius temperature: 40
Cels. Fahr.
------------------------------------------------------
40 104
41 105.8
42 107.6
43 109.4
44 111.2
45 113
46 114.8
47 116.6
48 118.4
49 120.2
Sample run 2: (user input underlined)
Enter a celsius temperature: 12.54
Cels. Fahr.
------------------------------------------------------
12.54 54.572
13.54 56.372
14.54 58.172
15.54 59.972
16.54 61.772
17.54 63.572
18.54 65.372
19.54 67.172
20.54 68.972
21.54 70.772
--------------------------------------------------------------------------------
Exercise 2
Write a recursive function called GCD() that takes in two integer parameters and returns the greatest common divisor. The recursive routine should be based on the following recursive definition of GCD (Djikstra's algorithm):
GCD(m,n) is m, if m equals n.
GCD(m,n) is GCD(m - n, n) if m is greater than n.
GCD(m,n) is GCD(m, n - m) otherwise.
Test your function by writing a main() routine that asks the user to input two positive integers and then calls the GCD method and prints the result.
Sample run:
Input first number : 45
Input second number : 10
GCD(45, 10) = 5
-
You should try it on your own, but for now here is some pseudo code as a hint:
1.
Code:
int main()
{
while loop, as you have two conditions{
ask for input
put input in an int variable
send that variable to a function that does the calculation
}
return 0;
}
void celstofahr( double )
{
for loop
{
do calculations along with print out
}
}
try this and then post your code if you have anymore problems...for the recursive one just have the same operation done, check if the result is a GCD, if not call the function again until the result is a GCD.
axon
-
Code:
int main()
{
while loop, as you have two conditions{
ask for input
put input in an int variable
send that variable to a function that does the calculation
}
return 0;
}
void celstofahr( double )
{
for loop
{
do calculations along with print out
}
}
Code:
#define QUIT 666
void manipulate( int, int );
void main()
{
int *number= new int;
do {
cout << "Input a number: ";
cin >> *number;
manipulate( *number, 3 );
}while(*number!= QUIT );
delete number;
}
void manipulate( int number, int cliMAX )
{
for(int i= 0; i<cliMAX; i++)
{
cout << number*i << '\n';
}
}
Something like that.
-
Quote:
int *number= new int;
Why do this? It doesn't seem necessary.
-
hahah, I know but I felt like it for some reason. It is the same as using int number;
It's harmful either way, but yes it is un-necessary to use the pointer version.........for some reason.
-
The best kind of function is modular, returns something useful, uses no global variables or unnecessary dynamic allocations, and - if it is a calculatory function - no input/output directly from/to the user. Take some simple mathematical functions:
Code:
double add(double a, double b)
{
return a+b;
}
double divide(double a, double b)
{
if(b == 0) // prevent divide-by-zero
return 0;
else
return a/b;
}
double diagonal(double s1, double s2)
{
return sqrt((s1*s1)+(s2*s2));
}
That would be considered good form. Insulate the calculations from the user-interface.
Anyway, this is homework, so let's see some of your efforts.