|
||||||
| General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
| Sponsored Links |
|
|
|
|||||
|
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! |
|
|||||
|
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 :/ |
|
|||||
|
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. |
|
|||||
|
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. |
| Sponsored Links |
|
|
|
|||||
|
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. |
|
|||||
|
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. |
|
|||||
|
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. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |
Goal: 100,000 Posts
Complete: 98%