Hey guys! Our teacher wants us to create a factorial chart using recursion and I figured that part out already, but I am having trouble writing my function data to an array. Our teacher does not teach us, so we basically are on our own. He also wants us to print the data out into an HTML file. So far I have the row of x printed, but I am assuming I will have to store my factorial data into an array before I can use a loop to write it to HTML. I am a beginner and this is my first year so please go easy on me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream> //to use cout and cin and endl// allows using cout without std::cout
#include <string> // to use string data type
#include <cstring> //to use strlen, strcmp, strcpy
#include <cmath> //for pow function,sqrt,abs
#include <iomanip> // for set precision, setw,
#include <fstream> //for file input
#include <cassert> // to use assert to disable place before #define NDEBUG
#include <cstdlib>//for random numbers, exit function
#include <ctime>//time function used for rand seed
#include <cctype> // for toupper, tolower
#include <algorithm>
#include <locale.h>
#include <stdio.h>
#include <sstream>
using namespace std;
double findFactorial (double x){
if(x==1)
{
return 1;
}
else
{
return x*findFactorial(x-1);
}
}
int main(){
ofstream outfile;
cout<<"This program displays a chart of factorials using recursion."<<endl;
cout<<" "<<endl;
cout<<fixed<<setprecision(0);
{
for (int x=1;x<=30;x++)
{
cout<<x<<"! "<<findFactorial(x)<<endl;
}
}
int x;
outfile.open("c:\\data\\functions.html");
outfile<<"<!DOCTYPE html><html><head><title>Functions</title>";
outfile<<"<style> table, th, td { border: 1px solid black;border-collapse:collapse;background-color:lightblue;} td{width:120px;}</style>";
outfile<< "</head><body style='background-color:yellow;'>\r\n";
outfile<<"<h1>Functions</h1>\r\n";
outfile<<"<p>This is a chart of functions.</p>\r\n" ;
outfile<<"<table>";
for(x=1;x<=30;x++)
{
outfile<<"<tr><td>"<<x<<"</td></tr>\r\n";
}
outfile<<"</table>\r\n";
outfile
<<" </body></html>\r\n";
return 0;
}
|
output:
This program displays a chart of factorials using recursion.
1! 1
2! 2
3! 6
4! 24
5! 120
6! 720
7! 5040
8! 40320
9! 362880
10! 3628800
11! 39916800
12! 479001600
13! 6227020800
14! 87178291200
15! 1307674368000
16! 20922789888000
17! 355687428096000
18! 6402373705728000
19! 121645100408832000
20! 2432902008176640000
21! 51090942171709440000
22! 1124000727777607680000
23! 25852016738884978212864
24! 620448401733239409999872
25! 15511210043330986055303168
26! 403291461126605650322784256
27! 10888869450418351940239884288
28! 304888344611713836734530715648
29! 8841761993739700772720181510144
30! 265252859812191032188804700045312