Writing function data to an array

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
Last edited on
23! 25852016738884978212864

This is wrong. Your teacher needs you to calculate 30 factorial in C++? C++'s built-in types can't handle exact numbers that big, so you will need to use a custom big integer class most likely.
See, for example: https://github.com/kasparsklavins/bigint

Are you guessing as to whether or not you need to use an array? Because if you're just writing the values to a file, you don't need an array. But if you still want one, if you want an array of size 30 of doubles, you would declare it like this:

 
double arr[30];


Then, you can fill it in with variables by accessing each index
1
2
3
4
for (int i = 0; i < 30; i++)
{
    arr[i] = /* some number */ ;
}
I think you need to get your factorials right first.

Anything from 5! onward must have factors of 2 and 5 ... so must end in 0.
Your factorials from 23! on don't. This is a feature of the modest precision with which floating-point numbers are stored.

When you get your factorials right (or you use unsigned long long and restrict them to the limiting value of such a type) then you can write them to a table data element <td> in the same way as you are currently writing x. Just evaluate the factorial function at the point where you want to write to file.
Last edited on
Yes our teacher says the data has to be stored in an array. These were his instructions:

due Oct 29

Create a Table of factorials using recursion, where a function calls its self.

You can modify the code for a factorial that we used for the binomial problem.

Use an array to store the factorials.

Print the factorials to a HTML file. You will have 2 columns, one for 0 to 30 and the other for the equivalent factorial.

Go from 0 to 30 for the factorials.


Thank you both for replying.
Topic archived. No new replies allowed.