writing functions

This is a discussion on writing functions within the C++ Programming forums, part of the General Programming Boards category; I have two small programs dealing with writing functions. I'm not not skilled at writing functions. for the second assignment, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    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

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    int *number= new int;
    Why do this? It doesn't seem necessary.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    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.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where to put local auxiliary functions?
    By draugr in forum C++ Programming
    Replies: 10
    Last Post: 03-17-2009, 08:46 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 06:05 PM
  3. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  4. newb question about interpreting and writing functions
    By crazychile in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 07:51 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21