473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Question about 2d arrays

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
3 1580
myusernotyours
188 100+
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
JinFTW
12
Thank you very much, that helped a lot!
Sep 4 '08 #3
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

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

Similar topics

7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
19
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
5
by: Wajih-ur-Rehman | last post by:
The question is about C++ (since its the C family, i posted it on this newsgroup) Lets say i declare an array int a = {1,2,3,4}; int *p = a; //This is allowed because "a" returns the address of...
3
by: James dean | last post by:
I have created algorithms in C# unsafe code and have fixed the arrays in memory for optimum performance. I use multidimensional arrays rather than jagged arrays. The algorithms i use usually read a...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
2
by: phpCodeHead | last post by:
Hello fellow codemeisters! I am needing to parse through two lists of serial numbers for parts being received into an inventory database. First, is the list of serial numbers already in the...
31
by: mdh | last post by:
I am still having a problem understanding K&RII on p 112. I have looked at the FAQs --which I am sure answer it in a way that I have missed, so here goes. A 2-dim array, (per K&R) is really a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.