943,924 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 8283
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 2nd, 2006
0

Re: Copying words into an array of char ?!?

I GIVE UP !!!!!

Ive been trying to program for over two years now, have been reading several books, did all exercises, but for some unknown reason, I'm not getting it.

So, after some thought about it, Ive decided to stop, I want to thank all of you who have helped me the past two years here on Daniweb and wish you all the best.

Kind regards,
Johan aka JoBe
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Dec 2nd, 2006
0

Re: Copying words into an array of char ?!?

*sigh* We really will miss you JoBe, though I wish you hadn't given up.

Your friend always,
~s.o.s~
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 3rd, 2006
0

Re: Copying words into an array of char ?!?

Thanks for the kind words s.o.s, but, I can't stop untill I know the answer, so if someone would please help me out with this, Ive been trying and I can't find the solution.

The code Ive got is this:

  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. int main()
  5. {
  6. char wordArr[5][10];
  7. bool contr = false;
  8. size_t i, j;
  9.  
  10. for(i = 0; i < 5; i++)
  11. std::cin >> wordArr[i];
  12.  
  13. for(i = 0; i < 5; i++)
  14. {
  15. for(j = 0; j < i; j++)
  16. {
  17. contr = strcmp(wordArr[i], wordArr[j]);
  18.  
  19. if (contr == true)
  20. contr = true;
  21. }
  22. if (contr == false)
  23. std::cout << wordArr[i] << '\n';
  24. }
  25.  
  26. std::cin.ignore(2);
  27.  
  28. return 0;
  29. }

But, if I use the input:
one
two
one
four
five

The output is:
one

What am I doing wrong

Getting so frustrated by not finding this solution :mad: And it is probably something simple that I can't see !!!!!!
Last edited by JoBe; Dec 3rd, 2006 at 5:19 am.
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Dec 3rd, 2006
0

Re: Copying words into an array of char ?!?

#include <iostream>
#include <cstring>

int main()
{
	char wordArr[5][10];
	bool contr = false;
	size_t i, j;

	for(i = 0; i < 5; i++)
		std::cin >> wordArr[i];

	for(i = 0; i < 5; i++)
	{
		for(j = 0; j < i; j++)
		{
			contr = strcmp(wordArr[i], wordArr[j]);
			// If the above line sets contr to true or false...
			if (contr == true)    // what are these two lines for?
				contr = true;
		}
			if (contr == false)
				std::cout << wordArr[i] << '\n';
	}

	std::cin.ignore(2);
	
	return 0;
}
What is it you are trying to do?
Last edited by WaltP; Dec 3rd, 2006 at 5:43 am.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,738 posts
since May 2006
Dec 3rd, 2006
0

Re: Copying words into an array of char ?!?

Hi Walt,


Well, Lerner posted this helpfull piece of pseudo code:
  1. bool found = false
  2. for each word in the array //this word
  3. for each word prior to this word //current word
  4. if this word is the same as current word
  5. set found to true
  6. if not found
  7. print out this word

And I thought, oh, now I get it:

-First I have a loop for the whole array.
-Then, I have a loop for the word that is being checked whether it exists allready.
-Then, if that word is the same one of the allready added words in the first loop, put the bool variable to true, if not, at the end of looping threw the second loop. Output the word, since it hasn't been added yet.

The idea of the exercise now is to print every word only once, meaning:
one
two
one
four
five

would have to print:
one
two
four
five

Somehow, I'm just not seeing what it is that is wrong with the code Ive written with the help of the pseudo code from Lerner.
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Dec 3rd, 2006
0

Re: Copying words into an array of char ?!?

Click to Expand / Collapse  Quote originally posted by JoBe ...
Hi Walt,


Well, Lerner posted this helpfull piece of pseudo code:
  1. bool found = false
  2. for each word in the array //this word
  3. for each word prior to this word //current word
  4. if this word is the same as current word
  5. set found to true
  6. if not found
  7. print out this word

And I thought, oh, now I get it:

-First I have a loop for the whole array.
-Then, I have a loop for the word that is being checked whether it exists allready.
-Then, if that word is the same one of the allready added words in the first loop, put the bool variable to true, if not, at the end of looping threw the second loop. Output the word, since it hasn't been added yet.

The idea of the exercise now is to print every word only once, meaning:
one
two
one
four
five

would have to print:
one
two
four
five

Somehow, I'm just not seeing what it is that is wrong with the code Ive written with the help of the pseudo code from Lerner.
Hint: wouldn't it be a lot easier if you sorted the current words into alphabetical order first.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 3rd, 2006
0

Re: Copying words into an array of char ?!?

Click to Expand / Collapse  Quote originally posted by iamthwee ...
Hint: wouldn't it be a lot easier if you sorted the current words into alphabetical order first.
Probably, but the exercise goes as follows:

- Read a sequence of words from input. Use Quit as a word that therminates input. Print the words in the order they were entered. Don't print a word twice. Modify the program to sort the words before printing them.

So,
1) Read a sequence of words from input.
2) Use Quit as a word that terminates input.
3) Print the words in the order they were entered.
4) Don't print a word twice.
And
5) Modify the program to sort the words before printing them.

I could try what you suggest, but, then I won't be doing it in the order the exercise says, probably for the reason that N°4 would become easier

Anyway, thanks for the tip iamthwee
Last edited by JoBe; Dec 3rd, 2006 at 9:34 am.
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Dec 3rd, 2006
0

Re: Copying words into an array of char ?!?

>I could try what you suggest, but, then I won't be doing it in the order the exercise says, probably for the reason that N°4 would become easier

It certainly would be a lot easier. Which is how most hash tables operate I would imagine. I.e sorting the list first before removing duplicates.

Of course after sorting you lose the order you begin with.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 3rd, 2006
0

Re: Copying words into an array of char ?!?

Quote ...
Somehow, I'm just not seeing what it is that is wrong with the code Ive written with the help of the pseudo code from Lerner.
Okay. Here this goes.

What Mr. Lerner tried to explain is that if you start from the start of the array, you would have a hard time keeping track of duplicates.

Eg. one, two, one, two, three

If you begin at the start of the array you have no way of knowing that the current word under consideration would be repeated or not i.e. if we consider the first word "one" we have no way of knowing that "one" would occur again or not.

The only logical thing to beat this problem would be to start from the end of the array, eg. "three", look up for it in the whole array, and see if it occurs anywhere in the whole array. If it does, don't print "three" because we know for sure that we will encounter "three" again definately.

Quote ...
A sample run for input "one, two, one, two, three"

Current Word: three
Searching upwards for "three"...not found
Print "three"

Current word: two
Searching upwards for "two"...found
Dont print "two"

Current word: one
Searching upwards for "one"...found
Dont print "one"

Current word: two
Searching upwards for "two"...not found
Print "two"

Current word: one
Searching upwards for "one"...not found
Print "one"
I won't let you give up so easily

Hope it helped, repost if necessary.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 3rd, 2006
0

Re: Copying words into an array of char ?!?

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
Okay. Here this goes.

What Mr. Lerner tried to explain is that if you start from the start of the array, you would have a hard time keeping track of duplicates.
Ah, ok, let me see whether I can find a solution that way.


Quote ...
I won't let you give up so easily
Thanks for sticking in s.o.s Programming is sometimes so frustrating, but, I can't seem to let it go, seems like Ive been bitten by the microbe, problem is, my brains don't seem to be willing to help out here. Glad that this is merely a hobby of mine and I don't have to earn a living with it, wouldn't be earning alot with it :cheesy:

Quote ...
Hope it helped, repost if necessary.
I most certainly will s.o.s, but, it'll probably will be next week since during the week, I don't have time, full time job and other responcebilities.
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Linker info
Next Thread in C Forum Timeline: Deleting characters function ( with just one parameter)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC