LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2006, 09:30 PM   #16
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0

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
 
Old 11-04-2006, 09:38 PM   #17
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
swap != swap words. Also, if swap_words is supposed to be local to that file only, put it in an anonymous namespace like
Code:
namespace // no name, anonymous
{
   // stuff
}
Also, if your code is assuming something (like the words really being the right size) then assert() it, after #include <cassert>
 
Old 11-04-2006, 09:52 PM   #18
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
oh my god, I can be really stupid at times. Thanks very much though.
 
Old 11-04-2006, 09:57 PM   #19
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
Quote:
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.
 
Old 11-05-2006, 10:45 AM   #20
jubaitca
LQ Newbie
 
Registered: Nov 2006
Posts: 13

Original Poster
Rep: Reputation: 0
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 there anyhting wrong with my code?
 
Old 11-05-2006, 06:55 PM   #21
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
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
Code:
strncpy(strlen(source_string) + 1)
so that you are always moving the null byte, too.

Last edited by dogpatch; 11-05-2006 at 06:57 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
true or false? alaios Programming 7 07-16-2005 10:54 AM
warning: comparison is always false due to limited range of data type hubabuba Programming 13 12-21-2004 10:03 AM
Return true or false if I have ping reply Menestrel Programming 4 11-29-2004 12:40 AM
question about /etc/false notstrider Debian 2 10-23-2004 12:52 AM
False installation p0rtzer0 Linux - Newbie 2 09-23-2004 10:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration