Solved by verified expert:Problem Definition: Write a function that receives a weight in pounds and ounces and returns the corresponding weight in grams. (1 oz = 28.249527 g)all in the document bellow
lab_9___learning_functions_2.pdf

Unformatted Attachment Preview

CSCI 180 Lab 9
Objectives:
1.
2.
3.
4.
To learn how functions work
To learn how to write functions
To learn how to write function prototypes.
To learn how to write function calls.
Laboratory Assignment:
Problem Definition: Write a function that receives a weight in pounds and ounces and returns the
corresponding weight in grams. (1 oz = 28.249527 g)
In order to do this we need to write an algorithm for this. Note the following:
• Functions perform one main idea (not multiple ideas).
• Keep functions simple such as the above problem definition.
• Functions can call other functions as well as itself which is called a recursive function.
• Function definitions are normally placed in libraries for reusability, but for now we will define these
functions below the main function call.
In this example this function will call another function to change pounds into ounces, so our algorithm will
reflect this.
1st function:
Step One: Write a comment to include the title of the function, a description of the function, parameters lists,
return value, any input, and any output for function pdsOzsToGrams.
/************************************************
* Function Name: pdsOzsToGrams
* Description: This function changes a weight in pounds and ounces into grams which is returned.
* Parameters: pds – represents pounds of float type, oz – represents ounces of float type.
* Return: gms – represents grams of float type.
* Input: none
* Output: none
************************************************/
Step Two: Write an algorithm for this function.
1) Define local variables.
2) Convert pounds to ounces.
(This will be a function call to another function which will receive pounds and return ounces. We will
hold off on defining this until later!).
3) Add converted pounds to previous ounces.
4) Multiply ounces by grams conversion.
1
5) Return grams value.
Step Three: Flush out algorithm.
//Define local variables
const float GCONVERT = 28.249527;
float gms;
//Convert pounds to ounces (a function call) and add returned converted pounds to previous ounces
oz += //function call to be filled in later!
//Multiply ounces by grams conversion
gms = oz * GCONVERT;
//Return grams value
return gms;
Step Four: Write function signature or heading. NOTE: The return type, parameter list, and function name
are the same as what was defined in the comment section. (See Step One!)
float pdsOzsToGrams (float pds, float ozs)
{
// function body
}
Replace // function body with the comments and code written in Step Three (flushed out section)!
Step Five: Write function prototype. The function prototype is for the compiler to match the types with the
function call and function definition at compile time. This is placed above the main function header. You
should have a comment section for this already in your code. Now it is time to place something in that
section. NOTE: This must match the function signature except without parameter identifiers! It is
acceptable to leave the identifiers but definitely not necessary!
float pdsOzsToGrams (float, float);
Step Six: Write function call. The function call uses the name from the function signature. Replace any
parameters with the appropriate identifiers. Assign to an identifier if returning a value which is needed.
NOTE: Some function calls, although they return a value, are not necessarily assigned to an identifier to be
later used such as the function call to printf(…)
//main driver program
int main (void)
{
// define variables
2
float grams, pounds, ounces;
//input from user for pounds and ounces
puts(“Please enter values for pounds and ounces: “);
scanf(“%f%f”, &pounds, &ounces);
//function call to convert pounds and ounces to grams.
grams = pdsOzsToGrams(pounds, ounces);
//normal end to program
return 0;
}
2nd function:
Now we will repeat the steps to write the function definition for converting pounds to ounces.
Step One:
/**********************************************************
* Function Name: pdsToOzs
* Description: This function converts pounds to ounces.
* Parameters: pds – represents pounds of float type.
* Return: ozs – represents ounces of float type.
* Input: none
* Output: none
************************************************/
Step Two: Write an algorithm for this function.
1) Define local variables
2) Convert pounds to ounces multiply pounds by 16
3) Return pounds value
Step Three: Flush out algorithm.
//Define local variables
const float PCONVERT = 16.0;
float ozs;
//Convert pounds to ounces multiply pounds by 16
ozs = pds * PCONVERT;
//Return ounces value
return ozs;
Step Four: Write function signature or heading.
float pdsToOzs (float pds)
3
{
// function body
}
Replace // function body with the comments and code written in Step Three!
Step Four: Write function prototype.
float pdsToOzs (float);
Step Five: Write function call. Return to previous function pdsOzsToGrams and replace comment with
function call.
//Convert pounds to ounces (a function call) and add returned converted pounds to previous ounces
oz += pdsToOzs (pds);
Final Complete Program:
#include
#include
float
float
pdsOzsToGrams (float, float);
pdsToOzs (float);
//main driver program
int main (void)
{
// define variables
float grams, pounds, ounces;
//input from user for pounds and ounces
puts(“Please enter values for pounds and ounces: “);
scanf(“%f%f”, &pounds, &ounces);
//function call to convert pounds and ounces to grams.
grams = pdsOzsToGrams(pounds, ounces);
}
//normal end to program
return 0;
float
{
pdsOzsToGrams (float pds, float ozs)
//Define local variables
const float GCONVERT = 28.249527;
float gms;
//Convert pounds to ounces
previous ounces
oz += pdsToOzs (pds);
(a function call) and
add returned converted pounds to
//Multiply ounces by grams conversion
gms = oz * GCONVERT;
//Return grams value
return gms;
4
}
float pdsToOzs (float pds)
{
//Define local variables
const float PCONVERT = 16.0;
float ozs;
//Convert pounds to ounces multiply pounds by 16
ozs = pds * PCONVERT;
}
//Return ounces value
return ozs;
Lab Assignment Instructions: Write the following functions which are called from the main function.
1) Write a function that receives a weight in grams and returns the corresponding weight in ounces. (1 g =
0.035274 oz)
2) Write a function that receives a measurement in yards, feet, and inches and returns the corresponding
measurement in centimeters. (1 in = 2.54001 cm)
3) Write a function check(x,y,n) that returns 1 if both x and y fall between 0 and 10. The function should return 0
otherwise. Assume that x, y, and n are all of type integer.
The main program should call the appropriate function according to the correct user input from the user. The
only method for the program stopping is if the user types the number 4.
5

Purchase answer to see full
attachment