-1

I'm trying to write a code that sorts an array of strings in alphabetical or reverse alphabetical order, depending on the command line argument given.

"-o a" for alphabetic.

"-o r" for reverse.

This is my code so far.

#include<stdio.h>
#include<string.h>

int string_compare(char *str1,char *str2){
    int ret;

    ret=strcmp(str1,str2);

    if(ret < 0) {
      return 0;
    } 
    else if(ret > 0) {
      return 1;
    } 
    else {
      return -1;
    }
}

void swap(char *str1, char *str2)
{
  char *temp = str1;
  str1 = str2;
  str2 = temp;
}  

int main(int argc,char *argv[]){
    char *planets[9]={"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
    int i,j;
    int a_ret=strcmp(argv[2],"a");
    int r_ret=strcmp(argv[2],"r");
    int cmp;

    for(i=0;i=8;i++){
        for(j=8;j=(i+1);j--){
            cmp=string_compare(planets[j],planets[j-1]);
            if(a_ret==0){
                if(cmp==0){
                    swap(planets[j],planets[j-1]);
                }
            }
            else if(r_ret==0){
                if(cmp==1){
                    swap(planets[j],planets[j-1]);
                }
            }
        }
    }
    printf("%s",planets[0]);
   return 0;
}

The program is supposed to work something like this:

./planets –o a
The planets in alphabetical order are: Earth, Jupiter, Mars, Mercury, Neptune, Pluto, Saturn, Uranus, Venus

or this:

./planets –o r
The planets in reverse alphabetical order are: Venus, Uranus, Saturn, Pluto, Neptune, Mercury, Mars, Jupiter, Earth

The program compiles without error, but when I run it I get

Segmentation fault(core dumped)

I'm new to C, and I don't quite fully understand how to manipulate the memory allocation. Any help or advice is much appreciated.

Wilson
  • 1
  • 1
  • 2
    Your `swap` function doesn't work the way you think it works. Do some research about *emulating pass by reference in C*. – Some programmer dude Dec 05 '17 at 07:06
  • 1
    Also, you should learn the difference between assignment using `=` and comparison for equality with `==`. Perhaps you should [get a good beginners book or two](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to read? – Some programmer dude Dec 05 '17 at 07:07
  • 1
    Furthermore, after an initialization like `i = 0` then `i == 8` will *never* be true, so your outer loop will never execute anyway. Perhaps you should use `i < 9` (or `i <= 8`) as condition instead? – Some programmer dude Dec 05 '17 at 07:09
  • 1
    Lastly, with crashes like this you should [learn how to *debug* your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Preferably using a *debugger*. – Some programmer dude Dec 05 '17 at 07:09
  • You are essentially asking why `int x=1; func(x); ... void func (int a) { a=2; }` does not change the value of x. – Lundin Dec 05 '17 at 07:55

2 Answers2

0

In for loop you have used = operator instead of <= or >=.
NOTE: There are other logical errors also.

UPDATED:
You can use following code:

#include<stdio.h>
#include<string.h>
#define MAX_SIZE 9

void swap(char **str1, char **str2)
{
  char *temp = *str1;
  *str1 = *str2;
  *str2 = temp;
}  

int main(int argc,char *argv[]){
    char *planets[MAX_SIZE]=    {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
    int i, j, cmp, minMax;

    //If we don't need an array sorted in accending order then it is obvious that
    //we need it to be sorted in decending order
    int a_ret= (strcmp(argv[2],"a") == 0);

    for(i=0;i<MAX_SIZE;i++){
        minMax = i;
        for(j=i+1;j<MAX_SIZE;j++){
            cmp=strcmp(planets[j],planets[minMax]);
            if(((a_ret==0) && (cmp>0)) || ((a_ret==1) && (cmp<0))){
                    minMax =j;
            }
        }

        //Swap only if required
        if(i != minMax)
           swap(&planets[i], &planets[minMax]);
    }

    //Print the sorted array
    for(i=0;i<MAX_SIZE;i++)
        printf("%s | ",planets[i]);
   return 0;
}

You can see working code here(Command line argument are nor provided due to online IDE restriction).

cse
  • 4,066
  • 2
  • 20
  • 37
-1

I fixed the logic errors that were pointed out and the swap function, and the program runs without crashing. The results are mostly right except for "Mercury".

Current program:

#include<stdio.h>
#include<string.h>

int string_compare(char *str1,char *str2){
    int ret;

    ret=strcmp(str1,str2);

    if(ret < 0) {
      return 0;
    } 
    else if(ret > 0) {
      return 1;
    } 
    else {
      return -1;
    }
}

void swap(char** s1, char** s2)
{
    char* temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}

int main(int argc,char *argv[]){
    char *planets[9]={"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
    int i,j;
    int a_ret=strcmp(argv[2],"a");
    int r_ret=strcmp(argv[2],"r");
    int cmp;

    for(i=0;i<8;i++){
        for(j=i+1;j<=8;j++){
            cmp=string_compare(planets[j-1],planets[j]);
            if(a_ret==0){
                if(cmp==1){
                    swap(&planets[j-1],&planets[j]);
                }
            }
            else if(r_ret==0){
                if(cmp==0){
                    swap(&planets[j-1],&planets[j]);
                }
            }
        }
    }
    for(int k=0;k<=8;k++){
        printf("%s,",planets[k]);

    }
    printf("\n");
return 0;
}

Result with command line argument -o a:

Mercury,Earth,Jupiter,Mars,Neptune,Pluto,Saturn,Uranus,Venus,   

It seems the program skips the first string in the array.

Wilson
  • 1
  • 1
  • This is because you are not doing sorting correctly. I have updated my answer. It is working fine. You can see it working [here](https://ideone.com/tDut8F). – cse Dec 05 '17 at 08:47