Thread: Trouble with program

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Trouble with program

    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.
    Last edited by kblanch09; 03-31-2010 at 03:05 PM. Reason: Forgot the problem

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by kblanch09 View Post
    I have a feeling that the factoral funtion is what is causing my problem
    you forgot to say what is your problem
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    N= 0 ------------------------------------------------------------------------
    trm=1.00000000000000000000000000000000000000000000
    sum=1.00000000000000000000000000000000000000000000
    n= 1 ------------------------------------------------------------------------
    trm=1.11111111111111111111111111111111111111111111
    sum=3.22222222222222222222222222222222222222222222
    n= 2 ------------------------------------------------------------------------
    trm=1.11111111111111111111111111111111111111111111
    sum=5.44444444444444444444444444444444444444444444

    that is what my output looks like
    so actually, my sum is off also and im only guessing it has somthing to do with my FactScaler function

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for the start
    Code:
    for (int i = 1; i < MAXDIG; i ++)
    		return a[i];
    is actually is the same as

    Code:
    return a[1];
    you want to return array, you cannot do it - you are passing array x as parameter - fill it with the output data instead
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a bit of trouble with a binary search program
    By d-dub in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2006, 05:20 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM