write a C program that As a simple first exercise with struc

write a C program that:

As a simple first exercise with structs, we will create a program that adds two distances, where the distances are defined in terms of feet and inches. Define the template for a struct with the tag of distance The struct has two members, an integer called feet and a double called inches. Declare three variables using the struct template: d1, d2, and sumOfDistances. You can do this as part of the struct definition or do it within main. Within main, use scanf () to enter values for the feet and inches of both d1 and d2. Then calculate the sum of d1 and d2 (feet and inches). Note if inches ends up being bigger than 12, the value of feet should be adjusted appropriately. For example, if the sum ended up as 5 feet 18 inches, it should be re-expressed as 6 feet 6 inches. Print out the resulting sum.

Solution

#include<stdio.h>

struct distance{// a template for a struct witha tag of distance
int feet; //two members of struct feet and inches
double inches;

}d1,d2,sumOfDistances;//declaration of 3 variables of struct
int main(){
int totalfeet;
double totalinches;
printf(\"enter feet for d1\ \"); // asking user to enter value of feet for d1
  
scanf(\"%d\",&d1.feet);
printf(\"enter inches for d1\ \");// asking user to enter value of inches for d1
scanf(\"%lf\",&d1.inches);
  
printf(\"enter feet for d2\ \"); // asking user to enter value of feet for d2
scanf(\"%d\",&d2.feet);
  
printf(\"enter inches for d1\ \");// asking user to enter value of inches for d2
scanf(\"%lf\",&d2.inches);
  
totalfeet=d1.feet+d2.feet; // calculating sum of 2 distances
totalinches=d1.inches+d2.inches;
if(totalinches>=12){ // if inches greater than 12 then adjusting appropriately
  
totalfeet++;
totalinches=totalinches-12;   
}
sumOfDistances.feet=totalfeet;
sumOfDistances.inches=totalinches;
printf(\"%d\",sumOfDistances.feet); // printing sum
printf(\"feets\");
printf(\"%lf\",sumOfDistances.inches);
printf(\"inches\");
  
return 0;
  
}

write a C program that: As a simple first exercise with structs, we will create a program that adds two distances, where the distances are defined in terms of f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site