programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Performing math equations within arrays

Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
Can someone point me in the right direction on this...I'm just now learning arrays and am trying to figure out how to get a sum or difference of my array numbers. So far I can display all the numbers but is there a different way to perform math when it comes to arrays, different from plugging in floating point variables?

public class Merchandise
{
public static void main(String[] args)
{
double[] salary = {5.25, 6.55, 10.25, 16.85};
System.out.println("Salaries one by one are:");
for(int x = 0; x < salary.length; ++x)

System.out.println(salary[x]);
}
}

Sheriff
Posts: 14691
16
• Number of slices to send:
Optional 'thank-you' note:
Do you know how to make a sum or difference of two variables ? If you can, doing it with arrays is not different, if you consider that things in the array are variables too.

Julie Nelson
Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
So if I want to add 5.25 to 6.55 will I type the following?

int value1 = 5.25, value2 = 6.55;
sum = value1 + value2;
System.out.println("Sum is " + sum);

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

Julie Nelson wrote:So if I want to add 5.25 to 6.55 will I type the following?

int value1 = 5.25, value2 = 6.55;
sum = value1 + value2;
System.out.println("Sum is " + sum);

Yes, but that's not using arrays -- what Christophe is saying is that elements of the array are just like any other variable...

double[] salary = {5.25, 6.55, 10.25, 16.85};
sum = salary[0] + salary[1];
System.out.println("Sum is " + sum);

Julie Nelson
Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
So I guess I have to make a separate array for the "sum"?

Sheriff
Posts: 14691
16
• Number of slices to send:
Optional 'thank-you' note:

So I guess I have to make a separate array for the "sum"?

Why not using a single variable ?

Julie Nelson
Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
Thank you both for your help!!! I really appreciate it.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.