Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Department of electronics and communication engineering

Indian institute of information technology


Title:-program to add two distance in inch-feet using structure in C
submitted by:-
GUIDED BY: PROFESSOR:
Content:
 Problem ststement
 Introduction
 Algorithm
 Code
 Output
 Conclusion
 references
Problem statement

• Write a program using structure to add two distances in


inch-feet using structure. The values of the distance should
be taken from the user
Introduction:-

Here, in this paper we have tried to add two distances infeet inch with the help of
structure and the values are decided by the user.
For example:- if the two distances are two feet 11 inches and 3 feet 1 inch then upon
adding them we’ll get 6 feet 12 inches=1 feet.
Algorithm:-

• In this program, a structure distance containing two data members(inch and feet)
is declared to store the distance in the inch-feet system.
• The two structure variables d1 and d2 have beencreated to store the distance
specified by the user.
• The ‘sumofdistances’ variable store the sum of the distances.
• We know that 12 inch=1 feet so,in order to add the numbers properly we have to
declare an ‘if’ statement to convert the inches to feet if the value of inch
exceeds 12 inches.
• Then we also have to subtract ‘12’ from the ‘sumofdistances.feet’ to get the
correct value of addition of the two distances.
Pseudo code:-
#include<stdio.h>
#include<time.h>
Struct distance
{
Int feet;
Float inch;
}d1,d2,sumofdistances;
Int
Main()
{
Clock_t start, end;double cpu_time_used;
Start=clock();
Printf(“please enter the distance 1:\n”);
Printf(“enter the distance 1 in feet\n”);
Scanf(“%d”,&d1.feet);
Printf(“enter inch\n”);
Scanf(“%f”,&d1.inch);
printf ("Please enter the distance 2:\n");
printf ("Enter the distance 2 in feet\n");
scanf ("%d", &d2.feet);
printf ("Enter inch\n");
scanf ("%f", &d2.inch);
SumOfDistances.feet = d1.feet + d2.feet;
SumOfDistances.inch = d1.inch + d2.inch;
if (SumOfDistances.inch >= 12.0)
{
SumOfDistances.inch = SumOfDistances.inch - 12; +
+SumOfDistances.feet;
}
printf ("Sum of distances is : %d feet ", SumOfDistances.feet);
printf ("%f inch", SumOfDistances.inch);
end = clock ();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; printf
("Time taken to execute the code in seconds:\n"); printf ("%f",
cpu_time_used);
return 0;
}
Analysis:-
Step 1- First, we specify the two distances in feet -inch.
Step 2-The addition of the specified distances takes place according to the specified algorithm.
Output:-
Time complexity:-
CONCLUSION -

We can conclude that using the above algorithm we can take two distances in
the feet-inches format and get them added. We have also added an ‘if’
statement in order to produce the correct output.If the value of inch exceeds
12 then ‘1’ gets added to the feet and ’12- inch’ is what remains in the inch
part.

REFERENCES

https://www.programiz.com
https://softwareengineering.stackexchange.com

You might also like