Hi,
Me and my team have been working on this program where e is the summation of: 1/0i + 1/1i + 1/2i....... the output gives the value of n. Next it gives the value of the term (ex. 1/0i). Finally, it gives the running sum (ex. 1/0i + 1/1i....)
Here is a sample output of what it should look like:
N= 0 ------------------------------------------------------------------------
TRM=1.00000000000000000000000000000000000000000000
SUM=1.00000000000000000000000000000000000000000000
N= 1 ------------------------------------------------------------------------
TRM=1.00000000000000000000000000000000000000000000
SUM=2.00000000000000000000000000000000000000000000
N= 2 ------------------------------------------------------------------------
TRM=0.50000000000000000000000000000000000000000000
SUM=2.50000000000000000000000000000000000000000000
Here is my code so far. I have a feeling that the factoral funtion is what is causing my problem but i just cant seem to figure it out. Any feedback will be appreciated. Thank you!!!!
Code:
#define _SECURE_NO_WARNINGS
#include <stdio.h>
#define MAXDIG 200
#define MAXTRM 10
#define MAXPRINT 72
int FactScaler (int x[], int n);
void ZeroArray (int x[]);
void PrintArray (int x[]);
void DivArrayScaler (int n, int newterm[]);
void CopyArray (int term[], int newterm[]);
void AddArray (int sum[], int newterm[], int term[]);
void main()
{
int term[MAXDIG];
int sum[MAXDIG];
int newterm[MAXDIG];
int n = 0;
ZeroArray(term);
ZeroArray(sum);
term[0] = 1;
sum[0] = 1;
printf("N=%3d -----------------------------------------------------------------------\n",n);
printf("TRM="); PrintArray(term); printf("\n");
printf("SUM="); PrintArray(sum); printf("\n");
for (n = 1; n <= MAXTRM; n++)
{
DivArrayScaler(n, newterm);
CopyArray(term, newterm);
AddArray(sum, newterm, term);
printf("N=%3d -----------------------------------------------------------------------\n",n);
printf("TRM="); PrintArray(term); printf("\n");
printf("SUM="); PrintArray(sum); printf("\n");
}
}
int FactScaler(int x[], int n)
{
double z = 1.0;
int a[MAXDIG];
for (int i = 1; i < MAXDIG; i++)
{
for (int j = 1; j < n; j++)
z *= i;
a[i] = z;
}
for (int i = 1; i < MAXDIG; i ++)
return a[i];
}
void ZeroArray(int x[])
{
for (int i = 0; i < MAXDIG; i++)
x[i] = 0;
}
void PrintArray(int x[])
{
printf("%d.",x[0]);
for (int i = 1; i < MAXPRINT; i++)
printf("%d",x[i]);
}
void DivArrayScaler(int n, int newterm[])
{
int x[MAXDIG];
for (int i = 0; i < MAXDIG; i++)
newterm[i] = FactScaler(x, n);
}
void CopyArray(int term[], int newterm[])
{
for (int n = 0; n < MAXDIG; n++)
term[n] = newterm[n];
}
void AddArray(int sum[], int newterm[], int term[])
{
int carry = 0;
for ( int n = 0; n < MAXDIG; n++)
{
sum[n] += (term[n] + newterm[n] + carry) % 10;
carry = (int)((term[n] + newterm[n] + carry) / 10);
}
}
Opps. My problem is that my output stays the same. The running sum is correct but the term is always 1.111111111111111111111. So the sum is always that number added onto the sum.