BCA
Object Oriented Programming using C++ Practical
Madras University
Program 3
Design and implement a class that represents a Harmonic Progression (HP).
Implement functions to do the following:
a. Generate the HP up to a specified number of terms
b. Calculate the sum of the HP to n terms and to infinity
c. Generate the nth term of the HP
d. Generate the corresponding Arithmetic Progression. (Design and implement
a class that encapsulates an AP, and allow the HP class to use its facilities by
implementing friend functions.)
SOURCE CODE:
#include <iostream>
#include <vector>
#include <limits>
#include <cmath> // For log and pow functions
using namespace std;
class ArithmeticProgression {
private:
double a; // First term
double d; // Common difference
public:
ArithmeticProgression(double first_term, double common_difference)
: a(first_term), d(common_difference) {}
double nth_term(int n) const {
return a + (n - 1) * d;
}
double sum_to_n_terms(int n) const {
return n * (2 * a + (n - 1) * d) / 2;
}
vector<double> generate_terms(int n) const {
vector<double> terms;
for (int i = 1; i <= n; ++i) {
terms.push_back(nth_term(i));
}
return terms;
}
friend class HarmonicProgression;
};
class HarmonicProgression {
private:
ArithmeticProgression ap;
public:
HarmonicProgression(double first_term, double common_difference)
: ap(first_term, common_difference) {}
double nth_term(int n) const {
return 1.0 / ap.nth_term(n);
}
vector<double> generate_terms(int n) const {
vector<double> terms;
for (int i = 1; i <= n; ++i) {
terms.push_back(nth_term(i));
}
return terms;
}
double sum_to_n_terms(int n) const {
double sum = 0;
for (int i = 1; i <= n; ++i) {
sum += nth_term(i);
}
return sum;
}
double sum_to_infinity() const {
if (ap.d == 0) {
return numeric_limits<double>::infinity();
}
return numeric_limits<double>::infinity();
}
ArithmeticProgression corresponding_ap() const {
return ap;
}
};
int main() {
HarmonicProgression hp(1, 1);
int n;
cout<<"Enter number of HP terms: ";
cin>>n;
cout << "First " << n << " terms of HP: ";
vector<double> hp_terms = hp.generate_terms(n);
for (double term : hp_terms) {
cout << term << " ";
}
cout << std::endl;
cout << "Sum of first " << n << " terms of HP: " << hp.sum_to_n_terms(n) << endl;
cout << n << "th term of HP: " << hp.nth_term(n) << endl;
cout << "Sum of HP to infinity: " << hp.sum_to_infinity() << endl;
ArithmeticProgression ap = hp.corresponding_ap();
cout << "First " << n << " terms of corresponding AP: ";
vector<double> ap_terms = ap.generate_terms(n);
for (double term : ap_terms) {
cout << term << " ";
}
cout << endl;
cout << "Sum of first " << n << " terms of AP: " << ap.sum_to_n_terms(n) << endl;
cout << n << "th term of AP: " << ap.nth_term(n) << endl;
return 0;
}
Explanation:
ArithmeticProgression Class:
- Represents an arithmetic progression with the first term a and common difference d.
- Provides methods to calculate the nth term, sum of the first n terms, and generate the first n terms.
- The
HarmonicProgression
class is declared as a friend to allow access to its private members.
HarmonicProgression Class:
- Contains an instance of the
ArithmeticProgression
class. - Provides methods to calculate the nth term of the HP, generate the first n terms of the HP, calculate the sum of the first n terms of the HP, and calculate the sum of the HP to infinity.
- Returns the corresponding AP using the
corresponding_ap
method.
Main Function:
- Demonstrates the usage of the
HarmonicProgression
and ArithmeticProgression
classes, including generating terms, calculating sums, and accessing corresponding AP terms.
OUTPUT:
Enter number of HP terms: 5
First 5 terms of HP: 1 0.5 0.333333 0.25 0.2
Sum of first 5 terms of HP: 2.28333
5th term of HP: 0.2
Sum of HP to infinity: inf
First 5 terms of corresponding AP: 1 2 3 4 5
Sum of first 5 terms of AP: 15
5th term of AP: 5