Results 1 to 18 of 18
- 06-29-2011, 12:28 PM #1
Help in Multidimensional Arrays :(
Hi.. I need your help in jcreator with Arrays...
.gif)
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
- 06-29-2011, 12:39 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.)
- 06-29-2011, 12:48 PM #3
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..
- 06-29-2011, 01:01 PM #4
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 . . . ");
}
}
- 06-29-2011, 01:29 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Please use code tags
[code]
YOUR CODE HERE
[/code]
- 06-29-2011, 01:38 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 06-29-2011, 02:02 PM #7
See also; [Help] Arrays and MagicSquare
- 06-29-2011, 02:03 PM #8
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.. @__@
- 06-29-2011, 02:07 PM #9
I'd put it at the location in your code that you want a String representation of the array.i dont know where to put Arrays.toString()..
Probably in a println() if you want to see the value on the console.
- 06-29-2011, 02:13 PM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
Pseudocode for the assignment looks like thisJava Code:int[] x = {1, 2, 3}; String s = Arrays.toString(x); System.out.println(s);
I'll leave translating it to you.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
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.
- 06-29-2011, 02:17 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 06-29-2011, 02:58 PM #12
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 newbLast edited by sexy_programmer; 06-29-2011 at 03:17 PM.
- 06-29-2011, 03:37 PM #13
Before you try to write code for this, you need an algorithm that solves the problem.fill the array with random numbers that sum up the same number in each row and columns
When you get that, then you write the code to follow the algorithm.
Deleting an element requires moving all the elements at a higher index down one.deleting an element from an array
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.
- 06-29-2011, 04:30 PM #14
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..
- 06-29-2011, 04:33 PM #15
You move elements in an array inside of a loop. You need two indexes, the source and the target.what is the code in deleting the object
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.
- 06-29-2011, 04:46 PM #16
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 =(
- 06-29-2011, 04:49 PM #17
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.
- 06-29-2011, 04:54 PM #18
Similar Threads
-
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
How to persist multidimensional Java arrays using JPA
By T_O_B_E_E in forum JDBCReplies: 0Last Post: 02-14-2011, 03:24 PM -
Multidimensional Arrays
By holytanx in forum New To JavaReplies: 1Last Post: 08-05-2010, 10:28 PM -
Make Java codes more simplier (Multidimensional Arrays)
By javanewbie in forum JCreatorReplies: 9Last Post: 06-25-2008, 04:48 AM -
Multidimensional arrays
By Java Tip in forum Java TipReplies: 0Last Post: 11-05-2007, 05:07 PM


LinkBack URL
About LinkBacks
Reply With Quote.gif)


Bookmarks