97,506 views
4 votes
4 votes
Complete the function ConvertToFeetAndInches to convert totalInches to feet and inches. Return feet and inches using the HeightFtIn struct. Ex: 14 inches is 2 feet and 2 inches.

#include


typedef struct HeightFtIn_struct {

int feet;

int inches;

} HeightFtIn;


HeightFtIn ConvertToFeetAndInches(int totalInches) {

HeightFtIn tempVal;


/* Your solution goes here */


}


int main(void) {

HeightFtIn studentHeight;

int totalInches;


scanf("%d", &totalInches);

studentHeight = ConvertToFeetAndInches(totalInches);

printf("%d feet and %d inches\\", studentHeight.feet, studentHeight.inches);


return 0;

}

User Craika
by
3.6k points

2 Answers

6 votes
6 votes

Answer:

The solution to this question with code explaination is given below in explanation section.

Step-by-step explanation:

#include <iostream>

typedef struct HeightFtIn_struct {// declaring struct for feet and inches

int feet;

int inches;

} HeightFtIn;

HeightFtIn ConvertToFeetAndInches(int totalInches) {// function to convert to feet and in inches from total inches

int inches = totalInches;// declare inches local variable in ConvertToFeetAndInches function.

int feet = inches/12;// convert total inches into feet;

int leftInches = inches%12;// remaining inches after converting total inches into feet

HeightFtIn tempVal;// declare HeightFtIn_struct variable to store the value of inches and feets.

tempVal.feet = feet;// store feet value

tempVal.inches = leftInches;// store remaining inches after converting totalInches into feet

return tempVal;// return feets and inches.

/* Your solution goes here */

}

int main(void) {

HeightFtIn studentHeight; // declare HeightFtIn_struct variable studentHeight;

int totalInches;// declaring totalInches variable.

scanf("%d", &totalInches);// prompt user to enter totalInches;

studentHeight = ConvertToFeetAndInches(totalInches);// ConvertToFeetAndInches

printf("%d feet and %d inches\\", studentHeight.feet, studentHeight.inches);//display converted feet and inches from totalInches;

return 0;

}

User Adutra
by
3.8k points
1 vote
1 vote

Answer:LengthFtIn ConvertToFeetAndInches(int totalInches) {

LengthFtIn tempVal;

int inchesVal = totalInches;

int feetVal = inchesVal / 12;

tempVal.feetVal = feetVal;

tempVal.inchesVal = inchesVal % 12;

return tempVal;

}

Explanation: int InchesVal = totalInches; // makes totalInches a local variable.

int feetVal = inchesVal / 12; // divides the total inches by 12 to get feet.

tempVal.feetVal = feetVal; // saves feetVal.

tempVal.inchesVal = inchesVal % 12; divides the remainder.

User Thomas Glick
by
3.0k points