By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,723 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

Question about 2d arrays

P: 12
Hey guys, I hate to bother you all, but I had a HW assignment last week that I couldn't get no matter what values (variables) I tried and according to my professor, it's too easy to have to go over in class (I don't think so), so even though I failed the homework, I have no idea on "the right way to do it" and I'm worried that I might be faced with a similar problem on the test in October. Any help in assisting me with the search and delete methods in this class would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. public class ArrayNew
  2.    {
  3.       private double[][] b;
  4.       public ArrayNew(int x, int y)
  5.       {
  6.          b = new double[x][y];
  7.  
  8.       }
  9.  
  10.        public void insert (int x, int y, int num)
  11.       {
  12.          b[x][y] = num;
  13.  
  14.       }    
  15.  
  16.        public boolean find(int searchKey)
  17.       {
  18.            int row = 0;
  19.             int column = 0;
  20.          for (row = 0; row < b.length-1; row++)
  21.             {
  22.                 for (column = 1; column < b[row].length; column++)                  
  23.                 if(b[row][column] == searchKey)
  24.                   break;                  
  25.           }
  26.           if(row == b.length-1 && column == b[row].length)
  27.              return false;
  28.          else
  29.             return true;
  30.  
  31.         }
  32.  
  33.  
  34.        public boolean delete (int value)
  35.       {
  36.          int i = 0;
  37.          int j = 0;
  38.          for (i = 0; i < b.length-1; i++)
  39.             for (j = 0; j < b[i].length; j++)
  40.                      {          
  41.                     if (b[i][j] == value)
  42.                   break;
  43.                    }      
  44.             if (i == b.length-1 && j == b[i].length )
  45.             return false;
  46.          else
  47.          {
  48.             for (int k = i; k < b.length-1; k++)        
  49.                     for (int h = j; h < b[k].length; h++)
  50.                   {            
  51.                         b[k][h] = b[k+1][h+1];     
  52.                       }     
  53.  
  54.                 return true;
  55.  
  56.             }
  57.          }
  58.        public void display()
  59.       {
  60.          for (int i = 0; i < b.length; i++)
  61.             {
  62.             for (int j = 0; j < b[i].length; j++)
  63.             {   
  64.                     System.out.print(b[i][j] + " ");
  65.                   }       
  66.             System.out.println("");  
  67.       }    
  68.  
  69.    }
  70.  
  71.    }
The original assignment was to create a 2d array using search, delete, and insertion methods, duplicates were not to be considered. Now I've gotten search to where it will show values from the 2nd row onward, but it will not show any values from the first row. Delete will not work at all. Oh and if there is a way to do either method without a nested for loop, please say something, I don't want you to do it for me, just give me a hint or two. Thanks in advance
Sep 2 '08 #1
Share this Question
Share on Google+
3 Replies


myusernotyours
100+
P: 188
Hi there,

When searching do not initialise column in the inner loop to 1. That will skip the value at index [0][0] which is not what you want to do. Just do it like in the outer loop and remember
Expand|Select|Wrap|Line Numbers
  1.  b.length() -1 
.

In the delete part you are breaking out of the inner loop but the outer one is still on. this ruins the i and j counters so they are equal to
Expand|Select|Wrap|Line Numbers
  1.  b.length() -1 
and it therefore returns false. Try stepping through the code or print out the values in the variables as you go.

Regards
Alex.
Sep 3 '08 #2

P: 12
Thank you very much, that helped a lot!
Sep 4 '08 #3

P: 2
Hi guys , I have the same kind of coursework, i need some help how design it,
sorry guys i am not asking you to do for me i need just the right direction how to do it, i have drawn relationship diagram but i dunno where to put right methods and how create relationship ************************************************** **************************


You are required to write a 'level' editor for a simple game called CharWorld. A level in CharWorld is a room made up from 10 x 10 squares. Each square in the room is referred to by an X and a Y coordinate. Hence the '@' in the room shown below is placed at square (1,3):

0 1 2 3 4 5 6 7 8 9
0 . . . . . . . . . . . .
1 . . . . . . . . . . . .
2 . . . . . . . . . . . .
3 . @ . . . . . . . . .
4 . . . . . . . . . . . .
5 . . . . . . . . . . . .
6 . . . . . . . . . . . .
7 . . . . . . . . . . . .
8 . . . . . . . . . . . .
9 . . . . . . . . . . . .

A room in CharWorld can be populated by two types of entities: inanimate objects and living things. There are two kinds of inanimate objects: obstacles which are represented by 'O' (capital 'o') and treasure chests, which are represented by a '*'. Treasure chests can contain up to 100 gold coins.

Each living thing in CharWorld has a measure of its health, represented using an integer in the range 0 to 100. Living things are divided in to monsters and humans. Monsters are represented in the display by a '#' character whilst humans are depicted by an '@' character. The amount of damage that a particular monster can cause a human depends on its strength which is represented by a number from 1 to 20. Similarly, the amount of damage that a human can cause a monster depends on his or her strength, which is represented by a number in the range 1 – 10. Each human also has a name.

A typical level for a game would look like the following:

0 1 2 3 4 5 6 7 8 9
0 . . . . . . . . . . . .
1 . . . . . # . . . . .
2 . . . O . . . . . . .
3 . @ . O . . . * . .
4 . . . O . . . . . . .
5 . . O O . . . . . .
6 . . . . . . . @ . . .
7 # . . . . . . . . . . .
8 . . . # . . . . . . . .
9 . . . . . . . . . . . .

This shows 1 treasure chest, 2 humans, 3 monsters and 5 obstacles on this level. Note that a full stop is used to denote an unoccupied square in the level.

Write an object oriented program which will enable a level designer to add and delete entities to and from a given level. Initially, the level will have no entities in it. Your program should be controlled using a simple console based menu similar to the one shown below:

Menu:
1 Add a human
2 Add a monster
3 Add an obstacle
4 Add a treasure chest
5 Delete an entity
6 Display level
7 Display the properties of an entity
8 Print menu
9 Exit

When adding an entity to the level, the user should be prompted for the X and Y coordinates of the square on which to place the entity. Note that an entity can only be placed on an empty square. Note also that the user should be prompted for the relevant details to be entered depending on which entity is being added (e.g. to add a human, the user should be prompted to enter the human’s name, their health as an intiger in the range 0 to100 and their strength as an integer in the range of 1 to 10).

When deleting an entity, it is only necessary to identify the square the entity occupies using its X and Y coordinate.

The 'Display level' option should display the current contents of the room to the console output using a format similar to that shown above.

When item 7 on the menu is selected, the user should be prompted for the X and Y coordinates of the square on which the entity in question is located. Your program should then display all the details of the entity on that square. For example, if square (6,7) was selected from the display shown at the top of this page, then the output might be as follows:

Entity properties:
Type: human
Name: Harold
Health: 47
Strength 5

If there is no entity on that square then your program should output a suitable message

************************************************** ****************************
Oct 22 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.