ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int i = 0;
int tmpArray;
int result;
while (i < N-1){
result = compare_words(word_array[i], word_array[i+1]);
printf("%d",result);
if (result = 0){
tmpArray = *word_array[i];
*word_array[i] = *word_array[i+1];
*word_array[i+1] = tmpArray;
}
i = i + 1;
}
result =0 means word_array[i] >word_array[i+1]
how can i make this to work?
Thanks in advance.
Last edited by kponenation; 11-22-2005 at 01:53 AM.
i figured out the comparing part ....
i need to know how to sort the words now...
i think my algorithm is correct (i already wrote this in Java)
but it doesn't work.
Originally posted by kponenation The followings are my code
Code:
int i = 0;
int tmpArray;
int result;
while (i < N-1){
result = compare_words(word_array[i], word_array[i+1]);
printf("%d",result);
if (result = 0){
tmpArray = *word_array[i];
*word_array[i] = *word_array[i+1];
*word_array[i+1] = tmpArray;
}
i = i + 1;
}
result =0 means word_array[i] >word_array[i+1]
how can i make this to work?
Thanks in advance.
does this code really compile?
by the looks of the code word_array is an array of pointers to strings, is this correct?
but then this line confuses me
Code:
tmpArray = *word_array[i];
if word_array is an array of strings
then word_array[i] is a pointer to a string, then dereferencing it gives the first char in the array, and you put this in an int(which is the same size as a char);
it looks like you are trying to sort strings(whole words) but then you do
Thanks for your reply.
i'll just post the function sort()
Code:
int sort_words(char **word_array, int N ){
int i = 0;
int tmpArray;
int result;
while (i < N-1){
result = compare_words(word_array[i], word_array[i+1]);
printf("%d",result);
if (result = 0){
tmpArray = *word_array[i];
*word_array[i] = *word_array[i+1];
*word_array[i+1] = tmpArray;
}
i = i + 1;
}
return 0;
}
Above code did compile w/o any warnings or errors. word_array is a double pointer to a two dimentional array.
Thank you.
int sort(char **word_array, int N){
int i = 0;
int j = 0;
int tmpArray;
int result;
while (i < N){
while(j < N-1){
result = compare_words(word_array[j], word_array[j+1]);
printf("%d",result);
if (result == 0){
tmpArray = *word_array[j];
*word_array[j] = *word_array[j+1];
*word_array[j+1] = tmpArray;
}
j = j +1;
}
i = i + 1;
}
return 0;
}
Originally posted by kponenation I modified my code a little
Code:
int sort(char **word_array, int N)
{
int i = 0;
int j = 0;
int tmpArray;
int result;
while (i < N)
{
while(j < N-1)
{
result = compare_words(word_array[j], word_array[j+1]);
printf("%d",result);
if (result == 0)
{
tmpArray = *word_array[j];
*word_array[j] = *word_array[j+1];
*word_array[j+1] = tmpArray;
}
j = j +1;
}
i = i + 1;
}
return 0;
}
ok i see what you are trying to do, but thes lines still confuse me.
after the first line what do you think the value of tmpArray is?
why do you have two while loops? j is never reset so if the inner loop takes j to its max value the inner loop is exited and i is incremented but j is still its max value so the inner loop is not entered.
why does the func return and int?
value of tmpArray is the address of word_array[j] right?
word_array[0] = program
word_array[1] = linux
so word_array[0][0] = p
word_array[0][1]= r
word_array[1][0]= l
word_array[1][2]= n
returning int doesn't really matter. i just need to re-arrange (sort) the array and save it.
When i run my program it stops after tmpArray = *word_array[j];
it won't run *word_array[j] = *word_array[j+1];
Why is that?
Thanks.
value of tmpArray is the address of word_array[j] right?
i'll have to write a small test programme to validate this, but you are trying to store a hex address in an int, which is then cast to a char? this just looks very wrong to me and i would be very suprised if you get the outcome you wanted.
char* words[20];
words[0]="program\0";
words[1]="linux\0";
//print before we swap;
printf("word one:%s\n word two:%s\n",words[0],words[1]);
//swap program and linux aroung
char* temp;
temp = words[0];
words[0] = words[1];
words[1] = temp;
//print after we swap;
printf("word one:%s\n word two:%s",words[0],words[1]);
yeah... but the problem is i need to make the array two dimensional because i first need to compare the two strings (each character) to see which one comes before.
so i can't do it like what you did.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.