Right, so I've got a project where I've got to take several data files, sort them using sorting algorithm of your choice, time how long it takes, etc etc. Simple stuff.
The problem is that we've got to make the datafiles ourselves. Since I'm using qsort in C, this means arrays. Got that code working, but when I try to pass the random array to the main function, gcc doesn't like something.
Snippets:
My random function
int* randomArray(int scale)
{
int length = INT_MAX / scale;
int arr[length];
int index = 0;
srand(clock());
for(index = 0; index < length; index++)
{
arr[index] = rand();
}
return arr;
}
int main()
{
time_t start, end;
double sort1Time, sort2Time, sort3Time;
int sort1[INT_MAX / 4], sort2[INT_MAX / 16], sort3[INT_MAX / 256];
sort1 = randomArray(4);
sort2 = randomArray(16);
sort3 = randomArray(256);
.
.
.(operations on the arrays)
The problem occurs when I try to assign sort1, sort2, and sort3. It says the types do not match.
Posts
Second, function randomArray() returns a pointer to an int, but you are assigning that value to an int. You need to tell the computer "the result of randomArray() is a memory address. Dereference that address and you'll find a value. Put that value in sort1."
Finally, speaking as a Computer Science grad student taking a type systems class: yes pointers are a spawn of Satan -- but get used to them because they aren't going away.
Edit: oops, ah I see, the result is a buffer full of ints. Consider either using memcpy, or redesigning your program so each buffer is created in main(), a pointer to that buffer is passed to your function, and the function returns void but changes that buffer.
Edit2: I just tried it. The compiler agrees with me about that first thing. It says "warning: function returns address of local variable."
I just rewrote your code my way, and I'm having moral issues with just posting it. When is your program due? I'd like to see you work things out on your own (with a few hints here and there) instead of just giving you the answer.
Edit3: also, does the assignment say you MUST run each procedure and compare wall-clock time before and after? If you're running this code on a unix machine, the 'time' shell command will print the amount of user and system time consumed by any program. If you can write nine separate programs and run them from the command line with 'time' I think that'd give you more accurate runtime information.
XBL Michael Spencer || Wii 6007 6812 1605 7315 || PSN MichaelSpencerJr || Steam Michael_Spencer || Ham NOØK
QRZ || My last known GPS coordinates: FindU or APRS.fi (Car antenna feed line busted -- no ham radio for me X__X )
XBL Michael Spencer || Wii 6007 6812 1605 7315 || PSN MichaelSpencerJr || Steam Michael_Spencer || Ham NOØK
QRZ || My last known GPS coordinates: FindU or APRS.fi (Car antenna feed line busted -- no ham radio for me X__X )
Am I on the right track, or just wanking?
BTW, the assignment's due 2PM tomorrow. It said that I had to time the sorting of different sizes of data files (it's an algorithms class, so we're showing running time and all that).
In randomArray() make the definition need int* arr instead of int arr[]. Otherwise you should be good to go.
XBL Michael Spencer || Wii 6007 6812 1605 7315 || PSN MichaelSpencerJr || Steam Michael_Spencer || Ham NOØK
QRZ || My last known GPS coordinates: FindU or APRS.fi (Car antenna feed line busted -- no ham radio for me X__X )
Semantically, (sort1+1) is equivalent to &sort1[1]. And (sort1+2) is equivalent to &sort1[2]. It looks like you're messing with fire, adding 1 or 2 bytes to the pointer address, but addition has a different meaning when one operand is a pointer to a datatype. When you use addition where one operand is a pointer and the other operand is an integer, the resulting address is offset by the integer multiplied by sizeof(DATATYPE).
XBL Michael Spencer || Wii 6007 6812 1605 7315 || PSN MichaelSpencerJr || Steam Michael_Spencer || Ham NOØK
QRZ || My last known GPS coordinates: FindU or APRS.fi (Car antenna feed line busted -- no ham radio for me X__X )