how do i Design a program to add all numbers in an array
Design a program to add all numbers in an arrayDesign a program to add all numbers in an array
Page 1 of 1
1 Replies - 4343 Views - Last Post: 25 March 2008 - 02:53 PM
Replies To: Design a program to add all numbers in an array
#2
Re: Design a program to add all numbers in an array
Posted 25 March 2008 - 02:53 PM
Create a variable to act as an "accumulator" that is, the value you add to each time so that at the end it will represent the sum. Be sure to start it at zero. Loop through the array from 0 to the size of the array - 1, each time through the loop pull the value out of the array and add it to the accumulator variable.
At the end of the loop, your accumulator variable will then have the sum of all values in the array.
So what you will need is a variable to hold the sum, a loop to loop through the array, an array to hold all your numbers and then a print statement to print out the results.
Once you get started and present some code, if you get stuck then you can show your code, any error messages you are getting and what is exactly wrong and then we can help you further.
Hope that makes some sense.
At the end of the loop, your accumulator variable will then have the sum of all values in the array.
So what you will need is a variable to hold the sum, a loop to loop through the array, an array to hold all your numbers and then a print statement to print out the results.
Once you get started and present some code, if you get stuck then you can show your code, any error messages you are getting and what is exactly wrong and then we can help you further.
Hope that makes some sense.

Page 1 of 1