Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define a struct type with the name Length that represents a length in yards, fee

ID: 3779736 • 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.

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
{
// Your data definition here
int yards;
int feet;
int inches;
};

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;
sum.yards = first.yards + second.yards;
sum.feet = first.feet + second.feet;
sum.inches = first.inches + second.inches;

return sum;
}
void show(struct Length length)
{
printf("%d yards %d feet %d inches", length.yards, length.feet, length.inches /* Three values here*/ );
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                           

Enter a length in yards, feet, and inches: 2 3 4                                                                                                                                                                                                       

Do you want to enter another(y or n)?: y                                                                                                                                                                                                               

Enter a length in yards, feet, and inches: 3 4 5                                                                                                                                                                                                       

Do you want to enter another(y or n)?: n                                                                                                                                                                                                               

The total of all the lengths is: 5 yards 7 feet 9 inches

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote