1

new to programming but trying to assign to my double variable inside my for loop. Basically I need to use Math.abs and Math.sin, which is sort of throwing me off. Any help is appreciated. If i need to provide any more information please let me know.

double[] xValues = new double[arrayAmount];
double[] yValues = new double[arrayAmount];

xValues[0] = minimumValue;

for (int index = 0; index==arrayAmount; index++)
{

    yValues = 20.0 * Math.abs((Math.sin(xValues))); // java saying this is wrong

}
Jason C
  • 34,234
  • 12
  • 103
  • 151
MikeJ
  • 53
  • 2
  • 9

4 Answers4

2

Do you want something like this?

for (int index = 0; index==arrayAmount; index++)
{
   yValues[index] = 20.0 * Math.abs((Math.sin(xValues[index])));
}

Notice that you are getting a value from a specific index of xValues and saving at a specific index of yValues.

Also notice that your xValues only have 1 element. So if you code specify more about the values or the problem, we could be able to help you more.

Good luck!

Rey Libutan
  • 4,907
  • 7
  • 34
  • 67
1

The syntax in Java used to access to an element on an specific index of an array is:

nameOfArray[index]

so if you want to assign some value to yValues in an specific index, you have to use:

yValues[index] = 20.0 * Math.abs((Math.sin(xValues[index])));

Note that your loop won't work unless the length of the array is 0. Try changing the loop condition to:

for (int index = 0; index < arrayAmount; index++) { ... }

or

for (int index = 0; index < yValues.length; index++) { ... } 
Christian
  • 31,246
  • 6
  • 45
  • 67
  • thank you! this really helped. I have one more quick question if you could help. I need to add to the xValue a user-inputted number every time the loop goes around. Can you help with how to code that? Would it be"""" xValues[index] = xValues[index] + increment; """" – MikeJ Mar 18 '14 at 05:21
  • You can use a `Scanner`: Declare the scanner object outside the loop: `Scanner sc = new Scanner(System.in);` Then inside the loop: `xValues[index] = sc.nextDouble();`. You can also see [this post](http://stackoverflow.com/questions/11871520/how-to-use-the-scanner-class-in-java) for further information. – Christian Mar 18 '14 at 05:23
  • Before the loop the user already declared the increment value. The value never changes, i just need the xValue to increase by this increment every go-around. Example would be xValue[0] = 5 ... increment = .25 ... xValue[1] = 5.25 ... xValue[2] = 5.50 ... etc. Sorry if i wasn't being clear enough. – MikeJ Mar 18 '14 at 05:26
  • Then what you did (`xValues[index] = xValues[index] + increment;`) would work. This can also be simplified by `xValues[index] += increment;` – Christian Mar 18 '14 at 05:28
0

You need to specify an index to get/set specific values in an array.

If you have:

double[] xValues = ...;

And you want to refer to the double at index, say, 3:

xValues[3] = ...;
... = xValues[3];

You need to add [index] to your arrays.

Also, by the way, your condition is wrong in your for loop. The loop will go until the condition is true, not while the condition is true. You have:

for (int index = 0; index==arrayAmount; index++)

This says "loop while index==arrayAmount". You want:

for (int index = 0; index<arrayAmount; index++)
Jason C
  • 34,234
  • 12
  • 103
  • 151
0

array is like rack on the shelf each have value block so instead of

yValues = 20.0 * Math.abs((Math.sin(xValues)));

put the value in each rack by yourself like this:

yValues[index] = 20.0 * Math.abs((Math.sin(xValues[index])));

now java does not says this is wrong

Sarz
  • 1,951
  • 3
  • 20
  • 41