Results 1 to 18 of 18
  1. #1
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Unhappy Help in Multidimensional Arrays :(

    Hi.. I need your help in jcreator with Arrays...
    ive been working on these programs but still cant get it..
    Please help me..

    Activity 1:
    Create class ArrayApp with 20 elements. The program must ask the user to input an element to search for and prints message " Found[search key] " Then prints the data eliminating the searched element. Otherwise, print "[search key] not Found".
    Below is the sample output you should acquire.

    Output:

    77 99 44 55 22 88 11 0 66 33
    Found 66.

    77 99 44 55 22 88 11 0 33
    ************************************************** ***********
    Activity 2:
    Create a MagicSquare with [7][7] dimension
    The magic Square should display a magic square below

    Output:


    28 19 10 1 48 39 30
    29 27 18 9 7 47 38
    37 35 26 17 8 6 46
    45 36 34 25 16 14 5
    4 44 42 33 24 15 13
    12 3 43 41 32 23 21
    20 11 2 49 40 31 22

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

    Default

    Also posted to the JCreator forum.

    @OP: I'm sure you realise that no-one here will write code for you. To make progress you need to describe (probably with code) what you have done and what the problem is.

    For example the first task involves creating an array with 20 elements. What happened when you tried this? Were there compiler errors? If not, was there unexpected behaviour when you ran your program? Such errors and runtime behaviour are what constitute a problem: posting the code and describing the results are what is involved in describing the problem.

    (If you are stuck with even the first steps - declaring a class and giving it a main() method so you have something to run then your textbooks will be more use than a forum, I suspect.)

  3. #3
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    import java.util.*;

    public class ArrayApp{
    public static void main(String[] args)throws Exception {
    Scanner scr = new Scanner(System.in);
    int choice;

    int anArray[]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
    System.out.print("Input an Element from 1-20 : ");
    choice =Integer.parseInt(scr.next());


    System.out.println("Found "+ choice);
    if (choice<21){
    }
    System.out.println(anArray);
    else{
    System.out.println(choice +" not Found");
    }


    }
    }


    This is what i came up in activity 1.. :( there is no errors but i dont know how to display all the elements of the array and eliminate the entered number from the array list..

  4. #4
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    this is my 2nd Activity: (the error is Incompatible type)
    i even dont know what im doing with this..

    import java.util.Scanner;
    public class Magic {

    public static void main(String[] args) throws Exception{
    Scanner xan=new Scanner(System.in);

    int [][] a= {{28, 19, 10, 1, 48, 39, 30},{29, 27, 18, 9, 7, 47, 38},{37, 35, 26, 17, 8, 6, 46},
    {45,36,34,25,16,14,5},{4,44,42,33,24,15,13},
    {12,3,43,41,32,23,21},{20,11,2,49,40,31,22}};

    a[0]=(a[0][0]+a[0][1]+a[0][2]+a[0][3]+a[0][4]+a[0][5]+a[0][6]);
    a[1]=(a[1][0]+a[1][1]+a[1][2]+a[1][3]+a[1][4]+a[1][5]+a[1][6]);
    a[2]=(a[2][0]+a[2][1]+a[2][2]+a[2][3]+a[2][4]+a[2][5]+a[2][6]);
    a[3]=(a[3][0]+a[3][1]+a[3][2]+a[3][3]+a[3][4]+a[3][5]+a[3][6]);
    a[4]=(a[4][0]+a[4][1]+a[4][2]+a[4][3]+a[4][4]+a[4][5]+a[4][6]);
    a[5]=(a[5][0]+a[5][1]+a[5][2]+a[5][3]+a[5][4]+a[5][5]+a[5][6]);
    a[6]=(a[6][0]+a[6][1]+a[6][2]+a[6][3]+a[6][4]+a[6][5]+a[6][6]);

    System.out.println("Press Any key to Continue . . . ");
    }
    }

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please use code tags

    [code]
    YOUR CODE HERE
    [/code]

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm gonna start by helping on activity one, do you know how to use a loop? A for loop is perfect for looping through an array, try looping until you find the search term. You may want to also use a loop to print the values. Printing the array directly will print the hex hash code of the array which is very un helpful to you.

    You can also print the array with the Arrays.toString() method which takes an array and returns a string of each element.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

  8. #8
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    sorry im just a newbie in this forum..

    thanks for your help..
    but could you please give me an example on how to print an array ..
    i dont know where to put Arrays.toString()..

    im gonna try the for loop.. i hope i can do it.. @__@

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    i dont know where to put Arrays.toString()..
    I'd put it at the location in your code that you want a String representation of the array.
    Probably in a println() if you want to see the value on the console.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Looping is fairly easy to print an array, each step through the loop you print the value at the index(remember array indexes start at 0). The Arrays.toString looks like
    Java Code:
    int[] x = {1, 2, 3};
    String s = Arrays.toString(x);
    System.out.println(s);
    Pseudocode for the assignment looks like this
    Java Code:
    Get input
    Print array
    loop
      if(item is input)
        Set flag
    end loop
    if flag
      print item, remove item, print changed array
    end if
    else
      print not found
    end else
    I'll leave translating it to you.

    Also, the glasses in your avatar are quite oversized looking.

    Don't post more than one thread as others have said, some people will be less helpful. Other than that, I hope you enjoy it here. Dont hesitate to ask for help, just make sure to show effort.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, can you clarify activity 2? Is it meant to just fill the 2d array with random numbers? Or is there some pattern that isn't jumping out at me?

  12. #12
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks so much guys.. im slow in making program so im gonna try this out today..

    in activity 2 u fill the array with random numbers that sum up the same number in each row and columns.. its kinda difficult...

    i would also like to add questions about deleting an element from an array and how to display all the elements in horizontal form.. it always goes down :( im sorry im a perfect newb
    Last edited by sexy_programmer; 06-29-2011 at 03:17 PM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    fill the array with random numbers that sum up the same number in each row and columns
    Before you try to write code for this, you need an algorithm that solves the problem.
    When you get that, then you write the code to follow the algorithm.

    deleting an element from an array
    Deleting an element requires moving all the elements at a higher index down one.
    For example: Given:{2,3,4} if you delete the second element (3) then the array would be: {2,4,?}
    The value of the ? would depend on the type of array. For primitives it should be the default value. For objects it should be null.

  14. #14
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    thanks Mr. Moderator.. :)

    But what is the code in deleting the object ?? ive been searching for the code since yesterday and i cant get the right one.. :(


    ill make the nxt activity tomorrow.. coz im stuck up with this..

    i really do appreciate your help
    thanks everyone..

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    what is the code in deleting the object
    You move elements in an array inside of a loop. You need two indexes, the source and the target.
    Take a piece of paper, write down an array, write arrows to the elements to be moved and look at the logic required to move an element, change the pointers and to stop at the end of the array.

  16. #16
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    the thing is the user will input an element to be removed..i dont know what elements to move coz my prof will enter random values =(

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    You would use a loop to search the array for the element to remove. When you get the index of the element to remove that would be the target index I spoke of before.

  18. #18
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    thanks Norm.. you're a great help.. really..
    ill finish it tomorrow since its already late here
    ill come back for more questions haha
    goodnyt!

Similar Threads

  1. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  2. Replies: 0
    Last Post: 02-14-2011, 03:24 PM
  3. Multidimensional Arrays
    By holytanx in forum New To Java
    Replies: 1
    Last Post: 08-05-2010, 10:28 PM
  4. Replies: 9
    Last Post: 06-25-2008, 04:48 AM
  5. Multidimensional arrays
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 05:07 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
  •