SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Procedural Abstraction And Functions That Return A Value
Exercise:
Self-test Exercises
Question:22 | ISBN:9780321531346 | Edition: 7

Question

The following function is supposed to take as arguments a length

expressed in feet and inches and return the total number of inches in that

many feet and inches. For example, total_inches(1, 2) is supposed to

return 14, because 1 foot and 2 inches is the same as 14 inches. Will the

following function perform correctly? If not, why not?

double total_inches(int feet, int inches)

{

inches = 12*feet + inches;

return inches;

}

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

The provided function will perform correctly and return the total number of inches in the given feet and inches. The function takes two arguments, feet and inches, and converts them into a single value representing the total inches.

Let's analyze the function step by step:

  1. inches = 12 * feet + inches;: This line calculates the total number of inches by multiplying the number of feet by 12 (since there are 12 inches in a foot) and then adding the remaining inches.

  2. return inches;: The function returns the calculated total inches.

For example, if you call the function with total_inches(1, 2), it will calculate the total inches as 14 (1 foot * 12 inches/foot + 2 inches) and correctly return the value 14.

So, yes, the provided function will perform correctly and return the total number of inches in the given feet and inches.

0 0

Discussions

Post the discussion to improve the above solution.