
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add Two Distances in Inch-Feet System Using Structures in C++
A structure is a collection of items of different data types. It is very useful in creating complex data structures with different data type records. A structure is defined with the struct keyword.
An example of a structure is as follows:
struct DistanceFI { int feet; int inch; };
The above structure defines a distance in the form of feet and inches.
Example of Adding Two Distances (inch-feet) Using Structure
The program uses a structure named DistanceFI to represent a distance in terms of feet and inches. It creates two different variables of distances, adds their feet and inches separately, and if the total inches exceed 12, it converts the extra inches into feet to maintain proper distance. So, this way we can run the program.
#include <iostream> using namespace std; struct DistanceFI { int feet; int inch; }; int main() { struct DistanceFI distance1, distance2, distance3; distance1.feet = 5; distance1.inch = 8; distance2.feet = 3; distance2.inch = 11; distance3.feet = distance1.feet + distance2.feet; distance3.inch = distance1.inch + distance2.inch; if(distance3.inch > 12) { distance3.feet++; distance3.inch = distance3.inch - 12; } cout << endl << "Sum of both distances is " << distance3.feet << " feet and " << distance3.inch << " inches"; return 0; }
The above program produces the following result:
Sum of both distances is 9 feet and 7 inches
Advertisements