Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Using an Array in an equation

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Banned
    Join Date
    Jul 2012
    Location
    On planet Earth
    Posts
    1
    My Mood
    Innocent
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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)

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using an Array in an equation

    Thanks for the response. I actually have the for loop.

    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

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using an Array in an equation

    lol that is where it was lol. it is 20*n[i] - 4. Overlooked that

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using an Array in an equation

    Please read the forum rules - duplicate posts are not allowed. I have merged your two threads.

Similar Threads

  1. Does there exist a Jar that solves Math Equation?
    By williamshen25 in forum Java Theory & Questions
    Replies: 3
    Last Post: June 29th, 2012, 11:45 AM
  2. quadratic equation solver help!
    By overlord in forum Algorithms & Recursion
    Replies: 2
    Last Post: October 20th, 2011, 11:39 AM
  3. Testing equation of a line
    By TimoElPrimo in forum Object Oriented Programming
    Replies: 8
    Last Post: February 23rd, 2011, 12:40 AM
  4. Linear Equation Help !!!
    By thangavel in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 13th, 2011, 06:32 AM
  5. Help with Quadratic forumla equation in java please.
    By taylor6132 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2010, 07:27 PM