- Mar 31, 2003
- 11,679
- 0
- 81
First off: THIS IS SCHOOLWORK. PLEASE DO NOT GIVE ME THE ANSWER- MERELY POINT ME IN THE RIGHT DIRECTION
Now that is out of the way - this is a painfully simply program in C:
#include <stdlib.h>
unsigned int reverseArray(int A[], unsigned int Lo, unsigned int Hi)
{
/*
This allocates enough space to fit the size of the array passed
into the function.
*/
int *temp = malloc (sizeof (int) *Hi);
int *index = temp;
// This is the number of swaps done in the function.
int numSwap=0;
// This loop switches the indices between Hi and Lo.
auto int i;
for(i=Hi; i>=Lo; i--)
{
// This is a pointer to the index of the temp array.
*index = A BRACKET I BRACKET;
// This increases the index and number of swaps.
index++;
numSwap++;
}
// Resets the index to the initial value.
index = index - numSwap;
// Fills the original array with the values that are now flipped.
for(i=Lo; i<=Hi; i++)
{
A BRACKET I BRACKET = *index;
index++;
}
// This returns the number of swaps done by the function.
return numSwap;
}
We submit this to an online grader which then spits out the result and docks point. The grader has a main function which it uses as a "test harness" where it tests a variety of conditions. I am not able to see the tests that it is going through.
Apparently my program is failing and the online grade is creating a stack dump. I cannot, for the life of me, find a test which causes my program to crash.
The Lo and Hi have to be within bounds of the array. The Lo and Hi have to be a Low Point and a High Point. Nothing I have been able to do in my testing has been able to produce incorrect results.
All my Professor said was that I had boundary issues. It all seems to work fine for me. Can anyone point me in the right direction here - because I can't figure out why such a stupidly simple program is causing me more than 5 minutes of work.
(I suspect it is a problem with my Dynamic Memory Management malloc() function if it is indeed boundary issues)
-Kevin
Now that is out of the way - this is a painfully simply program in C:
#include <stdlib.h>
unsigned int reverseArray(int A[], unsigned int Lo, unsigned int Hi)
{
/*
This allocates enough space to fit the size of the array passed
into the function.
*/
int *temp = malloc (sizeof (int) *Hi);
int *index = temp;
// This is the number of swaps done in the function.
int numSwap=0;
// This loop switches the indices between Hi and Lo.
auto int i;
for(i=Hi; i>=Lo; i--)
{
// This is a pointer to the index of the temp array.
*index = A BRACKET I BRACKET;
// This increases the index and number of swaps.
index++;
numSwap++;
}
// Resets the index to the initial value.
index = index - numSwap;
// Fills the original array with the values that are now flipped.
for(i=Lo; i<=Hi; i++)
{
A BRACKET I BRACKET = *index;
index++;
}
// This returns the number of swaps done by the function.
return numSwap;
}
We submit this to an online grader which then spits out the result and docks point. The grader has a main function which it uses as a "test harness" where it tests a variety of conditions. I am not able to see the tests that it is going through.
Apparently my program is failing and the online grade is creating a stack dump. I cannot, for the life of me, find a test which causes my program to crash.
The Lo and Hi have to be within bounds of the array. The Lo and Hi have to be a Low Point and a High Point. Nothing I have been able to do in my testing has been able to produce incorrect results.
All my Professor said was that I had boundary issues. It all seems to work fine for me. Can anyone point me in the right direction here - because I can't figure out why such a stupidly simple program is causing me more than 5 minutes of work.
(I suspect it is a problem with my Dynamic Memory Management malloc() function if it is indeed boundary issues)
-Kevin