• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I guess I have to make a separate array for the "sum"?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Why not using a single variable ?
 
Julie Nelson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic