Define a struct type with the name Length that represents a length in yards, fee
ID: 3779913 • Letter: D
Question
Define a struct type with the name Length that represents a length in yards, feet, and inches. Define an add() function that will add two Length arguments and return the sum as type Length. Define a second function, show(), that will display the value of its Length argument.
Write a program that will use the Length type and the add() and show() functions to sum an arbitrary number of lengths in yards, feet, and inches that are entered from the keyboard and output the total length.
USE THE GIVEN DEFAULT TEMPLATE TO FINISH THE CODE. DO NOT MODIFY THE TEMPLATE. WRITE THE CODE IN C LANGUAGE. USE ALL VARIABLES GIVEN IN THE DEFAULT TEMPELATE.
GIVE CODE:
#include <stdio.h>
#include <ctype.h>
#define INCHES_PER_FOOT 12
#define FEET_PER_YARD 3
struct Length
{
// Your data definition here
};
struct Length add(struct Length first, struct Length second);
void show(struct Length length);
int main(void)
{
char answer = 'n';
struct Length length;
struct Length total = { 0,0,0};
int i = 0;
do {
printf("Enter a length in yards, feet, and inches: ");
scanf(" %d %d %d", &length.yards, &length.feet, &length.inches);
total = add(total,length);
printf("Do you want to enter another(y or n)?: ");
scanf(" %c", &answer);
fflush(stdin);
} while(tolower(answer) == 'y');
printf("The total of all the lengths is: ");
show(total);
printf(" ");
return 0;
}
struct Length add(struct Length first, struct Length second)
{
unsigned long inches = 0;
struct Length sum;
return sum;
}
void show(struct Length length)
{
printf("%d yards %d feet %d inches", /* Three values here*/ );
}
Explanation / Answer
#include <stdio.h>
#include <ctype.h>
#define INCHES_PER_FOOT 12
#define FEET_PER_YARD 3
struct Length
{
int inches;
int feet;
int yards;
};
struct Length add(struct Length first, struct Length second);
void show(struct Length length);
int main(void)
{
char answer = 'n';
struct Length length;
struct Length total = { 0,0,0};
int i = 0;
do {
printf("Enter a length in yards, feet, and inches: ");
scanf(" %d %d %d", &length.yards, &length.feet, &length.inches);
total = add(total,length);
printf("Do you want to enter another(y or n)?: ");
scanf(" %c", &answer);
fflush(stdin);
} while(tolower(answer) == 'y');
printf("The total of all the lengths is: ");
show(total);
printf(" ");
return 0;
}
struct Length add(struct Length first, struct Length second)
{
unsigned long inches = 0;
struct Length sum;
int temp=0;
int inch=0;
int fee=0;
temp=first.inches+second.inches;
if(temp>=INCHES_PER_FOOT)
{
inch=temp/INCHES_PER_FOOT;
sum.inches=temp%INCHES_PER_FOOT;
}
else
sum.inches=temp;
temp=0;
temp=first.feet+second.feet+inch;
if(temp>=FEET_PER_YARD)
{
fee=temp/FEET_PER_YARD;
sum.feet=temp%FEET_PER_YARD;
}
else
sum.feet=temp;
sum.yards=fee+first.yards+second.yards;
return sum;
}
void show(struct Length length)
{
printf("%d yards %d feet %d inches",length.yards,length.feet,length.inches );
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.