Like Tree2Likes
  • 1 Post By tabstop
  • 1 Post By quzah

how to sum 500 numbers

This is a discussion on how to sum 500 numbers within the C Programming forums, part of the General Programming Boards category; Hello all, Could anyone please help me writing a code to sum a 500 numbers from 2-arrays. heres what i ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    8

    how to sum 500 numbers

    Hello all,

    Could anyone please help me writing a code to sum a 500 numbers from 2-arrays.

    heres what i wrote so far
    Code:
    unsigned int a[500];
    unsigned int b[500];
    int i;
    unsigned int answer;
     
       for ( i=0; i<=1000 ; i++)
    { answer= a[i]*b[i];
    
    .......
    }
    what shall i do next to get the sum of all the values ?

    thank you

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you store a value?
    How do you add to that value?

    Answer those two questions, and you will have your answer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    thanks quzah ... yeah the problem is i am not sure hot to add to that value .. so i am a newbie to programming

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, you know how to multiply, as we see that in the code. Surely you know what symbol is used for addition? (Hint: It's '+'.)
    quzah likes this.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    haha
    well, thank you
    is something like that correct ?
    Code:
    unsigned int a[500];
    unsigned int b[500];
    int i;
    unsigned int answer[];
    unsigned int sum=0; 
       for ( i=0; i<=1000 ; i++)
    { answer[i]= a[i]*b[i];
    sum= sum+answer[i];
    }
    thanks

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to give a size for the answer array. Your loop goes to 1000 but you only have 500 pairs of numbers.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Yes, something like that, but definitely not that:
    Code:
    unsigned int a[500];
    unsigned int b[500];
    int i;
    unsigned int answer[];
    unsigned int sum=0; 
       for ( i=0; i<=1000 ; i++)
    { answer[i]= a[i]*b[i];
    sum= sum+answer[i];
    }
    Egad! Where are those other 500 numbers coming from? a and b are only defined for indexes 0 to 499, but your loop tries to index all the way through 999. Also, you should probably give answer a size. I reckon 500 would be good there too, once you change your loop. This sounds like a perfect opportunity to put in a plug for named constants (i.e. not bad juju magic numbers like 500 and 1000) and indentation:
    Code:
    #define SIZE 500  // hooray for constants...want to change the size?  only one place to change it instead of 4
    unsigned int a[SIZE];
    unsigned int b[SIZE];
    int i;
    unsigned int answer[SIZE];
    unsigned int sum = 0;
    
    for (i = 0; i < SIZE; i++) {
        answer[i] = a[i] * b[i];
        sum += answer[i];  // that's shorthand for sum = sum + answer[i]
    }
    Last edited by anduril462; 07-13-2011 at 03:59 PM. Reason: fixed boo boo

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Code:
     
    unsigned int a[500];
    unsigned int b[500];
    int i;
    unsigned int answer[2];
    unsigned int sum=0; 
       for ( i=0; i<=500 ; i++)
    { answer[i]= a[i]*b[i];
    sum= sum+answer[i];
    }
    ?

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    thank you guys

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you think that 2 is the same number as 500, this is going to be a long thread.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This thread teaches us: You can do a man's work for him, but you can't teach a man to think.


    Quzah.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Code:
    unsigned int answer[2];
    Why would you only make answer an array of 2 ints? Your loop runs i from 0 to 500 (should be 499, see below), and tries to access answer[i], which will be invalid for any values of i other than 0 or 1.

    Code:
    for ( i=0; i<=500 ; i++)
    Drop that = sign. Your array has 500 elements, but the valid indexes are 0..499 (it's always 0..size-1 in C). Your loop goes one too far, and tries to access a[500] and b[500], which don't exist.

    I think you just missed my post when you were replying, it gives a good solution to all your array size and loop troubles. Scroll up a little...

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    thank you anduril462 ... I appreciate your help ... and for the others , bravo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-08-2010, 10:26 AM
  2. Can anyone help me to break numbers into an array numbers
    By s2xcracker in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2009, 05:17 PM
  3. Replies: 5
    Last Post: 12-21-2007, 12:38 PM
  4. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 06:56 AM
  5. Replies: 4
    Last Post: 03-03-2003, 02:52 PM

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