I need to write a program that will be a table with the columns being: n, n!, an
ID: 3541620 • Letter: I
Question
I need to write a program that will be a table with the columns being: n, n!, and e^n. The numbers for n = 1, 2, 3, 4,...,12 and then the values of n! and e^n with respect to n. It needs to be a column for n with numbers 1 through 12, then the next column must be n! with the numbers of 1! through 12!, and then the next column must be for e^n with e^1 through e^12. They must also be in floating decimal point. I figured out how to make the equations but I can't figure out how to make the table of 3 columns and 13 rows. Please please help.
Explanation / Answer
#include<iostream>
using namespace std;
int main(){
//You can create such a table in c++ using 2-D array having 12 rows and 3 columns as follows :-
float table[12][3];//You need 12 rows and not 13
//Now, you can populate the table as follows :-
int i;//loop variable
for(i=0;i<12;i++){//Loop through each row and fill the table one row at a time
table[i][0] = i+1;//Enter the row number i into the first column of ith row
table[i][1] = fact(i+1);//Enter n! into the second column of ith row. Function fact(n) is provided below
table[i][2] = exp(i+1);//Enter e^n into the third column of the ith row. exp(x) is predefined in math.h library
}
//Remaining code can be pasted here.
return 0;
}//end of main function
//function to calculate factorial of a number n
int fact(int n){
if(n < 0) return -1;//Error case. Factorial of negative numbers doesn't exist
if(n == 0) return 1;//Base case. 0! = 1
else return n*fact(n-1);//recursion. n! = n*(n-1)!
}