Apr 11, 2011 at 2:35am UTC
May I see the declaration of c?
Edit: Actually, I think I might need to see the whole code. The value of K is apparently not within the bounds of your array.
Last edited on Apr 11, 2011 at 2:37am UTC
Apr 11, 2011 at 2:36am UTC
You're making that a lot harder than it needs to be, though I'm guessing you have your reasons. :|
for (int i=K;i>=0 ;i--)
When i = 0, what is K - i going to be equal to? Remember, array indexes go only up to size - 1.
-Albatross
Last edited on Apr 11, 2011 at 2:36am UTC
Apr 11, 2011 at 2:40am UTC
ahhhh ok yea thats it thank you haha i knew it was something obvious
Apr 11, 2011 at 2:41am UTC
change your condition from a >= sign to a > sign
Apr 11, 2011 at 2:43am UTC
well here is what my main is atm and its still faulting in the same place, but would i want to keep the >= so it puts a number in the sot zero of the array?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
int main()
{
const int K = 100000; // Number of items to be sorted.
unsigned seed = time(0);
// Seed the random number generator.
srand(seed);
double t1, t2,elapsed_time;
int a[K];
int a2[K];
int b[K];
int b2[K];
int c[K];
int c2[K];
for (int i=0;i<K;i++)
a[i]=rand()%100;
for (int i=0;i<K;i++)
b[i]=i%100;
for (int i=K-1;i>0;i--)
c[(K-1)-i]=i%100;
t1=clock();
mergeSort(a, a2, K);
t2=clock();
elapsed_time=t2-t1;
cout<<"The elapsed time was " <<elapsed_time/CLOCKS_PER_SEC<<" seconds" <<endl;
print(a,K);
t1=clock();
mergeSort(b, b2, K);
t2=clock();
elapsed_time=t2-t1;
cout<<"The elapsed time was " <<elapsed_time/CLOCKS_PER_SEC<<" seconds" <<endl;
print(b,K);
t1=clock();
mergeSort(c, c2, K);
t2=clock();
elapsed_time=t2-t1;
cout<<"The elapsed time was " <<elapsed_time/CLOCKS_PER_SEC<<" seconds" <<endl;
print(c,K);
system("PAUSE" );
return 0;
}//end main
Last edited on Apr 11, 2011 at 2:52am UTC
Apr 11, 2011 at 2:54am UTC
I tried your code and I'm having memory issues. Try using shorts instead of ints for your arrays.
1 2
for (int i=K-1;i>0;i--)
c[(K-1)-i]=i%100;
Also, here you won't access the last element of your array,
despite what ascii said I believe you want
i >= 0, or c[K-i] as Albatross said below.
Last edited on Apr 11, 2011 at 3:04am UTC
Apr 11, 2011 at 2:56am UTC
Aw, why'd you have to change the K to (K-1) in that loop? Now you're missing the element at the 99999th position. :(
I'm still not sure why you had to make the loop the way you did.
That aside, I can't put my cursor finger on any obvious faults in your code.
-Albatross
Apr 11, 2011 at 3:01am UTC
Albatross how would you code it? what i did seemed to be the most trivial way to me to do it.
Apr 11, 2011 at 3:06am UTC
If I wanted a loop that iterated over an array from 0 to size - 1, I might do what you did in the loops for a and b.
If I wanted a loop that iterated over an array from size - 1 to 0 (is this what you wanted?), I might consider writing something like this:
1 2
for (int i = K - 1; i >= 0; --i)
c[i] = (K-1-i)%100;
I see why you had the loop you had.
-Albatross
EDIT: Ack, missed a question mark.
Last edited on Apr 11, 2011 at 3:09am UTC
Apr 11, 2011 at 3:10am UTC
Did you try what I said? ^see above^
Edit: If that doesn't work you could even try using char .
Last edited on Apr 11, 2011 at 3:14am UTC
Apr 11, 2011 at 3:13am UTC
so after further investigation the segmentation fault ends when i remove the declaring of int c[];and c2[];
Apr 11, 2011 at 3:14am UTC
yea i havnt im trying to see if i can fix it with out doing that because thats alot of code i have to change to make them shorts
Apr 11, 2011 at 3:16am UTC
No, it's not a lot of code to change, you only have 6 arrays ;). You could use Find and Replace if you wanted.
Apr 11, 2011 at 3:19am UTC
i have to change all of my functions as well to work with shorts instead of just ints
Apr 11, 2011 at 3:21am UTC
ok so the problem was it doesnt like me making 6 arrays of size 100000
Apr 11, 2011 at 3:21am UTC
Find&Replace is great for this sort of thing. I really think this will resolve your problem.
And yes, that is the problem, since shorts use less memory, it will probably work. At least it worked when I tried it.
Last edited on Apr 11, 2011 at 3:25am UTC
Apr 11, 2011 at 3:34am UTC
cant a short int only go up to the number like 40,000 or something like that though? so if thats the case then i cant use 100000 anyway
Apr 11, 2011 at 3:41am UTC
a[i]=rand()%100;
You're values only go from 0-99.
const int K = 100000;
This will still be an int, and can hold the value of 100000.
So shorts will still work, with less memory, and therefore little chance of a seg-fault unless your computer is a dinosaur.
@Anybody: Can you please back me up on this? The OP doesn't seem to believe me :-(.
Again:
I wrote:it worked when I tried it.
Of course, I could only test your code up to line 29, but guess what. I didn't get a seg-fault!
Last edited on Apr 11, 2011 at 3:42am UTC