Define a struct type with the name Length that represents a

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.

Solution

#include <stdio.h>

struct Length //structure Length
{
   int yards;
   int feet;
   int inches;
};

struct Length add(struct Length,struct Length); //prototypes for functions
void show(struct Length);
  

int main(void)
{
struct Length l1,l2,l3;

printf(\"\ Enter Length in Yards ,feet and inches of l1\");
scanf(\"%d %d %d\",&l1.yards,&l1.feet,&l1.inches);

printf(\"\ Enter Length in Yards ,feet and inches of l2\");
scanf(\"%d %d %d\",&l2.yards,&l2.feet,&l2.inches);

l3 = add(l1,l2); //adding two structure variables and assign the sum to another structure variable
show(l3);

  
   return 0;
}

struct Length add(struct Length l1,struct Length l2) //add function
{
   struct Length temp;
   temp.yards=l1.yards+l2.yards;
   temp.feet = l1.feet+l2.feet;
   temp.inches =l1.inches+l2.inches;
   return temp;
}

void show(struct Length l) //show function
{
   printf(\"\ %d yards\",l.yards);
   printf(\"\ %d feet\",l.feet);
   printf(\"\ %d inches\",l.inches);
}

output:

 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site