473,385 Members | 1,536 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,385 software developers and data experts.

Ordered arrays

12
Hey guys I hate to bother you with another question so soon, but I've set up a 2d ordered array, and while my delete and search methods work okay, my display/insertion methods have to be missing something, or perhaps I've used too much information. Don't get me wrong, the 2d array in my driver is in ascending order like it should be, but the elements are not in the right coordinates. The only one that is placed right is 0,0. Everything else just shows up in 1,1 or 2,2, or 3,3, or 4,4, instead of where they are supposed to go. Any hints would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  class ArrayBinary                        
  3.    {
  4.       private int[][] a;        
  5.       private int nElems;        
  6.  
  7.        public ArrayBinary (int x, int y) 
  8.       {
  9.          a = new int[x][y];            
  10.          nElems = 0;
  11.       }
  12.  
  13.        public int size()
  14.       {
  15.          return nElems;                    
  16.       }
  17.  
  18.        public int find(int searchKey)
  19.       {
  20.          int lowerBound = 0;
  21.          int upperBound = nElems-1;
  22.          int middle;
  23.  
  24.          while(true)
  25.          {
  26.             middle = (lowerBound + upperBound) / 2;
  27.             if(a[middle][middle] == searchKey)
  28.                return middle;                                        //found it
  29.             else if (lowerBound > upperBound)
  30.                return nElems;    
  31.             else
  32.             {
  33.                if (a[middle][middle] < searchKey)
  34.                   lowerBound = middle + 1;                    
  35.                else
  36.                   upperBound = middle - 1;                    
  37.          }            // ends find
  38.       }        
  39.        public void insert (int x, int y, int value)        
  40.       {
  41.        int i;                                                        
  42.          for (i =0; i < nElems; i++)            
  43.             if(a[i][i] > value)
  44.                break;
  45.          for (int j = nElems; j > i; j--)
  46.             a[j][j] = a[j-1][j-1];            
  47.          a[i][i] = value;                    
  48.          nElems++;                            
  49.       }
  50.  
  51.  
  52.  
  53.        public boolean delete(int value)
  54.       {
  55.          int i = find(value);
  56.          if(i==nElems)
  57.             return false;                    
  58.          else
  59.          {
  60.             for(int j = i; j < nElems; j++)
  61.                a[j][j] = a[j+1][j+1];                    
  62.             nElems--;                            
  63.             return true;
  64.          }
  65.       }        // end delete
  66.  
  67.        public void display()                    
  68.       {
  69.          for (int i = 0; i < a.length; i++)
  70.          {
  71.             for (int j = 0; j < a[i].length; j++)
  72.             {   
  73.                System.out.print(a[i][j] + " ");
  74.             }       
  75.             System.out.println("");
  76.          }
  77.  
  78.       }
  79.    }    
  80.  
Sep 4 '08 #1
2 1422
JosAH
11,448 Expert 8TB
but the elements are not in the right coordinates. The only one that is placed right is 0,0. Everything else just shows up in 1,1 or 2,2, or 3,3, or 4,4, instead of where they are supposed to go. Any hints would be greatly appreciated.
That doesn't surprise me because the only elements you consider are a[x][x]
for any value of x; (see your own code).

kind regards,

Jos
Sep 5 '08 #2
JinFTW
12
Ah thank you for the advice, I reworked the insert code and I've come up with this, but now I can't get it to compile at all.

Expand|Select|Wrap|Line Numbers
  1. public void insert (int x, int y, int value)        // puts element into array
  2.       {
  3.          int i;                                                        
  4.          int j;
  5.             int a;
  6.             int b;
  7.             for (i = 0; i < nElems; i++)            // finds where it goes
  8.                 for (j = 0; j < nElems; j++)        
  9.                 if(a[i][j] > value)
  10.                break;
  11.          for (a = nElems; a > i; a--)
  12.                 for (b = nElems; b > j; b--)                      
  13.                 a[a][b] = a[a-1][b-1];            // moves bigger ones up
  14.          a[i][j] = value;                    // inser it
  15.          nElems++;                            // increment size
  16.  
Sep 8 '08 #3

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

Similar topics

210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.