Results 1 to 5 of 5
  1. #1
    dekelm12 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default I need help on solving an exercise from school

    Hi,
    I began my journey in Java a few months ago so I'm a beginner.
    I learn it in my school.
    Anyway I got an assignment that I need help with.
    How do I do the following assignment?

    Write a code that inputs 10 numbers into a one-dimensional alignment - a.
    The code will define a new alignment - b which is made of the alignment a in the power of 2.
    The code will define an alignment - c, which is made of the product of the number in alignment - a with the numbers in alignment - b.
    The code will output in the first row the numbers of alignment - a.
    In the second row, the numbers of alignment - b will be outputted.
    In the third row, the numbers of alignment - c will be outputted.

    That's it :) Thanks!

    Java Code:
    import java.util.Scanner;
    public class daf167
    {
      public static void main(String[]args)
      {
        Scanner in= new Scanner(System.in);
        int[] a=new int[10];
        int[] b=new int[10];
        int[] c=new int[10];
        int i;
        for(i=0;i<=9;i++)
        {
          System.out.println("Enter a number");
        a[i]=in.nextInt();
        for(i=0;i<=9;i++)
          b[i]=a[i]*a[i];
        for(i=0;i<=9;i++)
          c[i]=a[i]*b[i]
        }
        System.out.println("The first row - a is" +a[i]);
        System.out.println("The second row - b is" +b[i]);
        System.out.println("The third row - c is" +c[i]);
      }
    }
    This is what I've done. What's wrong with it? It doesn't work.

    By the way, do you have any tips for Java beginners? :)
    Last edited by dekelm12; 12-31-2010 at 01:16 PM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    well, let's look at what an input and output might look like.

    enter 10 numbers
    4 5 1 6 3 8 0 9 13 11

    alignment a: 4 5 1 6 3 8 0 9 13 11
    alignment b: 16 25 1 36 9 64 0 81 169 121
    alignment c: 64 125 1 216 27 512 0 729 1331
    note that having row b product with row a is really just row a ^ 3. but what ever.

    so I am thinking of 3 arrays of integers, or double types.

    using the "Scanner" class to read input from the user, and invoking nextInt() to parse the tokens entered on the line into the first array a[], using a for() loop probably.

    then in a second for() loop, perform the a[i] * a[i] operation, storing into b[i].

    then in a third for() loop, perform c[i] = a[i] * b[i];

    then in a fourth for() loop, print out the contents of a into a line

    a fifth for() loop to print out the contents of line b

    a sixth for() loop to print out the contents of line c

    well, all the looping purely for academic clarity here;

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    extra points for style using the StringBuilder to construct outputs and using tab characters to format into pretty columns.

  4. #4
    dekelm12 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    public class daf167
    {
      public static void main(String[]args)
      {
        int[] a=new int[10];
        int[] b=new int[10];
        int[] c=new int[10];
        int i;
        for(i=0;i<=9;i++)
        {
          System.out.println("Enter a number");
        a[i]=in.nextInt();
        for(i=0;i<=9;i++)
          b[i]=a[i]*a[i];
        for(i=0;i<=9;i++)
          c[i]=a[i]*b[i]
        }
        System.out.println("The first row - a is" +a[i]);
        System.out.println("The second row - b is" +b[i]);
        System.out.println("The third row - c is" +c[i]);
      }
    }
    That is what I've done, what's wrong with it? It doesn't work.
    Last edited by dekelm12; 12-31-2010 at 01:15 PM.

  5. #5
    dekelm12 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Solving Collisions in a car game
    By Rectal Exambot in forum Advanced Java
    Replies: 1
    Last Post: 09-29-2010, 03:13 PM
  2. Help Needed in Solving the Following Isuue.
    By raju.i in forum Advanced Java
    Replies: 3
    Last Post: 05-14-2010, 06:01 PM
  3. Solving a Maze
    By bdario1 in forum New To Java
    Replies: 4
    Last Post: 04-14-2010, 01:02 AM
  4. I need help solving this problem.
    By Felicia in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 09:40 PM
  5. Pls help me in solving this java question
    By dunnoGUy in forum New To Java
    Replies: 1
    Last Post: 02-28-2010, 08:10 AM

Posting Permissions

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