LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple program (https://www.linuxquestions.org/questions/programming-9/simple-program-109002/)

bprasanth_20 10-27-2003 05:41 AM

simple program
 
http://www.linuxquestions.org/questi...hreadid=108981

the above link has a program for insertion sort for sorting integers. IMPORTANT NOTE : please change the line

esize = malloc(sizeof(int));

to

esize = sizeof(int);

now i want a program to sort strings(not only single letters but also words). i used char data[100]; instead of int data[100]; and in printf and scanf statements i used ("%d", &data[i]);
but the result is coming as:

[prasanth@chanduram prasanth]$ cc issort2.c
[prasanth@chanduram prasanth]$ ./a.out

Enter the number of elements : 5

Enter the list of elements :
a
b
d
e
c

After insertion sort the list of elements :
abcde
bcde
cde
de
e


and for words i get :

[prasanth@chanduram prasanth]$ ./a.out

Enter the number of elements : 5

Enter the list of elements :
elf
bat
dog
ant
cat

After insertion sort the list of elements :
abcdeat
bcdeat
cdeat
deat
eat


please help me

Hko 10-27-2003 12:03 PM

You need to change your data_compare() function to compare strings instead of char's.
Hint: use strncpy()
<edit>
strncpy() is the wrong function.
I meant to say strncmp()
</edit>

acid_kewpie 10-27-2003 03:06 PM

bprasanth_20, please stick to one thread, do not repost a brand new thread when you change 1 line of code... i have closed you previous thread. thanks.

bprasanth_20 10-28-2003 03:45 AM

HKo ur suggsetion is nice but the output is the same man
i think the problem is within the lines printf("%s", &data[i]); and scanf("%s", &data[i]); . please try to work out the program in ur linux c and help me

Hko 10-28-2003 12:01 PM

Just change data[] to hold strings instead of char's too.
Quote:

please try to work out the program in ur linux c and help me
Have you tried to find out yourself yet?

bprasanth_20 10-29-2003 12:55 AM

not only me. 3 other pple tried it out. they souldnt find out the problem. thats why i asked u to run the program in ur linux machine.they told me to use a 2 dimensional array for data to hold charecters. but that requires changes in the program issort.c. but no changes should be made in issort.c. changes can be made only in issort1.c. To refer them use the link:

http://www.linuxquestions.org/questi...hreadid=108981


All times are GMT -5. The time now is 10:54 PM.