View Single Post
  #6 (permalink)  
Old 07-04-2008, 07:45 PM
hannehomuth's Avatar
hannehomuth hannehomuth is offline
Member
 
Join Date: Jul 2008
Location: Sommerfeld (Brandenburg, Germany)
Posts: 35
hannehomuth is on a distinguished road
Hey this is realy simple. But you have to learn to work with for loops or stuff like that. I will give you a little example.

To calculate the sum you have to write code like this.

Code:
double sum = 0; for(int i=0;i<prices.length;i++) { sum = sum + prices[i]; }
This code will go through the whole array and adds each value in the array to the variable sum.

In other Words it does exactly this
Code:
double sum = prices[0] + prices[1] + prices[2] + prices[3] + prices[4] + prices[5] + prices[6] + prices[7] + prices[8] + prices[9] + prices[10] + prices[11] + prices[12] + prices[13] + prices[14]+ prices[15] + prices[16] + prices[17] + prices[18] + prices[19];
only cooler.

And if you wanna know how many are under 5

Code:
int howManyUnderFive = 0; for(int i=0;i<prices.length;i++) if(prices[i] < 5) howManyUnderFive++;
Learn to use loops, you will not be able to do more stuff if you dont learn that. Its nesseary for everything, but its easy to understand. I hope it helps you
Reply With Quote