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 8 of 8

Thread: Help for Terminal Exams................

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Help for Terminal Exams................

    Plz help:
    1. Declare an array of int n and put the squares of the elements of the array at a 2nd array and the cubes of the original(1st array)
    in a third array.


  2. #2
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Help for Terminal Exams................

    How to use arrays in Java.

    As for the calculating the squares and cubes, you can use the Math.pow() method if you want, but since it's just squares and cubes, you can calculate that out pretty easily without, since the exponents are pretty low. I'd probably do that if I were writing this program, but since either route is pretty simple, it's 6 of one, half dozen of the other.

  3. The Following User Says Thank You to mstabosz For This Useful Post:

    arko_chatterjee (September 21st, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help for Terminal Exams................

    What help do you need?

  5. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help for Terminal Exams................

    thank you very much...

    --- Update ---

    the help that i needed is that how can i move the squares into another array

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help for Terminal Exams................

    There are 3 arrays required. You might call them:

    rootArray[]
    squaredArray[]
    cubedArray[]

    Then, for example, each element of squaredArray[] = rootArray[] squared. Similarly for cubedArray[], both of which can be filled in a single loop.

    If you still don't understand, please explain your confusion.

  7. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    5
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help for Terminal Exams................

    i hv written a code... it is working . but the factor is whenever i try to print the two arrays in a separate for loop it says array out of index
    (IMPORTANT: The Code which i have written computes the odd in one array and even numbers in another array)

    import java.util.*;
    class Odd_Even
    {
    void main()
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter Array Size");
    int n=sc.nextInt();
    int a1[]=new int[n];
    for(int i=0;i<n;i++)
    {
    System.out.println("Enter Elements");
    a1[i]=sc.nextInt();
    }
    int i,k,j;int e=0;int o=0;int t=0,m=0;
    int a2[]=new int[n];
    int a3[]=new int[n];
    for(i=0;i<n;i++)
    {
    if(a1[i]%2==0)
    {
    //e++;
    t= a1[i];
    a2[i]=t;
    // System.out.println("t="+t);
    System.out.print("a2[]"+a2[i]);
    System.out.println(" ");
    }
    else
    {
    //o++;
    m= a1[i];
    a3[i]=m;
    //System.out.println("m="+m);
    System.out.print("a3[]"+a3[i]);
    System.out.println(" ");
    }
    }


    // for(int s=0;s<=n;s++)
    // { This for loop says array out of index

    // System.out.println("a2[]"+a2[i]);
    //System.out.println("a3[]"+a3[i]);

    }
    }

  8. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help for Terminal Exams................

    Please post your code in format tags.

    The problem is probably with your for loop condition, s <= n. Try s < n.

  9. #8
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Help for Terminal Exams................

    Yeah like Greg said, your loop logic is wrong. If you declare an array of size 5, then its available elements are at 0, 1, 2, 3, and 4. If you loop through with something like for(i=0; i<=5; i++), then your loop looks at elements 0, 1, 2, 3, 4, and the nonexistent 5. It looks like you got that right in your first for loop but slipped up in the second one.

    You can also use a for-each loop to iterate through an array. The syntax is different, but it prevents out of bounds errors. If you had an array named numbers of 5 integers, to loop through with a for-each, you would write

    for(int each: numbers)
        System.out.println(each);

    You don't have to use "each" in there. You can call it whatever you want.

    Also, it helps to give your variables names more meaningful to their purpose. Names like "number" and "squareArray" make the code easier to follow and debug.

  10. The Following User Says Thank You to mstabosz For This Useful Post:

    arko_chatterjee (September 21st, 2013)

Similar Threads

  1. get terminal value back to ide?
    By bean in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 23rd, 2013, 07:01 PM
  2. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  3. Terminal Tamagotchi
    By FlurrYx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2011, 01:05 PM
  4. Need some help with OSX Java (Terminal)
    By mkoop in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 14th, 2011, 12:23 PM