Using an Array in an equation
I am having trouble trying to figure out this only section of this program. I have an array. To keep this simple lets say the array is int n[] = {1, 2, 3, 4, 5} and I would like each number in the array to go through the equation 30n - 5. I would like the output to have the answer for each number going through. the output is going to look like. After the number was entered into the equation the answer is 25. After the number was entered into the equation the answer is 55. After the number was entered into the equation the answer is 85. After the number was entered into the equation the answer is 115. After the number was entered into the equation the answer is 145. So long question short is how do you make each number of the array go through the equation
Re: Using an Array in an equation
Use a for loop.
An array has a variable called length. It is the length of the array, though you want to go to one less than this length as array indexes start at 0.
a for loop syntax looks kind of like this
for (variable_type variable = someValue; some_boolean_condition_for_that_value; some_incrementation_or_decrementation)
Here's a page that shows an example of a for loop with an array.
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: Using an Array in an equation
Thanks for the response. I actually have the for loop.
Code :
for (int i = 0; i < n.length; i++){
double sum = (20*n.length) - 4;
System.out.println("After the numbers are entered into the function t(n)=20n-4 the outcome is " + sum);
}
I believe the problem is at the 20*n.length
Re: Using an Array in an equation
lol that is where it was lol. it is 20*n[i] - 4. Overlooked that
Re: Using an Array in an equation
Replace the variable n in your equation with an element of the array: n[idx] where idx is the index into the array
Re: Using an Array in an equation
Please read the forum rules - duplicate posts are not allowed. I have merged your two threads.