hello
i was trying to do one of my assignments but i got stuck at sorting my strut arrays elements by alphabetical order. here is the code that i have dose so far but after this even reading as much as i can, i can not get to go any where. hope can get some help here.

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


void record();
void display();
void sort();


struct std
{
    int stdid;
    char name[20];
    int mark;
    char status[10];
}s1[5];

void main()
{
    record();
    sort();
    display();
}

void record()
{
    int i;

    for(i=0;i < 5;i++)
    {
        printf("Enter the Student Id: ");
        scanf("%d",&s1[i].stdid);
        printf("Enter the name: ");
        flushall();
        gets(s1[i].name);
        printf("Enter the student marks: ");
        scanf("%d",&s1[i].mark);
        printf("Enter the status: ");
        flushall();
        gets(s1[i].status);
    }

}

void display()
{
    int i;
    printf("The record is\n");
    for(i=0;i < 5;i++)
    {
        printf("\nStudent Name : %s",s1[i].name);
        printf("\nStudent ID: %4d",s1[i].stdid);
        printf("\nStudent Marks: %d",s1[i].mark);
        printf("\nStudent Status: %s",s1[i].status);
    }
}

void sort()

{

    int i;


    for ( i = 0; i <5; i++ ){//single pass
    if (strcmp( s1[i].name, s1[i+1].name ) < 0 )
    {//sort in order of last name

    s1[i] = s1[i+1];//swap array elements if statement is true
    }
}
}

Recommended Answers

All 2 Replies

i also need to do binary search for the name. this is a code i found on web and tried to put in my program but theres more problem( i never did binary search)

void search()
{
    int entries;
    char searched[30];
    int j, low, high;

    int flag_found(0);


    printf("\nPlease enter name to look for:  ");

    gets(searched);


    low=1;
    high= entries-1;
    flag_found=0;


    do
    {
            j = ((low + high)/2);

            if(!strcmp(s1[j].name,searched))
            {
                flag_found = 1;
            }

            else
                {
                    if ((s1[j].name > searched)>0)
                    {
                        high = j - 1;
                    }
                    else
                        {
                            low = j + 1;
                        }
                    }
            }while ((low <= high) && (flag_found == 0));


    if(flag_found==1)
    {

        printf("Here is the person you asked for:%c \n\n", s1[].name);





    }

    else
    {


        printf("Name entered was not found in database!");



    }
}

In your binary search, the logic is wrong. If the word being compared is < target word (has a negative number returned from strcmp()), then you need to have the high brought down mid-1

And if the word being compared is > the target word, you need to have the low brought UP to mid+1.

So your binary search is completely backwards. Try it and see. Understand that the wrong logic wil occassionally find a word anyway, however.

Sorting by last name will be a problem because you don't have a lname field in your student struct - just name.

I would break it up to lname and fname, and make both char arrays. THEN you can sort by last name, otherwise you are sorting by whatever is first in the name field.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.