So I'm working on a homework assignment and I need to create recursive functions for partitions, Stirling numbers(first and second kind), and Chebyshev polynomials of the first. My program should be able to have a user input a positive integer n, and then create files named Partitions.txt, Stirling1.txt, Stirling2.txt, and Chebyshev.txt, that creates a table of all values f(k,m) for 1<=k<=n and 1<=m<=n. I'm struggling just to start off the assignment and feel like I have no understanding of it even though I've been doing research and trying to figure it out. If someone can help me out, I'd really appreciate it! Thank you!
#include <iostream>
#include <vector>
#include "OutputFile.h"
using namespace std;
using namespace OutputStream;
int firstKindStirling();
vector<vector<int> > getPartitions(int number, int maxElement);
int main() {
cout << "Welcome! Please input a number m:";
int m;
cin>>m;
OFile fout("Partitions.txt");
return 0;
}
vector<vector<int> > getPartitions(int number, int maxElement)
{
if (number < 1)
return vector<vector<int>>();
vector<vector<int>> partitions;
if (number <= maxElement)
partitions.push_back(number); //for some reason this doesn't want to work. Not sure what I'm missing here.
for (int i = number - maxElement; i < number; ++i)
{
// (recursively) get the partitions of `i`, with elements no larger than `n - i`
auto partitionsForI = getPartitions(i, number - i);
// add `n - i` to the front of all of those partitions
for(vector<int>& partition : partitionsForI)
{
partition.insert(partition.begin(), number - i);
}
// add these new partitions to our list.
partitions.insert(partitions.end(), partitionsForI.begin(), partitionsForI.end());
}
return partitions;
}
int firstKindStirling(int n, int k)
{
if (n == 0 && k == 0) return 1;
else if (n == 0 || k == 0) return 0;
else return -(n-1) * firstKindStirling(n-1, k) + firstKindStirling(n-1, k-1);
}
And here's my Output .h file
#ifndef OUTPUT_H
#define OUTPUT_H
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <sys/stat.h>
#include <sstream>
#include <memory>
namespace OutputStream {
class OFile {
std::ofstream file;
public:
OFile(std::string filename, size_t output_precision = 10) {
file.open(filename);
if(file.fail()) throw std::runtime_error("Error: cannot open file");
file.precision(output_precision);
};
/*
OFile& operator<<(int x) {
file<<x;
return *this;
}
*/
/*
OFile& operator<<(const Point2D& p) {
file<<p;
return *this;
}
*/
OFile& operator<<(const std::vector<int>& v) {
for(auto x : v) file<<x<<std::endl;
return *this;
}
template<typename T>
OFile& operator<<(const T& p) {
file << p;
return *this;
}
~OFile() { file.close(); };
};
// Strongly enumerate type
enum class FileType { Input, Output, SafeOutput };
// Partial Template Specialization
template<FileType> class File;
template<>
class File < FileType::Input > {
public:
File( const std::string& filename ) : fin(filename) {
if(fin.fail()) throw std::runtime_error("Error opening file: "+filename);
};
/** ...
IFile& allows for syntax like
fin>>a>>b>>c;
*/
File& operator>>(int& a) {
fin>>a;
return *this;
}
/**...*/
operator bool() {
return !(fin.fail());
}
operator std::string() {
return "Active";
}
// operator [data type]() {
// code here
// return [object of type data type];
// }
friend File& getline( File& fin, std::string& line) {
getline( fin.fin, line);
return fin;
}
friend File& getrow( File& fin, std::vector<int>& rows);
friend File& getmatrix( File& fin, std::vector< std::vector<int> >& table);
~File() { fin.close(); };
private:
std::ifstream fin;
};
template<>
class File < FileType::Output > {
std::ofstream file;
public:
File(std::string filename, size_t output_precision = 10) {
file.open(filename);
if(file.fail()) throw std::runtime_error("Error: cannot open file");
file.precision(output_precision);
};
/*
OFile& operator<<(int x) {
file<<x;
return *this;
}
*/
/*
OFile& operator<<(const Point2D& p) {
file<<p;
return *this;
}
*/
File& operator<<(const std::vector<int>& v) {
for(auto x : v) file<<x<<std::endl;
return *this;
}
template<typename T>
File& operator<<(const T& p) {
file << p;
return *this;
}
~File() { file.close(); };
};
}
#endif