EmbLogic's Blog

adding two distances in feet and inches using functions and structure pointers

header file*********************************************************************

#include<stdio.h>
#include<stdlib.h>

struct distance
{    int feet;
int inches;
};
int display(struct distance *,struct distance *,struct distance *);
struct distance * add(struct distance *,struct distance *);
struct distance * input();

**************************************************************************

main file******************************************************************

#include”header.h”
int main()
{
struct distance *d1,*d2,*d3;
d1 = input();
d2 = input();
d3 = add(d1,d2);
display(d1,d2,d3);
return 0;

}
struct distance * input()
{
struct distance *d1;
d1 = (struct distance*)malloc(sizeof(struct distance));
printf(“\nenter the distance in feet”);
scanf(“%d”,&d1->feet);
printf(“\nenter the distance in inches”);
scanf(“%d”,&d1->inches);
return d1;

}
struct distance * add(struct distance *d1, struct distance *d2)
{
struct distance *d3;
d3 = (struct distance*)malloc(sizeof(struct distance));
d3->inches= d1->inches +d2->inches;
d3->feet = d1->feet +d2->feet;
if(d3->inches >= 12)
{    d3->feet++;
d3->inches-=12;
}
return d3;

}
int display(struct distance *d1,struct distance *d2,struct distance *d3)
{
printf(” %d\’ %d\” \n %d\’ %d\” \n —— \n%d\’ %d\” \n “,d1->feet,d1->inches,d2->feet,d2->inches,d3->feet,d3->inches);
}
****************************************************************************

output:::

enter the distance in feet6

enter the distance in inches7

enter the distance in feet6

enter the distance in inches7
6′ 7″
6′ 7″
——
13′ 2″

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>