Results 1 to 9 of 9
  1. #1
    whileloop is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default For Loop Help! Completely Lost

    1. Write and test a function
    int triangular(int n)
    which returns the nth triangular number. A triangular number is a number which is the sum of the first n positive integers. So, for example 15 is the 5th triangular number since 15 = 1 + 2 + 3 + 4 + 5. Some check values:
    • The 10th triangular number (triangular(10)) is 55.
    • The 100th triangular number is 5050
    • The 1337th triangular number is 894453.


    Note: you must do this problem using a for loop to add the numbers; even if you know a “shortcut” formula to find the sum, don’t use it.
    This assignment contains only 3 problems, but you will need to write 7 methods: the main method, plus 2 methods for each problem. For each problem, you will need to write one method that takes one or more arguments and returns a value, but which DOES NOT PRINT ANYTHING (like the isLeapYear method in my MenuDemo file), and another method which handles user input and output, and which calls the other method (like the leapYear method in my MenuDemo file). Make sure to copy the MenuDemo code to your file to act as a structure for your assignment.

    This is what I have so far. I got errors. I don't even know if I'm headed the right direction.
    Java Code:
        public static void triangularNumber() {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter  number: ");
            int x = input.nextInt();
            int triangularNumber = (triangular(x));
            System.out.println("The" + x + "triangular number is" + addition);
        }
        
        public static int triangular(int n) {
            for (int total = 0; total < triangularNumber + 1;total++) {
                int addition = addition + total;
            }
            return addition;
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: For Loop Help! Completely Lost

    Please tell us your errors as they often are very helpful by telling you what you're doing wrong.

  3. #3
    whileloop is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: For Loop Help! Completely Lost

    The compiler can't find variable addition and triangularNumber. The variable addition can't be initialized.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: For Loop Help! Completely Lost

    You're running into what's known as a scope issue in that variables declared inside of a method (or any block for that matter such as an if block or for loop) are only visible inside of the method or block in which they've been declared. So since triangularNumber has been declared within the main method, it is visible only in the main method, and since addition has been declared inside of the for loop, it is only visible inside of the for loop. To fix addition, declare it *before* the for loop, assigning it initially equal to 0. For triangularNumber, perhaps you meant to use the n parameter inside of your method.

  5. #5
    Bushman is offline Member
    Join Date
    Apr 2012
    Location
    Spainish TOwn
    Posts
    25
    Rep Power
    0

    Default Re: For Loop Help! Completely Lost

    NO, your going in the wrong direction. You already have the triangle number, so you don't need any form of for loop to do anything.
    What you need is to transpose the formulate used to derive that triangle number, making the summator the subject.

    I found the formulae here:
    Calculate a triangle number

    NOW,
    where triangle number = i * ( i + 1 ) / 2,
    We want i, which is the number of positive integers that summate to form the triangle number.
    So, we need to isolate this i from the RHS[i * ( i + 1 ) / 2], so we need to make i the subject of
    this formulae."
    So:
    A.(i + 1)/2 = n/i
    B.(i + 1)/2 = n/i*2;
    C.i= 2n/i
    D.2n=i^2
    E.i=root(2n) DONE!!!


    PHP Code:
    public class triangleNumber_NthFinder
    {
        public static void main ( String [ ] args )
        {
            int nth = 0, triangle_number = 0;
            java.util.Scanner scanner = new java.util.Scanner ( System.in );
            //get user input
            System.out.print ( "Enter a triangle number : " );
            triangle_number = scanner.nextInt ( );
            //compute nth value
            nth = ( int ) Math.sqrt ( 2 * triangle_number );
            //show result
            System.out.print ( "result : " + nth );
        }
    }
    Last edited by Bushman; 04-17-2012 at 07:40 AM. Reason: forgot to add important note

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: For Loop Help! Completely Lost

    NO, your going in the wrong direction. You already have the triangle number
    But the OP's question said "Write and test a function
    int triangular(int n)
    which returns the nth triangular number".

    That is, the triangular number has to be calculated.

    I found the formulae here:
    Calculate a triangle number
    And it's a good formula, but "Note: you must do this problem using a for loop to add the numbers; even if you know a “shortcut” formula to find the sum, don’t use it".

  7. #7
    Bushman is offline Member
    Join Date
    Apr 2012
    Location
    Spainish TOwn
    Posts
    25
    Rep Power
    0

    Default Re: For Loop Help! Completely Lost

    Lol, I've been misreading posts on many forums since all of today. I haven't gotten any sleep.....it wouldn't be a shortcut formulae if the only the nth value was needed, here go a loop wouldn't be needed. But anyhow, the site i linked the OP to should give the exact java code required to do this.

  8. #8
    Bushman is offline Member
    Join Date
    Apr 2012
    Location
    Spainish TOwn
    Posts
    25
    Rep Power
    0

    Default Re: For Loop Help! Completely Lost

    Anyway, you just need to declare all variables that give you an error, such that all blocks can access them.

    HEre is a quickly written solution for you:
    PHP Code:
    public class trinagleNumberFinder
    {
        public static void main(String[] args) 
        {  
            int summation = 0, nth =0;
            java.util.Scanner scanner = new java.util.Scanner ( System.in );
            System.out.print ( "Enter nth number : " ); nth = scanner.nextInt ( );
            for (int i = 0; i < nth; i++) 
                summation += ( i + 1 );
            System.out.println ( "triangle number(summation 1 to nth) : " + summation );
        }
    }

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: For Loop Help! Completely Lost

    declare all variables that give you an error, such that all blocks can access them.
    As, I think, had already been said.

Similar Threads

  1. Help plz: not completely working JAR
    By linkingabo in forum NetBeans
    Replies: 6
    Last Post: 01-28-2012, 04:32 AM
  2. HELP!!! Completely confused on how to fix this program!!!
    By BeginnerJava in forum New To Java
    Replies: 17
    Last Post: 06-12-2011, 11:59 PM
  3. completely new
    By battosai16 in forum New To Java
    Replies: 1
    Last Post: 08-05-2010, 07:43 PM
  4. Completely new to Java problem
    By Seamo14 in forum New To Java
    Replies: 4
    Last Post: 10-09-2008, 12:50 PM
  5. I am completely stuck
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-14-2007, 06:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •