Back-End Programming Exam  >  Back-End Programming Notes  >  Learn to Program with C++: Beginner to Expert  >  Programming Examples - File Structures

Programming Examples - File Structures | Learn to Program with C++: Beginner to Expert - Back-End Programming PDF Download

Example 1: C++ Program to Store Information of a Student in a Structure

This program stores the information (name, roll and marks entered by the user) of a student in a structure and displays it on the screen
Program: Store and Display Information Using Structure

#include <iostream>
using namespace std;
struct student
{
    char name[50];
    int roll;
    float marks;
};
int main()
{
    student s;
    cout << "Enter information," << endl;
    cout << "Enter name: ";
    cin >> s.name;
    cout << "Enter roll number: ";
    cin >> s.roll;
    cout << "Enter marks: ";
    cin >> s.marks;
    cout << "\nDisplaying Information," << endl;
    cout << "Name: " << s.name << endl;
    cout << "Roll: " << s.roll << endl;
    cout << "Marks: " << s.marks << endl;
    return 0;
}

Output
Enter information,
Enter name: Bill
Enter roll number: 4
Enter marks: 55.6
Displaying Information,
Name: Bill
Roll: 4
Marks: 55.6

In this program, student (structure) is created.
This structure has three members: name (string), roll (integer) and marks (float).
Then, a structure variable s is created to store information and display it on the screen.

Example 2: C++ Program to Add Two Distances (in inch-feet) System Using Structures
This program takes two distances (in inch-feet system), adds them and displays the result on the screen.
Program: Add Distances Using Structures

#include <iostream>
using namespace std;
struct Distance{
    int feet;
    float inch;
}d1 , d2, sum;
int main()
{
    cout << "Enter 1st distance," << endl;
    cout << "Enter feet: ";
    cin >> d1.feet;
    cout << "Enter inch: ";
    cin >> d1.inch;
    cout << "\nEnter information for 2nd distance" << endl;
    cout << "Enter feet: ";
    cin >> d2.feet;
    cout << "Enter inch: ";
    cin >> d2.inch;
    sum.feet = d1.feet+d2.feet;
    sum.inch = d1.inch+d2.inch;
    // changing to feet if inch is greater than 12
    if(sum.inch > 12)
    {
        ++ sum.feet;
        sum.inch -= 12;
    }
    cout << endl << "Sum of distances = " << sum.feet << " feet  " << sum.inch << " inches";
   return 0;
}

Output
Enter 1st distance,
Enter feet: 6
Enter inch: 3.4
Enter information for 2nd distance
Enter feet: 5
Enter inch: 10.2
Sum of distances = 12 feet  1.6 inches

In this program, a structure Distance containing two data members (inch and feet) is declared to store the distance in inch-feet system.
Here, two structure variables d1 and d2 are created to store the distance entered by the user. And, the sum variables stores the sum of the distances.
The if..else statement is used to convert inches to feet if the value of inch of sum variable is greater than 12


Example 3: C++ Program to Add Complex Numbers by Passing Structure to a Function
This program takes two complex numbers as structures and adds them with the use of functions.
Program: Source Code to Add Two Complex Numbers

// Complex numbers are entered by the user


#include <iostream>

using namespace std;


typedef struct complex
{
    float real;
    float imag;
} complexNumber;
complexNumber addComplexNumbers(complex, complex);
int main()
{
    complexNumber n1, n2, temporaryNumber;
    char signOfImag;
    cout << "For 1st complex number," << endl;
    cout << "Enter real and imaginary parts respectively:" << endl;
    cin >> n1.real >> n1.imag;
    cout << endl << "For 2nd complex number," << endl;
    cout << "Enter real and imaginary parts respectively:" << endl;
    cin >> n2.real >> n2.imag;
    signOfImag = (temporaryNumber.imag > 0) ? '+' : '-';
    temporaryNumber.imag = (temporaryNumber.imag > 0) ? temporaryNumber.imag : -temporaryNumber.imag;
    temporaryNumber = addComplexNumbers(n1, n2);    
    cout << "Sum = "  << temporaryNumber.real << temporaryNumber.imag << "i";
    return 0;
}
complexNumber addComplexNumbers(complex n1,complex n2)
{
      complex temp;
      temp.real = n1.real+n2.real;
      temp.imag = n1.imag+n2.imag;
      return(temp);
}

Output
Enter real and imaginary parts respectively:
3.4
5.5
For 2nd complex number,
Enter real and imaginary parts respectively:
-4.5
-9.5
Sum = -1.1-4i

In the problem, two complex numbers entered by the user is stored in structures n1 and n2.
These two structures are passed to addComplexNumbers() function which calculates the sum and returns the result to the main() function.
Finally, the sum is displayed from the main() function.


Example 4: C++ Program to Calculate Difference Between Two Time Period
Program: Program to Time Difference

// Computes time difference of two time period
// Time periods are entered by the user
#include <iostream>
using namespace std;
struct TIME
{
  int seconds;
  int minutes;
  int hours;
};
void computeTimeDifference(struct TIME, struct TIME, struct TIME *);
int main()
{
    struct TIME t1, t2, difference;
    cout << "Enter start time." << endl;
    cout << "Enter hours, minutes and seconds respectively: ";
    cin >> t1.hours >> t1.minutes >> t1.seconds;
    cout << "Enter stop time." << endl;
    cout << "Enter hours, minutes and seconds respectively: ";
    cin >> t2.hours >> t2.minutes >> t2.seconds;
    computeTimeDifference(t1, t2, &difference);
    cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" << t1.minutes << ":" << t1.seconds;
    cout << " - " << t2.hours << ":" << t2.minutes << ":" << t2.seconds;
    cout << " = " << difference.hours << ":" << difference.minutes << ":" << difference.seconds;
    return 0;
}
void computeTimeDifference(struct TIME t1, struct TIME t2, struct TIME *difference){
     if(t2.seconds > t1.seconds)
    {
        --t1.minutes;
        t1.seconds += 60;
    }
    difference->seconds = t1.seconds - t2.seconds;
    if(t2.minutes > t1.minutes)
    {
        --t1.hours;
        t1.minutes += 60;
    }
    difference->minutes = t1.minutes-t2.minutes;
    difference->hours = t1.hours-t2.hours;
}
Output
Enter hours, minutes and seconds respectively: 11
33
52
Enter stop time.
Enter hours, minutes and seconds respectively: 8
12
15
TIME DIFFERENCE: 11:33:52 - 8:12:15 = 3:21:37

In this program, user is asked to enter two time periods and these two periods are stored in structure variables t1 and t2 respectively.
Then, the computeTimeDifference() function calculates the difference between the time periods and the result is displayed on the screen from the main() function without returning it (call by reference).

Example 5: C++ Program to Store and Display Information Using Structure
This program stores the information (name, roll and marks) of 10 students using structures.
In this program, a structure, student is created.
This structure has three members: name (string), roll (integer) and marks (float).
Then, we created a structure array of size 10 to store information of 10 students.

Program: Store Information in Structure and Display it

#include <iostream>
using namespace std;
struct student
{
    char name[50];
    int roll;
    float marks;
} s[10];
int main()
{
    cout << "Enter information of students: " << endl;
    // storing information
    for(int i = 0; i < 10; ++i)
    {
        s[i].roll = i+1;
        cout << "For roll number" << s[i].roll << "," << endl;
        cout << "Enter name: ";
        cin >> s[i].name;
        cout << "Enter marks: ";
        cin >> s[i].marks;
        cout << endl;
    }
    cout << "Displaying Information: " << endl;
    // Displaying information
    for(int i = 0; i < 10; ++i)
    {
        cout << "\nRoll number: " << i+1 << endl;
        cout << "Name: " << s[i].name << endl;
        cout << "Marks: " << s[i].marks << endl;
    }
    return 0;
}

Output
Enter information of students: 

For roll number1,
Enter name: Tom
Enter marks: 98


For roll number2,
Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:

Roll number: 1
Name: Tom
Marks: 98
.
.
.

The document Programming Examples - File Structures | Learn to Program with C++: Beginner to Expert - Back-End Programming is a part of the Back-End Programming Course Learn to Program with C++: Beginner to Expert.
All you need of Back-End Programming at this link: Back-End Programming
73 videos|7 docs|23 tests
73 videos|7 docs|23 tests
Download as PDF
Explore Courses for Back-End Programming exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Semester Notes

,

pdf

,

study material

,

Viva Questions

,

Summary

,

MCQs

,

practice quizzes

,

Important questions

,

shortcuts and tricks

,

video lectures

,

Sample Paper

,

Free

,

Programming Examples - File Structures | Learn to Program with C++: Beginner to Expert - Back-End Programming

,

Programming Examples - File Structures | Learn to Program with C++: Beginner to Expert - Back-End Programming

,

past year papers

,

Extra Questions

,

Previous Year Questions with Solutions

,

Exam

,

Programming Examples - File Structures | Learn to Program with C++: Beginner to Expert - Back-End Programming

,

Objective type Questions

,

ppt

,

mock tests for examination

;