Pro Vayne Pro Vayne - 1 year ago 68
C Question

Always says segmentation error,dont know where should i fix

This code is used to create array and sort it, my homeworks request is to calculate the time it runs. But seems this code doesn't work. When I type

$./sorting select v 20000
the shell says segmentation error, I dont know where I should fix.

#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>

int* insertionSort(int * arr,int len){
for (int i=1; i<len; i++){

int x=arr[i];
int j=i;

while (j>0 && x<arr[j-1]){
arr[j]=arr[j-1];
j--;
}

arr[j]=x;
}

return arr;
}

int* selectionSort(int* arr,int len){
int pos=0;

for (int i=0; i<len;i++){
int minpos=arr[i];

for (int j=i; j<len; j++){
if (minpos>arr[j]){
minpos=arr[j];
pos=j;
}

}

int x=arr[i];
arr[i]=minpos;
arr[pos]=x;
}

return arr;
}

int* create_array(char category, int n){
int *arr=malloc(n*sizeof(int));

if (category=='s'){
for (int i=0;i<n;i++){
arr[i]=i;
}
}
else if (category=='v'){
for (int i=n-1;i>=0;i--){
arr[i]=i;
}
}
else if (category=='r'){
for (int i=0;i<n;i++){
arr[i]=i;
}
for(int i = 0; i < n; i++){
int j = rand() % n;
int t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
else {return NULL;}

return arr;
}

void main(char sort_algo,char category,int array_size){

int *arr=create_array(category,array_size);

if (&sort_algo == "select"){
selectionSort(arr,array_size);
}
else{
insertionSort(arr,array_size);
}
}

Answer Source

You should modify you main function, prototype must be as below if you want to take command line arguments:

int main ( int argc, char *argv[] )

You must also consider WeatherVane's and Bence Kaulics's suggestions and read the stuff suggested for better understanding of concepts.

As a side note i have modified your code ( to bare minimum ) and it will run now. There are lots of improvement which can be done. Keep Learning !

#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>

int* insertionSort(int * arr,int len){
    int i;
    for (i=1; i<len; i++){

        int x=arr[i];
        int j=i;

        while (j>0 && x<arr[j-1]){
            arr[j]=arr[j-1];
            j--;
        }

        arr[j]=x;
    }

    return arr;
}

int*  selectionSort(int* arr,int len){
    int pos=0;
    int i;
    for (i=0; i<len;i++){
        int minpos=arr[i];
        int j;
        for (j=i; j<len; j++){
            if (minpos>arr[j]){
                minpos=arr[j];
                pos=j;
            }

        }

        int x=arr[i];
        arr[i]=minpos;
        arr[pos]=x;
    }

    return arr;
}

int* create_array(char category, int n){
    int *arr=malloc(n*sizeof(int));

    if (category=='s'){
        int i;
        for (i=0;i<n;i++){
             arr[i]=i;
        }
    }
    else if (category=='v'){
        int i;
        for (i=n-1;i>=0;i--){
            arr[i]=i;
            //printf("%d\n",i);
        }
    }
    else if (category=='r'){
        int i;
        for (i=0;i<n;i++){
            arr[i]=i;
        }
        for(i = 0; i < n; i++){
             int j = rand() % n;
             int t = arr[j];
             arr[j] = arr[i];
             arr[i] = t;
        }
    }
    else {return NULL;}

    return arr;
}
//main with command line arguments
int main( int argc, char *argv[] )  {
//    argv[0]  argv[1]  argc[2]   argv[3]
//    sorting select      v        20000
    int num;
    sscanf (argv[3],"%d",&num);
    int *arr=create_array(argv[2][0],num);

    if(strcmp(argv[1],"select")==0){
        selectionSort(arr,num);
    }
    else{
        insertionSort(arr,num);
    }
}