Thread: Help! - FILE - sort alphabetically

  1. #31
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read it again: For the love of all that is good, remove ALL the [20] things EXCEPT the one (one!) in the declaration of the array.

    Notice that you removed 20, which is not the same as [20].

  2. #32
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Yessssssssssss!!! It works for 3 names!!!!!!! I'm so happy!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        int N;
        printf("\nNumber of names: ");
        scanf("%d", &N);
    
        char name[N][20];
        char aux[N][20];
        int i, ret0=0, ret1=0, ret2=0, ret3=0;
    
        for(i=0; i<N; i++)
        {
            printf("\nName: ");
            scanf("%s", &name[i]);
        }
    
        // 3 lower names
        printf("\nFIRST 3 NAMES THE USER WROTE\n");
        for (i=0; i<3; i++)
        {
            ret0 = strcmp(name[i], name[i+1]);
            if (ret0 > 0)
            {
                strcpy(aux[i], name[i]);
                strcpy(name[i], name[i+1]);
                strcpy(name[i+1], aux[i]);
            }
    
        }
        printf("\n%s ", &name[0]);
        printf("\n%s ", &name[1]);
        printf("\n%s ", &name[2]);
    
    
        printf("\n3 LOWER NAMES (ALPHABETICALLY)\n");
        for(i=3; i<N; i++)
        {
            if ((ret1 = strcmp(name[0], name[i])) > 0 && (ret2 = strcmp(name[1], name[i])) > 0 && (ret3 = strcmp(name[2], name[i])) > 0)
            {
                strcpy(aux[0], name[0]);
                strcpy(name[0], name[i]);
                strcpy(aux[1], name[1]);
                strcpy(name[1], aux[0]);
                strcpy(name[2], aux[1]);
            }
            if ((ret1 = strcmp(name[0], name[i])) < 0 && (ret2 = strcmp(name[1], name[i])) > 0 && (ret3 = strcmp(name[2], name[i])) > 0)
            {
                strcpy(name[2], name[i]);
            }
            if ((ret1 = strcmp(name[0], name[i])) > 0 && (ret2 = strcmp(name[1], name[i])) < 0 && (ret3 = strcmp(name[2], name[i])) > 0)
            {
                strcpy(aux[0], name[1]);
                strcpy(name[1], name[i]);
                strcpy(aux[1], name[2]);
                strcpy(name[2], aux[0]);
            }
        }
        printf("\n%s \n%s \n%s", name[0], name[1], name[2]);
    
        return 0;
    }
    (it has 4 errors that says something about pointers)

    Now, I have to improve it to read the names from a .txt and to print the first 100 names (for 3 names, I used a for and some if, but for 100 it's going to be impossible)

    Can you continue helping me, please?
    Last edited by juanjuanjuan; 02-16-2014 at 03:01 PM.

  3. #33
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Hint: Keep the code mostly like it is till you write and test a swap function.

    After, getting the swap to work; write the sort function.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by juanjuanjuan View Post
    (it has 4 errors that says something about pointers)
    You need to fix these, whatever they are. And "fixing" them doesn't mean "typing random things until the error goes away", which is how you appear to have "fixed" all the other problems. Read what it says, look at what it's telling you about what your code is, and more importantly what your code should be, and change your code to match.

  5. #35
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    in the warnings, it says that "%s" expects argument of type 'char *', but argument has type 'char (*)[20]'

    I understand the program because I wrote it, but I don't know how to do what I said: read the names from a .txt and print the first 100 names (for 3 names, I used a for and some if, but for 100 it's going to be impossible)

  6. #36
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Yessssss! I did it!

    It works perfectly, but the user has to write the names... can anyone help me so the input names come from a .txt?

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  2. Arranging a file into alphabetical order.
    By Malachi in forum C Programming
    Replies: 18
    Last Post: 02-10-2009, 11:07 PM
  3. Replies: 4
    Last Post: 03-06-2008, 03:38 PM
  4. Binary File - Byte order / endian
    By chico1st in forum C Programming
    Replies: 27
    Last Post: 08-22-2007, 07:13 PM
  5. Header file include order
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2006, 03:22 PM