Data Structure Keyword Typedef struct Distance{ int feet; float inch; ): typedef struct Distan int feet; float inch: } distances: int main() { struct Distance d1, d2; int main() { distances d1. d2: Modify the code to use typedef when declaring a structure.
#include <stdio.h>
#include<stdlib.h>struct Distance
{
int feet;
float inch;
} dist1, dist2, sum;int main()
{
struct Distance* distance1 = NULL;
struct Distance* distance2 = NULL;
struct Distance* distance3 = NULL;
distance1 = (struct Distance*)malloc(sizeof(struct Distance*));
distance2 = (struct Distance*)malloc(sizeof(struct Distance*));
distance3 = (struct Distance*)malloc(sizeof(struct Distance*));
printf("input feet for distance no.1:");
scanf("%d", &distance1->feet); printf("input inch for distance no.1:");
scanf("%f", &distance1->inch); printf("input feet for distance no.2:");
scanf("%d", &distance2->feet); printf("input inch for distance no.2:");
scanf("%f", &distance2->inch); printf("input feet for distance no.3:");
scanf("%d", &distance3->feet); printf("input inch for distance no.3:");
scanf("%f", &distance3->inch); printf("1st distance\n");
printf("Enter feet: ");
scanf("%d", &dist1.feet); printf("Enter inch: ");
scanf("%f", &dist1.inch);
printf("2nd distance\n"); printf("Enter feet: ");
scanf("%d", &dist2.feet); printf("Enter inch: ");
scanf("%f", &dist2.inch); // adding feet
sum.feet = dist1.feet + dist2.feet;
// adding inches
sum.inch = dist1.inch + dist2.inch; // changing to feet if inch is greater than 12
while (sum.inch >= 12)
{
++sum.feet;
sum.inch = sum.inch - 12;
} printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch);
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images