Question

this is a school assignment.

The essential problem is to take a large array of numbers, implement multiple different kinds of shell sorts on it, analyze the complexity, and so on.

I have a shell sort written using selection sort (I know it's inefficient, that's the point) but I am having trouble testing it.

My main file is called sorting_main.c and the function defs are in sorting.c.

I compile and then run it using these commands.

gcc -Werror -Wall -Wshadow -O3 sorting.c sorting_main.c -o proj1

When the project is finished, I will be able to use this command to run it...

./proj1 s input.txt seq.txt output.txt

I can run it now though, with just ./proj1 in the terminal if I manually define testing parameters in my main. This is where I am stuck, I don't know if I am doing something wrong in making stuff up to test or if my implementations are fatally flawed. And my goal is to test my selection sort... Here is my main:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

//Function Declarations////
long *Load_File(char *Filename, int *Size);
int Save_File(char *Filename, long *Array, int Size);
void Shell_Insertion_Sort(long *Array, int Size, double *N_Comp, double *N_Move);
void Shell_Selection_Sort(long *Array, int Size, double *N_Comp, double *N_Move);
int Print_Seq(char *Filename, int Size);


int main()
{



long int  *Array[11];
 *Array[0]=9;
 *Array[1]=80;
 *Array[2]=4;
 *Array[3]=1;
 *Array[4]=5;
 *Array[5]= 3;
 *Array[6]= 7;
 *Array[7]=15;
 *Array[8]= 9;
 *Array[9]= 5;
 Shell_Selection_Sort(Array[10],11,0,0);
 /*
 int i = 0;
 for(i=0;i<=10;i++)
   {
     printf("%li\n",*Array[i]);
   }
 */
  return 0;
}

Again, I compile using this command in the terminal:

gcc -Werror -Wall -Wshadow -O3 sorting.c sorting main.c -o proj1

Where I receive only 2 warnings saying that I have not used a calculated value yet (which is expected)

And then I run using this:

./proj1

Does anyone see any reasons that I would receive a segfault in

#0  0x000000000040096b in Shell_Selection_Sort ()
#1  0x00000000004005d7 in main ()

I feel like this should be a simple mistake...

Was it helpful?

Solution

You're declaring an array of pointers.

Either declare an array or a pointer (if you declare a pointer, you need to malloc some memory).

In your case you should declare an array.

long Array[11];

Store elements like this

Array[2]=4;
Array[3]=1;

long Array[11] has the signature of long *Array, so it will compile with the function you're calling.

Now as for those double * arguments, you're going to have to write some comparison functions...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top