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.
Ok after trying to fix the errors, but I still get some errors. I think something is wrong when i am passing in the value.
Here is my new code:
Code:
void SelectionSort(char arr[][MAX_WORD_LENGTH+1], const int size)
{
int smallest_index;
for (int i= 0; i <=size; i++)
{
smallest_index = i;
for (int j = i + 1; j <= size; j++)
{
if (strcmp(arr[j],arr[smallest_index]) < 0)
{
smallest_index=j;
}
}
swap(arr[i],arr[smallest_index]);
}
}
void swap_words(char arr1[],char arr2[])
{
char temp[strlen(arr1)];
strncpy(temp,arr1,strlen(arr1));
strncpy(arr1,arr2,strlen(arr2));
strncpy(arr2,temp,strlen(temp));
}
Here is new set of error:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h: In function `
void std::swap(_Tp&, _Tp&) [with _Tp = char[26]]':
test.c:73: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h:130: error: in
valid initializer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h:131: error: IS
O C++ forbids assignment of arrays
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algobase.h:132: error: IS
O C++ forbids assignment of arrays
if you can please give me a hand with my algorithm (my basic approach to do it). Is there something wrong with it?
As a basic approach, i would suggest you take one step at a time, instead of trying to code the entire program at once.
1.Code functions as stubs that do nothing but accept the arguments.
2.Understand and correct compile errors one at a time.
3.When you get a clean compile, finish coding each function one at a time.
4.When you finally have a clean compile, and the program doesn't work right, post a thread with the specifics of what's going wrong.
Thanks for the advice. I am error free now. But, now I run into a bigger problem. My output is not quite right.
My code now is the following:
Code:
void SelectionSort(char arr[][MAX_WORD_LENGTH+1], const int size)
{
int smallest_index;
for (int i= 0; i <=size; i++)
{
smallest_index = i;
for (int j = i + 1; j <= size; j++)
{
if
(strncmp(arr[j],arr[smallest_index],strlen(arr[smallest_index])) < 0)
{
smallest_index=j;
}
}
swap_words(arr[i],arr[smallest_index]);
}
}
void swap_words(char arr1[],char arr2[])
{
char temp[strlen(arr1)];
strncpy(temp,arr1,strlen(arr1));
strncpy(arr1,arr2,strlen(arr2));
strncpy(arr2,temp,strlen(temp));
}
And my output is:
Quote:
the
war
of
the
worlds
by
h
g
-ells
-ook
-ne
-he
-oming
-f
-he
-artians
10,000,000
140,000,000
1894
2ve
35,000,000
ahe
aar
aut
aho
ahall
awell
an
ahese
aorlds
af
ahey
ae
anhabited
ahe˙d◄A
aar˙d◄A
afr˙d◄A
are
ae
ar
ahey
aords
af
ahe
aorld
ahe˙d◄A
aorldsA
ayrld◄A
and
aow
are
all
ahings
aade
aor
aan
aells◄A
aepler
auoted
an
ahe
anatomy
af
aelancholy
ao
ane
aould
aave
aelieved
about
about
about
about
about
about
abouteenth
aboutry
about
abundance
accelerated
across
across
acrossd
across
activity
adjacent
admits
advancegences
affairs
after
after
after
after
after
againl
against
against
against
agot
air
air
I think its repeating a lot of words, and some words aren't even being alphabetized. For example, the words in the begenning(they are in the begenning of the text file as well) aren't being sorted for some reason. And I think a lot of junk words are put in as well.
Is the const variable 'size' what it appears to be, the size of the array? If so, your for loop stop condition should be using '<', not '<=' to quit the loop at the end of the array.
Secondly, your use of strncpy() on variable length words may be having unintended results. strncpy() will move 'n' characters from one string to the other. By moving only strlen(source_string) number of characters, you are not moving the null terminator to the destination string, which means the destination string may end up being longer than the source. Try
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.