Thread: Sort an array alphabetically HELP!!

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    8

    Sort an array alphabetically HELP!!

    Hello,
    I am new to C and taking a class. I need some help with an assignment.
    //Build a program that uses a single-dimension array to store 10 names input by a user.
    //After inputting the names, the user should see a menu with two options to sort and print the 10 names in ascending or descending order.

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
        char names[10];
        char temp[10];
        int count,i,j;
        int sort;
        count=1;
        for ((i=0);i<10;i++)
        {
            while (count<11)
            {
                printf("Please enter name %i\n",count);
                gets(names);
                count=count+1;
            }
        }
        printf("Would you like to print in Ascending (press 1) or Descending (press 2) order?\n\n");
        scanf("%d",&sort);
        if (sort==1){
            printf("\n Ascending order:\n ");
            printf("%c\n", names[i+1]);
                for (i=0;i<names-1;i++)
                {
                    for (j=i+1;j<names;j++)
                    {
                        if (strcmp(&names[i],&names[j])>0)
                        {
                            strcpy(&temp,&names[i]);
                            strcpy(&names[i],&names[j]);
                            strcpy(&names[j],&temp);
                        }
                    }
                }
            for (i=0;i<names;i++){
                    printf("\n%c\n",names[i]);
            }
        }
        else if (sort==2){
            printf("\n Descending order:\n ");
            printf("%c\n", names[i-1]);
                for (i=0;i>names ;i--)
                {
                    for (j=i-1;j>names;j--)
                    {
                        if (strcmp(&names[i],&names[j])>0)
                        {
                            strcpy(&temp,&names[i]);
                            strcpy(&names[i],&names[j]);
                            strcpy(&names[j],&temp);
                        }
                    }
                }
            for (j=i-1; i>names; j--) {
                printf("\n%c\n",names[i]);
            }
        }
        return 0;
    }
    I keep

  2. #2
    Registered User
    Join Date
    Apr 2014
    Posts
    8
    I keep getting an error i dont understand.....any advice?

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    How large are the names being input?

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    8
    Alpo, I did not define how large the name is and I would prefer not to.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you don't tell us what error you are getting and you don't tell us how long the names you are entering are, then it's going to be very hard to help you.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    1. In that loop you keep overwriting content of the array.
    Quote Originally Posted by pabeyta View Post
    Code:
    while (count<11)
    {
        printf("Please enter name %i\n",count);
        gets(names);
        count=count+1;
    }
    2. Don't use gets as you can't control the size of the input string, use fgets instead.

    3. Since the task requires the array to be single-dimension you need to create an array of pointers to char (array of strings). A block of dynamically allocated memory should be assigned to each pointer allowing to store a name from input.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by DRK View Post
    3. Since the task requires the array to be single-dimension you need to create an array of pointers to char (array of strings). A block of dynamically allocated memory should be assigned to each pointer allowing to store a name from input.

    Too hard. Allocate an array with one dimension and address it manually.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Hodor View Post
    Too hard. Allocate an array with one dimension and address it manually.
    Given that the OP hasn't demonstrated much understanding of arrays, addressing "manually" may not be any easier.

    Look at line 27 in the original code. "for (i = 0; i < names-1; i++)" has all sorts of problems, given that i is an int, and names is an array of char. <shudder>.

    To the OP: you really need to do back and read your introductory text and lecture notes CAREFULLY, before even trying to write more code. There are just too many problems in your code (attributable to your lack of basic understanding) that any attempt to help you would cause undue frustration - both for you and anyone trying to help. Your code has a "whack the mole" characteristic - every time one problem is fixed, another one will pop up to cause frustration.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Apr 2014
    Posts
    8
    Quote Originally Posted by grumpy View Post
    Given that the OP hasn't demonstrated much understanding of arrays, addressing "manually" may not be any easier.

    Look at line 27 in the original code. "for (i = 0; i < names-1; i++)" has all sorts of problems, given that i is an int, and names is an array of char. <shudder>.

    To the OP: you really need to do back and read your introductory text and lecture notes CAREFULLY, before even trying to write more code. There are just too many problems in your code (attributable to your lack of basic understanding) that any attempt to help you would cause undue frustration - both for you and anyone trying to help. Your code has a "whack the mole" characteristic - every time one problem is fixed, another one will pop up to cause frustration.
    Well "Grumpy," your name is fitting. As I previously stated, I am new to this and I am asking for help. There is no need to be pompous, if you find it too frustrating to help simply do not respond and move on but thank you anyways for your wealth of experience.

    Code:
    int main(){
        char names[10][10],i,j;
        char temp[10];
        int count;
        int sort;
        count=1;
        for ((i=0);i<10;i++)
        {
            while (count<11)
            {
                printf("Please enter name %i\n",count);
                scanf("%s",names);
                count=count+1;
            }
        }
        printf("Would you like to print in Ascending (press 1) or Descending (press 2) order?\n\n");
        scanf("%d",&sort);
        if (sort==1){
            printf("\n Ascending order:\n ");
            printf("%s\n",names);
        }
                else if (sort==2){
            printf("\n Descending order:\n ");
            printf("%s\n",names);
                }
                return 0;
    }
    Since I was having issues with the int and char change, I have since removed most of the code. So... as I said I need help getting the array to sort alphabetically, in both a ascending and descending order with the user ability to chose which is displayed. instead of using "if' and "else if" should I be using switch would this change/ help anything. Direction would be appreciated.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by pabeyta View Post
    Well "Grumpy," your name is fitting. As I previously stated, I am new to this and I am asking for help. There is no need to be pompous, if you find it too frustrating to help simply do not respond and move on but thank you anyways for your wealth of experience.
    My response was simple: you need to read up on the basics before trying again.

    Sometimes the best advice to give a beginner who has skipped over the basics is to go back and learn things a beginner absolutely needs to learn, because they can't be skipped over. That is the situation here.

    Quote Originally Posted by pabeyta View Post
    Since I was having issues with the int and char change, I have since removed most of the code. So... as I said I need help getting the array to sort alphabetically, in both a ascending and descending order with the user ability to chose which is displayed. instead of using "if' and "else if" should I be using switch would this change/ help anything. Direction would be appreciated.
    Again, you're jumping the gun. Yes, your objective is to sort the array alphabetically. However, if you can't properly create and populate an array, you will never be able to sort one. Those are the fundamentals you have skipped right over. You're not seeing the wood for the trees.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Apr 2014
    Posts
    8
    You are correct if I do not get my ducks in a row, it is harder to count them. I think I was able to create the code after I did more research on arrays. Thank you for the push to do so.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sort A String Alphabetically.
    By wantsree in forum C Programming
    Replies: 2
    Last Post: 02-05-2011, 02:14 AM
  2. how to sort strings alphabetically?
    By jaguar7 in forum C Programming
    Replies: 7
    Last Post: 10-02-2008, 01:32 PM
  3. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  4. How do I heap sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2007, 01:00 AM
  5. Replies: 2
    Last Post: 05-06-2003, 08:34 AM