Need help in C

Discussion in 'Programming' started by demonator09, Mar 1, 2009.

  1. #1
    I have got an assignment to get input of five names and then sort them alphabetically and print them. Seems too simple?? Even I thought at first. But I have got limits, I cant use multidimensional arrays or any functions in the program. But I can use string functions like strcmp, strcpy, etc. I tried it myself but could not do it.

    Can anyone please help me??
     
    demonator09, Mar 1, 2009 IP
  2. fulllydisturbed

    fulllydisturbed Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hai.. i can help you...i have done the code.. its easy..... do you want
     
    fulllydisturbed, Mar 1, 2009 IP
  3. fulllydisturbed

    fulllydisturbed Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The code is :

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

    void main()
    {
    int i,j;
    char names[5][10],swap[10];

    for(i=0;i<5;i++)
    {
    printf("\n Enter the name %d (less than 10 chara): ",i+1);
    gets(names);
    }

    printf("\n");

    /*for(i=0;i<5;i++)
    {
    printf("\n name %d : ",i+1);
    puts(names);
    } */

    for(i=0;i<4;i++)
    {
    for(j=i+1;j<5;j++)
    {
    if(strcmp(names,names[j])>0)
    {
    strcpy(swap,names);
    strcpy(names,names[j]);
    strcpy(names[j],swap);
    }
    }
    }

    printf(" Sorted Array \n");
    for(i=0;i<5;i++)
    {
    printf("\n name %d : ",i+1);
    puts(names);
    }

    }

    just paste it in a text editor and name it as .c file.
     
    fulllydisturbed, Mar 1, 2009 IP
    demonator09 likes this.
  4. fulllydisturbed

    fulllydisturbed Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i wont help u how it works... find out yourself and tell your teacher... you should learn something by yourself
     
    fulllydisturbed, Mar 1, 2009 IP
  5. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #5
    You are supposed do your own homework.
     
    it career, Mar 1, 2009 IP
  6. gramyla

    gramyla Peon

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    fulllydistributed gives a perfect code,it will work and simple. Did you tried it or not.
     
    gramyla, Mar 2, 2009 IP
  7. fulllydisturbed

    fulllydisturbed Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you Gramyla....
     
    fulllydisturbed, Mar 2, 2009 IP
  8. demonator09

    demonator09 Peon

    Messages:
    235
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    @it career's: I didn't ask it without trying it. I could not do it so I just asked for help.

    @fullydistributed thanks a lot for the code, it worked!!

    @gramyla: Yes the code worked!!
     
    demonator09, Mar 2, 2009 IP