Lost Password?


Go Back   CodeCall Programming Forum > Software Development > General Programming

General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-06-2006, 02:48 PM
Fischerspooner's Avatar   
Fischerspooner Fischerspooner is offline
Newbie
 
Join Date: Dec 2006
Posts: 27
Rep Power: 8
Fischerspooner is on a distinguished road
Default Moving elements in an array

Hi,

I'm currently studying Java on my own and I wanted to try to "rotate" elements in an array.
So the start would be:
{5,3,8,7,7,2,0,6,1,4}
But if I enter "rotate 1" it should give me
{4,5,3,8,7,7,2,0,6,1}

But I keep running into ArrayIndexOutOfBounds errors if I check my pseudocode in Java.

I don't want some code, just a hint or two would be very much appreciated

This is what I have already:
Code:
for(t=DIM-1 ; t > rot ; t--)
{
     if(t < DIM)
     {
        k = table[t];
        table[t-1] = table[t];
        table[t+1]= k;
     }
     else
     {
        k = table[t];
        table[t-1] = table[t-DIM];
        table[t+1] = k;
     }
}

Last edited by Fischerspooner; 12-06-2006 at 04:34 PM. Reason: forgot the change some vars
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-06-2006, 04:30 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

Without doing the code for you, I suggest using a debugger and stepping through the function. You will then be able to see where the object out of bounds is coming from.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-07-2006, 06:31 AM
Fischerspooner's Avatar   
Fischerspooner Fischerspooner is offline
Newbie
 
Join Date: Dec 2006
Posts: 27
Rep Power: 8
Fischerspooner is on a distinguished road
Default

I ran it through the debugger but I still cant find my mistake. I'm just overlooking something.

Some pseudocode:

temp = table[i+rot]; // remember what was on i+rot
table[i] = table[i+rot] // change the place of element i to i+rot
if (i+rot < table.length)
start from 0

My head is all messed up :/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-07-2006, 12:46 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,276
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default

At a guess, you are trying to push beyond the beginning/end of your array. One note: t < DIM is always true.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-07-2006, 05:44 PM
Fischerspooner's Avatar   
Fischerspooner Fischerspooner is offline
Newbie
 
Join Date: Dec 2006
Posts: 27
Rep Power: 8
Fischerspooner is on a distinguished road
Default

I used a second array to rotate and it works. Is it even possible with one array?

Code:
for(t=0 ; t < DIM ; t++)
{
   if(t+rot < DIM)
  {
       table2[t+rot] = table[t];
  }
  else
  {
       table2[t+rot-DIM] = table[t];
   }
}

Last edited by Fischerspooner; 12-08-2006 at 06:45 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 12-08-2006, 12:48 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,276
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default

It's possible, but you have to be careful around the beginning/end of the array.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-08-2006, 01:55 PM
Fischerspooner's Avatar   
Fischerspooner Fischerspooner is offline
Newbie
 
Join Date: Dec 2006
Posts: 27
Rep Power: 8
Fischerspooner is on a distinguished road
Default

Quote:
Originally Posted by WingedPanther View Post
It's possible, but you have to be careful around the beginning/end of the array.
Could you post some code? I know I didn't ask for code but I'm eager to learn...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-11-2006, 12:51 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,276
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default

Code:
#include <iostream>

int GCF(int a,int b)
{
 int quot, mod;
 do
 {
  quot = a/b;
  mod = a%b;
  a = b;
  b = quot;
 } while (mod != 0);
 return a;
}
    
int main()
{
 const int DIM=9;
 int table[DIM];
 int rot=3;
 int loops = GCF(DIM,rot);
 for (int i=0;i<DIM;i++) table[i] = i+1;
 for (int i=0;i<DIM;i++) std::cout << table[i] << " ";
 std::cout << "\n";
 for (int i=0;i<loops;i++)
 {
  int temp=table[i];
  for (int j=(i+rot)%DIM;j!=i;j=(j+rot)%DIM) 
  {
   table[(j-rot)%DIM]=table[j];
  }
  table[DIM+(i-rot)%DIM]=temp;
 }
 for (int i=0;i<DIM;i++) std::cout << table[i] << " ";
 std::cout << "\n";
 return 0;
}
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 12-11-2006, 01:30 PM
Fischerspooner's Avatar   
Fischerspooner Fischerspooner is offline
Newbie
 
Join Date: Dec 2006
Posts: 27
Rep Power: 8
Fischerspooner is on a distinguished road
Default

Thanks, I'll study it
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 12-12-2006, 09:24 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,276
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default

Here's an (possibly) interesting thing to think about: What are the time/space advantages/costs of each method of rotating an array?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fread into array position kenna C and C++ 0 08-17-2007 09:03 AM
Python 2D array question annannienann Python 3 04-23-2007 05:36 PM
Is string in array? smith Perl 2 02-14-2007 02:30 PM
Directory Size John PHP Forum 5 08-06-2006 02:02 PM


All times are GMT -5. The time now is 12:58 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 98%

Ads