lab 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)all in the document bellow
lab_9___learning_functions_2.pdf

Unformatted Attachment Preview

Don't use plagiarized sources. Get Your Custom Essay on
lab assignment
Just from $13/Page
Order Essay

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

GradeAcers
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

Order your essay today and save 15% with the discount code DISCOUNT15