Here is the question, A sequence of integers, 1,3,5,7,...can be represented by a
ID: 3536879 • Letter: H
Question
Here is the question,
A sequence of integers, 1,3,5,7,...can be represented by a function that takes a non negative integer as parameter and returns the corresponding term of teh 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 2 member functions
void printSeq (int K, int m);
int sumSeq(int k, int m)
That are passed 2 integer parameters K and m where k
Thank you for your help.
Explanation / Answer
#include<iostream>
using namespace std;
int odd(int k) {return 2 *k +1;}
class AbstractSeq
{
public:
virtual int fun(int K) = 0;
void printSeq (int k, int m)
{
for(int i=k;i<=m;i++)
{
cout << fun(i) << " ";
}
cout <<endl;
}
int sumSeq(int k, int m)
{
int totalSum = 0;
for(int i=k;i<=m;i++)
{
totalSum+=fun(i);
}
return totalSum;
}
};
class seq:public AbstractSeq
{
public:
int fun(int K)
{
return 2*K+1;
}
};
int main()
{
seq s1;
s1.printSeq(1,4);
cout << s1.sumSeq(1,4) << endl;
return 0;
}