#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    String bubble sorting


    This a modification of a simple string sorting.what I want to do is not use dynamic array. I defined a 2d array string with a width [10]. I am storing the character values as you can see below in the first loop. when the array is full, I want an exact max width to define another array string[no. of terms][max width known] & strcpy from prev array with width [10](10 is abstract no. I tuk) to new strings. And then bubble sort it.



    However this following code is giving problem. Can somebody please point out what is going wrong?

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <string.h>
    
    /* Fighting with algos to compensate the memory tradeoff with processing.*/
    
    void main()
    
    {
    
    int i=NULL;
    
    int lendest=0;
    
    int maxno=NULL;
    
    char character=NULL;
    
    printf(“Enter how many entries?:\t”);
    
    scanf(“%d”,&maxno);
    
    printf(“\n”);
    
    char arr[maxno][10];
    
    printf(“Enter the entry names one by one, press tab & reenter \n”);
    
    i=0;
    
    int sum=0;
    
    char chararr[10];
    
    while(i<maxno)
    
    {
    
    int j=0;
    
    printf(“OK TILL HERE.SIZEOF CHARARR:/t %d & j is %d \n”,sizeof(chararr),j);
    
    while(character!=’\t’)
    
    {
    
    character=getch();
    
    chararr[j++]=character;
    
    printf(“OK TILL HERE.j is %d. entered letter is:%c \n”,j,character);
    
    }
    
    character=NULL;
    
    chararr[j-1]=’\0′;
    
    printf(“OK TILL HERE. \n”);
    
    strcpy(arr[i],chararr);
    
    printf(“PRINTING STRING ARRAY %s \n”,arr[i]);
    
    lendest=strlen(arr[i]);
    
    if(sum<lendest)
    
    sum=lendest;
    
    i++;
    
    printf(“OK TILL HERE.i is %d LOOPING BACK\n”,i);
    
    printf(“\n”);
    
    }
    
    /* //arr[i]=’\0′;*/
    
    printf(“Exited loop\n”);
    
    char string[maxno][sum];
    
    printf(“sizeof char string is %d \n”,sizeof(string));
    
    i=0;
    
    while(i<maxno)
    
    {
    
    strcpy(string[i],arr[i]);
    
    printf(“Copied string & arr values are: %s & %s. i is %d\n”,string[i],arr[i],i);
    
    i++;
    
    }
    
    printf(“Exited loop\n”);
    
    i=1;
    
    while(i<maxno)
    
    {
    
    char dummy=NULL;
    
    int j=0;
    
    while(j<maxno-i)
    
    {
    
    if(strcmp(string[j],string[j+1]))
    
    {
    
    printf(“string values are: %s & %s. i & j are %d & %d\n”,string[j],string[j+1],i,j);
    
    strcpy(dummy,string[j]);
    
    strcpy(string[j],string[j+1]);
    
    strcpy(string[j+1],dummy);
    
    }
    
    j++;
    
    printf(“string values are: %s & %s. i & j are %d & %d\n”,string[j],string[j+1],i,j);
    
    }
    
    i++;
    
    printf(“string values are: %s & %s. i & j are %d & %d\n”,string[j],string[j+1],i,j);
    
    }
    
    i=0;
    
    while(i<maxno)
    
    {
    
    printf(“%s”,string[i]);
    
    i++;
    
    }
    
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    It's a hundred lines long, and devoid of indentation

    So I'm afraid the post will largely go unread.

    Comments on this post

    • dwise1_aol agrees : You should have a look at her other one, with about a dozen blank lines between each line of code. Definitely earned the title of "Worse Code Listing Ever!"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    I'm not even going to begin to try to clean up your unreadable listing, though at least it's almost not as bad as your other one.

    Code:
    char dummy=NULL;
    
    int j=0;
    
    while(j<maxno-i)
    
    {
    
    if(strcmp(string[j],string[j+1]))
    
    {
    
    printf(“string values are: %s & %s. i & j are %d & %d\n”,string[j],string[j+1],i,j);
    
    strcpy(dummy,string[j]);
    
    strcpy(string[j],string[j+1]);
    dummy is a single character and you "initialize" it by assigning NULL to it. NULL is a pointer to char, not a char. Then you try to use dummy as a char pointer in a call to strcpy.

    What did the warnings tell you about that? What did I just tell you about what happens when you ignore warnings?

    Compile it again with warnings turned on and up! Then read the warnings and use them to clean up your code.

    PS
    You also neglected to tell us anything about the symptoms of the problem, attempting to lure us into wasting our time in stupid guessing games.

    You need to help us so that we can help you. Preventing us from being able to help you only makes it that much harder for you to get the help you want.

    And never ignore warnings! They are much more important than error messages.
    Last edited by dwise1_aol; January 31st, 2013 at 02:56 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    @dwise1_aol Thanks for pointing out the mistake in my 1st thread. I'll take the NULL point in my mind. Btw, try to reply nicely kid.

    Originally Posted by dwise1_aol
    I'm not even going to begin to try to clean up your unreadable listing, though at least it's almost not as bad as your other one.

    Code:
    char dummy=NULL;
    
    int j=0;
    
    while(j<maxno-i)
    
    {
    
    if(strcmp(string[j],string[j+1]))
    
    {
    
    printf(“string values are: %s & %s. i & j are %d & %d\n”,string[j],string[j+1],i,j);
    
    strcpy(dummy,string[j]);
    
    strcpy(string[j],string[j+1]);
    dummy is a single character and you "initialize" it by assigning NULL to it. NULL is a pointer to char, not a char. Then you try to use dummy as a char pointer in a call to strcpy.

    What did the warnings tell you about that? What did I just tell you about what happens when you ignore warnings?

    Compile it again with warnings turned on and up! Then read the warnings and use them to clean up your code.

    PS
    You also neglected to tell us anything about the symptoms of the problem, attempting to lure us into wasting our time in stupid guessing games.

    You need to help us so that we can help you. Preventing us from being able to help you only makes it that much harder for you to get the help you want.

    And never ignore warnings! They are much more important than error messages.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Originally Posted by lovecodecakes
    @dwise1_aol Thanks for pointing out the mistake in my 1st thread. I'll take the NULL point in my mind. Btw, try to reply nicely kid.
    I do. Please don't throw crap at us.

    As salem told you, your unindented code is unreadable. Since most of us work for a living, we have very little time to spend here helping others, so we expect to be given code that we can read. If you give us unreadable code (AKA "crap"), then most of us won't bother to try; we can't afford to waste the time. And if you give us code that you make even more unreadable as in your other thread, then that's even worse.

    You need to help us to help you. Simple fact of life.

    But indenting your code is not mainly for our benefit, but primarily for your own benefit. Above all, you need to be able to read and understand your own code, not what you think you might have written, but rather what you have actually written. A great many mistakes become apparent immediately if in properly indented code, but remain hidden in unindented code or in improperly indented code.

    Indent your code properly. It's for your own good.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    to lovecodecakes,

    While cleaning your code I discovered 4 different types of quote marks to denote strings and characters. None of these are valid in the standard compilers I use. Use a code editor to edit code, not a word processor. I recommend emacs. You can install xemacs onto windows systems fairly cheaply. (It's free, the source is free. By "cheaply" I mean you can get this good software without a huge distribution.) Makes me wonder if you use a compiler.

    void main() is wrong.
    int main() is right.
    return 0; is good.

    Please partition your program with functions. It would be much easier, even possible to read if the main function were
    Code:
    int main() {
      gather_input();
      never_bubble_sort();
      show_ouput();
      return 0;
    }
    with perhaps a few variables and arguments passed around.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    @dwise1_aol It looks like a mess. That "indentation" is what I am sorry for in a hurry I pasted the whole thing in the thread. I think we all work here. Thanks again.

    Originally Posted by dwise1_aol
    I do. Please don't throw crap at us.

    As salem told you, your unindented code is unreadable. Since most of us work for a living, we have very little time to spend here helping others, so we expect to be given code that we can read. If you give us unreadable code (AKA "crap"), then most of us won't bother to try; we can't afford to waste the time. And if you give us code that you make even more unreadable as in your other thread, then that's even worse.

    You need to help us to help you. Simple fact of life.

    But indenting your code is not mainly for our benefit, but primarily for your own benefit. Above all, you need to be able to read and understand your own code, not what you think you might have written, but rather what you have actually written. A great many mistakes become apparent immediately if in properly indented code, but remain hidden in unindented code or in improperly indented code.

    Indent your code properly. It's for your own good.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    @b49. Thanks. Don't make an issue out of indentation. Ive already mentioned a while back the indentation mess I made while copying from C::B to here.
    Yes implementing functions is neat. this was a short program I was trying to play around while brushing with C.
    If I may ask, we don't use any return type with a void & so why is void main not recommended?

    Originally Posted by b49P23TIvg
    to lovecodecakes,

    While cleaning your code I discovered 4 different types of quote marks to denote strings and characters. None of these are valid in the standard compilers I use. Use a code editor to edit code, not a word processor. I recommend emacs. You can install xemacs onto windows systems fairly cheaply. (It's free, the source is free. By "cheaply" I mean you can get this good software without a huge distribution.) Makes me wonder if you use a compiler.

    void main() is wrong.
    int main() is right.
    return 0; is good.

    Please partition your program with functions. It would be much easier, even possible to read if the main function were
    Code:
    int main() {
      gather_input();
      never_bubble_sort();
      show_ouput();
      return 0;
    }
    with perhaps a few variables and arguments passed around.
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    The operating system uses the return value from the program. Shell scripts use the value. Your program will return a value whether you declare main as void or int. Best that you declare as int and choose the value the operating system obtains.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo