Homework 8, An Introduction to Functions.

Short Description:

Modify a solution to homework 2 to include functions.

This assignment is worth 10 points.

Goals

When you finish this homework, you should have:

Formal Description

In this homework we will begin to learn about functions. There are a number of basic concepts you will be learning We will discuss all of these pieces in class, so this exercise is designed to provide a simple introduction and make you familiar with the practice of functions.

More details can be found in chapter 8 of your book.

  1. Get the starting source code file.
  2. Put your name and class information at the top of this program.
  3. Write a function to compute fingers from the values of fingers, palms, and cubits.
    1. Create the function prototype
      • At the top of the program after the constants but be fore main insert the following
      • int ConvertToFingers(int fingers, int palms, int cubits); 
      • Your code should look like this:
        const int GRAINS_PER_FINGER{1};
        
        int ConvertToFingers(int fingers, int palms, int cubits);
        
        int main() { 
      • This function will take three integer parameters fingers, palms and cubits.
      • And will return a single integer value.
    2. Create a function definition
      • This is below the end of main.
      • int ConvertToFingers(int fingers, int palms, int cubits) {
            int totalFingers{0};
        
            totalFingers = fingers + palms * FINGERS_PER_PALM +
                           cubits * PALMS_PER_CUBIT * FINGERS_PER_PALM;
        
            return totalFingers;
        } 
      • Your final code will look like this
            return 0;
        }
        
        int ConvertToFingers(int fingers, int palms, int cubits) {
            int totalFingers{0};
        
            totalFingers = fingers + palms * FINGERS_PER_PALM +
                           cubits * PALMS_PER_CUBIT * FINGERS_PER_PALM;
        
            return totalFingers;
        } 
      • Notice, the parameters are used in the computation.
      • But totalFingers is a local variable
        • The type matches the function return type.
        • And it is returned.
    3. Finally use function
      • Change the line
        startFingers = inputFingers + inputPalms * FINGERS_PER_PALM +
                     inputCubits * PALMS_PER_CUBIT * FINGERS_PER_PALM; 
      • To be
        startFingers = ConvertToFingers(inputFingers, inputPalms, inputCubits); 
      • Note that the variable names passed as arguments do not match the parameter names.
        • This is acceptable, they must match in position and type.
      • Change the line
        endFingers = inputFingers + inputPalms * FINGERS_PER_PALM +
                     inputCubits * PALMS_PER_CUBIT * FINGERS_PER_PALM; 
      • To be
        endFingers = ConvertToFingers(inputFingers, inputPalms, inputCubits); 
  4. Write a function to do the input.
    1. Create the function prototype
      • Functions in c/c++ can only return one thing, so we will need to make reference parameters
      • These are parameters that will change the value of the arguments.
      • They have a & between the type and the parameter name.
      • We will also pass a description for the prompts.
      • Since this function does not return a value, it will be a void function.
      • At the top of the program, by the other function prototype put
        void GetInput(int & fingers, int & palms, int & cubits, string pos); 
    2. Create the function definition
      • At the bottom of the file insert the following
        void GetInput(int & fingers, int & palms, int & cubits, string pos){
        
            cout << "Enter the " << pos << " cubits => ";
            cin >> cubits;
        
            cout << "Enter the " << pos << " palms => ";
            cin >> palms;
        
            cout << "Enter the " << pos << " fingers => ";
            cin >> fingers;
        
            return;
        } 
    3. Call the function.
      • Replace
        cout << "Enter the starting cubits => ";
        cin >> inputCubits;
        cout << "Enter the starting palms => ";
        cin >> inputPalms;
        cout << "Enter the starting fingers => ";
        cin >> inputFingers; 
      • With
        GetInput(inputFingers, inputPalms, inputCubits, "starting"); 
      • Replace
        cout << "Enter the ending cubits => ";
        cin >> inputCubits;
        cout << "Enter the ending palms => ";
        cin >> inputPalms;
        cout << "Enter the ending fingers => ";
        cin >> inputFingers; 
      • With
        GetInput(inputFingers, inputPalms, inputCubits, "ending"); 
  5. Write a function to reduce the units.
    1. Create the function prototype
      • This function will take one value (the total fingers)
      • And will return three values (finger, palms and cubits)
      • So the function prototype will be
        ReduceFingers(int totalFingers, int & fingers, int & palms, int & cubits); 
      • Add this function prototype at the top.
    2. Create the function definition
      • Add the following function at the bottom of the file
        void ReduceFingers(int totalFingers, int & fingers, int & palms, int & cubits){
            fingers = totalFingers % FINGERS_PER_PALM;
            totalFingers /= FINGERS_PER_PALM;
            palms = totalFingers % PALMS_PER_CUBIT;
            cubits = totalFingers / PALMS_PER_CUBIT;
        
            return;
        } 
    3. Call the function.
      • Replace the three repeated version of the code with a function call.
      • The first will be replace
        fingers = startFingers % FINGERS_PER_PALM;
        startFingers /= FINGERS_PER_PALM;
        palms = startFingers % PALMS_PER_CUBIT;
        cubits = startFingers / PALMS_PER_CUBIT; 
      • With
        ReduceFingers(startFingers, fingers, palms, cubits); 
      • Repeat this, make sure you get the first parameter correct.
  6. Write a function to do the output.
    1. Create the function prototype
      • This function will take the three units.
      • And print them out with labels.
      • It should not change anything so these will be value parameters.
      • It will not return anything, so the it will be a void function.
        void PrintUnits(int fingers, int palms, int cubits); 
    2. Create the function definition
      void PrintUnits(int fingers, int palms, int cubits){
          cout << cubits << " cubits "
               << palms << " palms "
               << fingers << " fingers";
      
          return;
      } 
    3. Call the function.
      • Replace all code that looks like this:
        cout << "Nebi and Ako started at: " << cubits << " cubits "
             << palms << " palms " << fingers << " fingers " << endl;
                       
      • With this
        cout << "Nebi and Ako started at: ";
        PrintUnits(fingers, palms, cubits);
        cout << endl; 
If you need a reference, my code is here

Required Files

A single source code file. The starting code with the modifications described in the exercise.

Submission

Submit the assignment to the D2L folder Homework 8 by the due date.