sort program with pointers

Problem with the following program - does not sort from the 2nd function - am I pointing to the wrong pointer - have tried many things -any one have any ideas. thanks.

#include
#include
#define SIZE 100
#define LINE 10
void sortstr(char*str, int n);


int main(void)
{

char *strings[SIZE] = {"finance", "accounting",
"marketing", "programming",
"java", "fortran", "cobal",
"java script", "C", "C++"};

char *ptstr[SIZE];
int i;
int ct = 0;

while(ct < SIZE && strings[ct] != NULL && strings[ct][0] !='')
{
ptstr[ct] = strings[ct];
ct++;
}

printf("The unsorted array is:
");
for (i = 0; i < ct; i++)
puts(strings[i]);

sortstr(*ptstr, ct);

printf("The sorted array is:
");
for (i = 0; i < ct; i++)
puts(strings[i]);

return;
}


void sortstr(char*str, int n)
{
int i = 0;
int j = 0;
int flag;
int swap;

for (i = 0; i < n-1; i++)
{
flag = 0;
for (j = 0; j < n-1; j++)
{
if (str[j] > str[j+1])
{
swap = (str[j], str[j+1]);
flag=1;
}
}
if (flag==0)
break;
n--;
}
return;
}

Comments

  • sir,
    For string swap try this but I am not sure for the give situation.
    void swap(char *s1,char *s2)
    {
    char *temp;
    temp=malloc(sizeof(char));
    strcpy(temp,s1);
    strcpy(s1,s2);
    strcpy(s2,temp);
    }
    void bub(char *s1,int n)
    {

    int i,j;
    for(i=0;i s1[j])
    swap(s1-j,s1);
    }
    }
    }
    main()
    {
    char *i="rawa";
    //int j=strlen(i);
    //clrscr();
    bub(i,2);
    printf("%s
    ",i);
    }

    The above bub() function works with int with modifications with parameter passing at the time of function call but I am not sure with strings. I am working on it.

    note: systax errors in array declearation.Correct it.


    http://www.programmersheaven.com/groups/freedos

    NOTHING IS STABLE, EVEN THE UNIVERSE
    by
    HINDU GOD



  • Hi Rwia!

    There are much thing to change in your code:

    First: you must correct the syntax error in your array declaration mentioned by Sathyashrayan:
    [code]
    char *strings[red][][/red] = {
    "finance", "accounting",
    "marketing", "programming",
    "java", "fortran", "cobal",
    "java script", "C", "C++"};
    [/code]
    Second: In the first while loop you use [italic]ptstr[ct][/italic] but you DO NOT allocate space for your new vector! You define [italic]ptstr[/italic] but you do not assign a value to it before you start using it.
    The code that solves this task should look like that:
    [code]
    int main()
    {
    char *strings[]= {...}; // I do not write it down again...

    char *ptstr[10]; // Assuming you have 10 strings!!!
    int i;

    for (i=0; i<10; i++)
    ptstr[i]=strdup(strings[i]);
    }
    [/code]
    This is necessary because you have initialized the strings array directly. So you cannot modify the strings vector because it is [b]constant[/b]. Thatsway you have to make a copy of it first. This will be [italic]ptstr[/italic].

    Third: you have to sort an array of strings. An array of strings is an array containing [b]pointers[/b] to strings. A string is practically a vector of charcters terminated with an ASCII 0 character. It follows that you [b]cannot[/b] use the [italic]<(less than)[/italic] or [italic]>(greater than)[/italic] operators on pointers. You have to use the [italic]strcmp[/italic] function. So your sorting function should look like that:

    [code]
    void sortstr(char *str[],int n)
    {
    int i,j,min,res;

    for (i=0; i<n-1; i++)
    {
    min=i;
    for (j=i+1; j<n; j++)
    if (strcmp(str[min],str[j]==1)
    min=j;

    if (min!=i)
    swapstr(&str[i],&str[min]);
    }
    }

    void swapstr(char **s1, char **s2)
    {
    char *temp;

    temp=*s1;
    *s1=*s2;
    *s2=temp;
    }
    [/code]
    So you call sortstr: [b]sortstr(ptstr,ct)[/b]

    After you finished using [italic]ptstr[/italic] you should free the allocated space:

    [code]
    for (i=0; i<10; i++)
    free(ptstr[i]);
    [/code]

    Hope it will help...

    Gabe
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories