A sequence of integers such as 1, 3, 5, 7, ... can be represented by a function
ID: 3846683 • Letter: A
Question
A sequence of integers such as 1, 3, 5, 7, ... can be represented by a function that takes a nonnegative integer as parameter and returns the corresponding term of the sequence. For example, the sequence of odd numbers just cited can be represented by the function
int odd(int k) {return 2 * k + 1;}
Write an abstract class AbstractSeq that has a pure virtual member function
virtual int fun(int k) = 0;
as a stand-in for an actual sequence, and two member functions
void printSeq(int k, int m);
int sumSeq(int k, int m)
that are passed two integer parameters k and m, where k < m. The function printSeq will print all the terms fun(k) through fun(m) of the sequence, and likewise, the function sumSeq will return the sum of those terms. Demonstrate your AbstractSeq class by creating subclasses that you use to sum the terms of at least two different sequences. Determine what kind of output best shows off the operation of these classes, and write a program that produces that kind of output.
Explanation / Answer
Program:
#include <iostream>
#include “subfunctions.h”
using namespace std;
int main()
{
LeapYr Test;
int result;
Test.printSeq(0, 4);
result = Test.sumSeq(0, 4);
cout << result << endl;
return 0;
}
//subfunctions.cpp
#include <iostream>
#include “subfunctions.h”
using namespace std;
//********************************************************************************
// This function accepts an integer as its parameter and returns an odd number. *
//********************************************************************************
int Odds::fun(int k)
{
return 2 * k + 1;
}
//********************************************************************************
// This function accepts an integer as its parameter and returns an odd number. *
//********************************************************************************
int LeapYr::fun(int k)
{
return k * 4;
}
//subfunctions.h
#ifndef SUBFUNSTIONS_H
#define SUBFUNSTIONS_H
#include <iostream>
#include “AbstractSeq.h”
using namespace std;
class Odds:public AbstractSeq
{
public:
virtual int fun(int k);
};
class LeapYr:public AbstractSeq
{
virtual int fun(int k);
};
#endif
//AbstractSeq.h
#ifndef ABSTRACTSEQ_H
#define ABSTRACTSEQ_H
#include <iostream>
using namespace std;
class AbstractSeq
{
public:
virtual int fun(int k) = 0;
void printSeq(int k, int m);
int sumSeq(int k, int m);
};
#endif
//AbstractSeq.cpp
#include <iostream>
#include “AbstractSeq.h”
using namespace std;
//********************************************************************************
// This function accepts two integer parameters k and m, where k < m. The *
// function will print all the terms fun(k) through fun(m) of the sequence. *
//********************************************************************************
void AbstractSeq::printSeq(int k, int m)
{
if (k < m)
{
for (int i = k; i < m; i++)
{
cout << fun(i) << ” “;
}
cout << endl;
}
}
//********************************************************************************
// sumSeq *
// This function accepts two integer parameters k and m, where k < m. The *
// function will return the sum of all the terms fun(k) through fun(m). *
//********************************************************************************
int AbstractSeq::sumSeq(int k, int m)
{
int Sum = 0;
if (k < m)
{
for (int i = k; i < m; i++)
{
Sum+= fun(i);
}
}
return Sum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.