Math program

This is a discussion on Math program within the C++ Programming forums, part of the General Programming Boards category; I don't understand why my program doesn't work... Its supposed to find the sum of: 1*2 + 3*4 + 5*6 ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    195

    Math program

    I don't understand why my program doesn't work... Its supposed to find the sum of:

    1*2 + 3*4 + 5*6 ... + 2003*2004
    When i output what is being held in array[0] it gives me like 4.022+06 or something whereas it should be 1 and array[1] should be two

    Code:
    #include <iostream.h>
    #include <math.h>
    
    void main() {
    	int i, j;
    	double array[1002];
    	double sum=0;
    	double square;
    
    	for (j=0; j<1002; j++) {
    		for (i=1; i<=2004; i+=2) {
    			array[j]=i*(i+1);
    		}
    	}
    
    	for (i=0; i<1002; i++) {
    		sum+=array[i];
    	}
    	cout<<array[0]<<endl;
            cout<<sum<<endl;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Your inner loop does nothing - except to put 2004 * 2005 into every array element
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    195
    Oh yeah you're right thanks a lot

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    195
    k i fixed it

    Code:
    	for (j=1; j<=1002; j++) {
    		array[j-1]=(2*j)*(2*j -1);
    	}

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Code:
    for (j=0; j<1002; j++) {
    		for (i=1; i<=2004; i+=2) {
    			array[j]+=i*(i+1);
    		}
    	}
    would this work note you must init the array to zero

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    It would work but why have an array with 1002 elements, that are all the same?

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Code:
    int sum(int maxNumber)
    {
       if (maxNumber<=2) return maxNumber;
       return maxNumber*(maxNumber-1)+sum(maxNumber-2);
    }
    Thought I'd try a recursive solution.
    Is this correct. Not very pretty thats for sure.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    195
    well my new and improved code sets

    array [0]: 1*2
    array [1]: 3*4
    array [2]: 5*6

    ...

    array [1002] 2003*2004

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    17
    Been thinking about your new and improved code. . . I think that the last entry is wrong.

    It should read:
    Code:
    array[1001]: 2003*2004
    Reasoning: each "n" entry of the array should be equal to ([(n+1)*2]-1)*[(n+1)*2]

    just2peachy

    P.S.
    if you look closely at these numbers:
    array[0]=1*2=2(1)
    array[1]=3*4=2(6)
    array[2]=5*6=2(15)
    array[3]=7*8=2(28)
    .
    .
    .
    Examine the pattern forming. Do you see a special number pattern with 1, 6, 15, 28, . . .

    If you've messed around with triangular numbers or hexagonal numbers, you should see some bells and whistles.

    Try a google search with triangular numbers. You'll find that each
    T(2n+1) or each odd triangular number fitts this pattern. There are many formulas for dealing with the summation of triangular numbers or in this case hexagonal numbers. The summation of these numbers could probably be found with one quick formula.
    Last edited by just2peachy; 10-18-2004 at 09:29 AM. Reason: Another Thought!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math program help.
    By Deadlyaim in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2006, 08:57 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21