How to sum an array element using java

i want to calculate sum for array element,but cannot find the code.

Page 1 of 1

5 Replies - 2545 Views - Last Post: 26 August 2009 - 08:58 PM Rate Topic: -----

#1 emie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-August 09

How to sum an array element using java

Post icon  Posted 26 August 2009 - 06:06 PM

public class AverageDayForm1{
static int l = 5;
public static int[] studentNo;
public static double average;

studentNo=new studentNo[5];
studentNo[0]=25;
studentNo[1]=15;
studentNo[2]=28;
studentNo[3]=17;
studentNo[4]=19;

sum=



public static void avg() {
int sum=0, i;
for(i=0; i<l; i++) {
sum += values[i];
}
average = (double) sum / (double)l;
}
public static void main(String args[]){
set_values();
avg();
System.out.println("The value is " + average);
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: How to sum an array element using java

#2 mattman059  Icon User is offline

  • Epic Awesomeness
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Re: How to sum an array element using java

Posted 26 August 2009 - 06:09 PM

What is the problem? Make sure you post your code in between code tags also please so it's easier to read.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: How to sum an array element using java

Posted 26 August 2009 - 06:27 PM

int sum = 0;
for(int i = 0; i < studentNo.lenght; i++)
sum += studentNo[i];

and please :code:

as you can see my answer is not easy to read at all
Was This Post Helpful? 1
  • +
  • -

#4 prankster  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 67
  • Joined: 31-July 09

Re: How to sum an array element using java

Posted 26 August 2009 - 07:47 PM

As a lover of options I will add the equivalent of pbl's code using for-each:

int sum = 0;
for (int snum : studentNo)
{
	sum += snum;
}



See how easy it was to read with code tags?
:D We have to give the new guy a hard time you know!

This post has been edited by prankster: 26 August 2009 - 07:52 PM

Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: How to sum an array element using java

Posted 26 August 2009 - 08:23 PM

View Postprankster, on 26 Aug, 2009 - 06:47 PM, said:

As a lover of options I will add the equivalent of pbl's code using for-each:

int sum = 0;
for (int snum : studentNo)
{
	sum += snum;
}



See how easy it was to read with code tags?
:D We have to give the new guy a hard time you know!

and you used the [ code] tags !!!!
where is your "pedagogic approach :)
No money no candies :)
Was This Post Helpful? 0
  • +
  • -

#6 prankster  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 67
  • Joined: 31-July 09

Re: How to sum an array element using java

Posted 26 August 2009 - 08:58 PM

View Postpbl, on 26 Aug, 2009 - 07:23 PM, said:

and you used the [ code] tags !!!!
where is your "pedagogic approach :)
No money no candies :)


hehe! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1